Explaining how MetroEEG works

Some developers may wish to not include MetroEEG directly but include lower-level code. In this sample we'll endeavor to show a standalone example of getting information from a Mindwave Mobile headset into WP8.

Connecting to a Mindset Mobile Headset
Using the new Bluetooth support in WP8, it's possible to iterate over all paired Bluetooth devices and open a BT-SPP socket to them.

Next we iterate over that collection attempting to find one with the word "Mindwave" in it. If we don't find one, that could be for multiple reasons: Phone Bluetooth is off, mindwave is off, both devices aren't paired, etc.

Reading BT-SSP Packet from Mindwave
Mindwave tends to be very chatty and has over 500+ messages every seconds. Those messages are received over as Bytes by C#. We can see the message format at
Mindset Communications Protocol.

We'll use socket.InputStream.ReadAsync to read the data sent over by Mindwave.

Mindwave sends back A LOT of data. It's quite easy to see a pattern of using 2 bytes of
170, 170 as a separator between message

We're interested in a particular packet that start with 3 bytes of
170,170,32 and has an overall length of 36 bytes. for example:
170,170,32,2,25,131,24,0,0,132,0,0,53,0,0,22,0,0,12,0,0,10,0,0,5,0,0,1,0,0,1,4,0,5,0,84

So, we'll check if the 512 bytes we just read has that
170,170,32 header in it.

One unfortunate thing that might happen is that we get 512 bytes that have the header for our 36 bytes packet, but only a part of it. And we might need to read another 512 bytes to get the full 36 byte packet. Once we have that we'll take the length of
the data packet (and 2 more bytes to see the next header).