If you want to connect to the NeuroSky ThinkGear API -- what they call the ThinkGear Socket Protocol -- this code shows you how to do it, at least in Scala. It consists of a few pieces of code I pulled from this GitHub repo. I wrote this code to help debug a problem I was seeing with the data.
The RawReader class does all the work. It opens a socket on the right port, then sends the necessary JSON to that socket to get things started. After that I just read the raw data that the API writes to the socket.
Here’s the source code:
package com.alvinalexander.neurosky
import java.io.OutputStreamWriter
import java.net.Socket
import java.io.{BufferedReader, InputStreamReader}
import scala.util.parsing.json.JSONObject
object RawRecorder extends App {
val reader = new RawReader
reader.start
Thread.sleep(30*1000)
reader.stop
}
class RawReader extends Thread {
val host = "127.0.0.1"
val port = 13854
val rawOutput = false
val jsonFormat = true
val socket = new Socket(host, port)
val neuroInput = new BufferedReader(new InputStreamReader(socket.getInputStream))
val neuroOutput = new OutputStreamWriter(socket.getOutputStream)
def configure(rawOutput: Boolean = false, jsonFormat: Boolean = true) {
val jsonConfig = new JSONObject(Map("enableRawOutput" -> rawOutput,
"format" -> (if (jsonFormat) "Json" else "BinaryPacket")))
neuroOutput.write(jsonConfig.toString())
neuroOutput.flush()
}
configure(rawOutput, jsonFormat)
override def run {
while (true) {
val line = neuroInput.readLine
println(line)
}
}
}
This is written in Scala, so it’s an easy port to Java if someone needs that. Well, except for the JSON part, you’ll need to handle that differently in Java, but everything else is a simple port.
If you need to connect to the NeuroSky ThinkGear Socket Protocol API using Scala, I hope this has been helpful.

