Usage#
This guide provides a step-by-step example of how to connect to a BrainAccess device, start streaming data, and disconnect using the BrainAccess Kotlin SDK.
Connecting to a Device#
The following example demonstrates the basic workflow for connecting to a BrainAccess device and collecting EEG data.
1// 1. Initialize the BrainAccess Library
2val baLib = BALib(context)
3
4// 2. Scan for BrainAccess devices
5baLib.scanForDevices()
6
7// 3. Collect the list of discovered devices
8val discoveredDevices = baLib.devices
9
10// 4. Choose a device and connect to it
11val myDevice = discoveredDevices.first()
12myDevice.connect()
13
14// 5. Start the data stream
15myDevice.startStream()
16
17// 6. Collect data from the stream in a coroutine
18lifecycleScope.launch {
19 myDevice.processedDataFlow.collect { data ->
20 // Handle the incoming EEG data
21 println("Received data: $data")
22 }
23}
24
25// 7. Stop the data stream when you're done
26myDevice.stopStream()
27
28// 8. Disconnect from the device
29myDevice.disconnect()
Explanation#
Initialization: First, create an instance of BALib, passing in the Android context.
Scanning: Call scanForDevices() to start searching for nearby BrainAccess devices.
Device List: The devices property of your BALib instance will be updated with the list of discovered devices.
Connecting: Select a device from the list and call its connect() method to establish a connection.
Streaming: Once connected, you can start the data stream by calling startStream().
Data Collection: The processedDataFlow provides a Kotlin Flow that emits EEG data. It’s best to collect this data within a coroutine to avoid blocking the main thread.
Stopping the Stream: When you no longer need the data, call stopStream().
Disconnecting: Finally, call disconnect() to close the connection to the device.
For a complete, working example, please see the Examples page.
Versions used for testing:#
agp = “8.10.1” blueFalcon = “2.0.0” kotlin = “2.0.21”