Skip to content
This repository was archived by the owner on Nov 28, 2026. It is now read-only.
aasb13Public archive

About

My first hackathon: an experimental C++ bridge for the BrainBit EEG headband, streaming EEG, electrode impedance, battery and connection state plus spectral band powers over TCP port 9900.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

bbitcpp

A small C++ bridge between a BrainBit EEG headband and anything that can open a TCP socket. It connects to one BrainBit device over Bluetooth LE, forwards the vendor SDK callbacks as compact binary packets on port 9900, and computes EEG band powers with the vendor's EmotionsStateArtifacts library.

Status: experimental. This was my first hackathon (Cybergarden), written as a workaround for the official, proprietary SDK samples. It is not affiliated with or supported by the device vendor, and the hardware itself is prone to disconnects and inconsistent readings. Use it for experiments, not for anything clinical.

Outcome: no prize. I'm angry that none of the hackathons won anything.

Why this exists

The official SDK is closed-source, its samples assume Visual Studio 2019 on Windows, its CMake examples are incomplete, its Linux D-Bus integration is unreliable, and its documentation is poor. This project replaces the sample app with a single C++17 binary built by Meson, with a simple socket interface that any language can consume.

How it works

BrainBit (BLE) ──neurosdk2──> main.cpp ──binary packets──> TCP :9900 ──> your client
                                  └──libem_st_artifacts──> band-power packets ──┘
  1. A TCP server thread listens on 0.0.0.0:9900 and accepts one client at a time.
  2. The math library is initialised for 250 Hz, two bipolar channels, with artifact filtering.
  3. A BLE scanner looks for a device with a hard-coded MAC address (30-second timeout).
  4. Once connected, battery, connection-state, signal and electrode-resistance callbacks are registered, a 6-second calibration runs, then EEG streaming starts.
  5. Every signal callback is forwarded verbatim to the client and also pushed into the math library; the resulting delta/theta/alpha/beta/gamma percentages are sent as separate packets.

Packets sent while no client is connected are dropped (there is no buffering or replay).

Requirements

  • C++17 compiler, Meson and Ninja (or another Meson backend)
  • Bluetooth LE interface and a powered, discoverable BrainBit headband
  • Prebuilt vendor libraries are included in lib/: NeuroSDK 2 (Linux x64 .so; Windows x86/x64 .dll + .lib) and EmotionsStateArtifacts (Linux .so, Windows .dll + .lib)
  • On Linux the SDK may additionally need BlueZ/D-Bus; see Known limitations

Configure

Edit src/main.cpp before building:

What Where Default
Target device MAC targetMac (line 79) EF:9A:53:8F:57:7E
TCP port htons(9900) in tcpServerThread() 9900
Scan timeout waitForTargetDevice() argument 30 seconds
Math/artifact settings and calibration length initEmotion(), performCalibration() 250 Hz, 2 channels, 6 s

The scanner only accepts devices in the SensorLEBrainBit family. If your device never shows up, double-check the address format (uppercase, colon-separated) and make sure it is actually advertising.

Build and run

Linux

./build.sh                 # meson setup build && meson compile -C build
./start.sh                 # runs ./build/siroware

Or manually:

meson setup build
meson compile -C build
LD_LIBRARY_PATH=lib/neurosdk2/linux/x64:lib/libemotions/linux_em_st_artifacts/astra/raw:$LD_LIBRARY_PATH ./build/siroware

Windows

build.bat
start.bat

build.bat expects meson and ninja on PATH. clean.bat / rebuild.bat remove and recreate the build directory; clean.sh is the Linux equivalent. The executable is named siroware (the project name inside meson.build).

Wire protocol

All multi-byte values are little-endian on the supported x86/x64 platforms. The fixed-size packet headers are byte-packed (#pragma pack(1)); signal samples use native struct layout (40 bytes: uint32 packet number, uint8 marker, 3 bytes padding, then four double channel values in O1, O2, T3, T4 order).

Every packet starts with an int32 type field:

Type Name Payload after the type field Total size
1 Battery int32 charge in percent 8 B
2 State int32 sensor state (0 = in range, 1 = out of range) 8 B
3 Signal int32 sample count, then that many 40-byte samples; channel values are in volts 8 + 40·N B
4 Resistance four float values: O1, T3, T4, O2 (ohms) 20 B
5 Spectrum five double values: delta, theta, alpha, beta, gamma (percent) 44 B

Band percentages are already multiplied by 100 by the sender. Concentration, relaxation and cognitive-load indices are computed and printed to stdout only — they are not part of the TCP protocol.

Minimal Python client:

import socket, struct

def recvn(sock, n):
    buf = b''
    while len(buf) < n:
        chunk = sock.recv(n - len(buf))
        if not chunk:
            raise EOFError('connection closed')
        buf += chunk
    return buf

s = socket.create_connection(('127.0.0.1', 9900))
while True:
    kind, = struct.unpack('<i', recvn(s, 4))
    if kind in (1, 2):
        value, = struct.unpack('<i', recvn(s, 4))
        print('battery' if kind == 1 else 'state', value)
    elif kind == 3:
        count, = struct.unpack('<i', recvn(s, 4))
        for _ in range(count):
            packet_no, marker, o1, o2, t3, t4 = struct.unpack('<IB3x4d', recvn(s, 40))
            print('EEG', packet_no, marker, o1, o2, t3, t4)
    elif kind == 4:
        print('resistance', struct.unpack('<4f', recvn(s, 16)))
    elif kind == 5:
        print('spectrum', struct.unpack('<5d', recvn(s, 40)))

The server binds to all interfaces, has no authentication and no encryption — run it on a trusted network or connect via localhost.

Known limitations

  • The target MAC is hard-coded; the headband must be on and advertising, and the author reports frequent disconnects, freezes and occasional bogus data from the device itself.
  • The proprietary NeuroSDK has known Linux D-Bus problems.
  • The socket code includes POSIX headers unconditionally, so native Windows builds need a Winsock2 adaptation. Meson targets and batch scripts for Windows are present but not verified here.
  • No client-side flow control, reconnection history, or protocol versioning: packets are simply written to the current socket.
  • This is a hackathon prototype, not a maintained SDK. Issues and patches are welcome via GitHub.

Repository layout

Path Contents
src/main.cpp Entire application: scanner, callbacks, math library, TCP server
meson.build Cross-platform build definition (produces the siroware executable)
build.sh, build.bat, rebuild.bat, clean.sh, clean.bat, start.sh, start.bat Build/run helper scripts
lib/neurosdk2/ Vendored NeuroSDK 2 headers and binaries
lib/libemotions/ Vendored EmotionsStateArtifacts headers, binaries and docs
logs/ SDK log directory (sdk_log.log)
include/, scripts/, files/ Reserved for project headers, generated scripts and data

License

Mozilla Public License 2.0. The vendored SDK binaries under lib/ are third-party proprietary components and are not covered by this project's license; check the vendor's terms before redistributing them.

About

My first hackathon: an experimental C++ bridge for the BrainBit EEG headband, streaming EEG, electrode impedance, battery and connection state plus spectral band powers over TCP port 9900.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages