BrainBit for developers Subscribe for updates Visit website

Connect to device

Creating

You need to create a device using a scanner and info about device receiving while search. All manipulations with the device will be performed without errors only if the device is connected. When created, the device is automatically connected. In the future, the connection state can be controlled through the sensor object. The sensor allows you to monitor the status of the device, set parameters, receive a signal of various types.

OpStatus outStatus;

// retrieve information from a list or retrieve stored information from a callback
int32_t szSensorsInOut = 32;
SensorInfo* sensors = new SensorInfo[szSensorsInOut];
sensorsScanner(scanner, sensors, &szSensorsInOut, &outStatus);

SensorInfo sensorInfo = sensors[0];
Sensor* sensor = createSensor(scanner, sensorInfo, &outStatus);
            
// retrieve information from a list or retrieve stored information from a callback
List<SensorInfo> sensors = scanner.getSensors();
SensorInfo sensorInfo = sensors.get(0);

BrainBit sensor = (BrainBit)scanner.createSensor(sensorInfo);
            
// retrieve information from a list or retrieve stored information from a callback
val sensors = scanner!!.sensors
val sensorInfo: SensorInfo = sensors[0]

var sensor: Sensor? = scanner.createSensor(sensorInfo) as BrainBit
            
// retrieve information from a list or retrieve stored information from a callback
IReadOnlyList<SensorInfo> sensors = scanner.Sensors;
SensorInfo sensorInfo = sensors[0];

BrainBitSensor sensor = scanner.CreateSensor(sensorInfo) as BrainBitSensor;
            
// retrieve information from a list or retrieve stored information from a callback
var sensors = await scanner.Sensors()
var sensorInfo = sensors[0]

BrainBit sensor = scanner.CreateSensor(sensorInfo) as BrainBit
            
# retrieve information from a list or retrieve stored information from a callback
sensors = scanner.sensors()
sensorInfo = sensors[0]

sensor = scanner.create_sensor(sensorInfo)
            
// retrieve information from a list or retrieve stored information from a callback
var sensors = scanner?.sensors
var sensorInfo = sensors[0]

var sensor: NTBrainBit? = scanner.createSensor(sensorInfo) as? NTBrainBit
            
// retrieve information from a list or retrieve stored information from a callback
NSArray<NTSensorInfo*>* sensors = scanner.sensors;
NTSensorInfo* sensorInfo = sensors[0];

NTBrainBit* sensor = (NTBrainBit*)[scanner createSensor:sensorInfo];
            
// retrieve information from a list or retrieve stored information from a callback
List<SensorInfo?> sensors = await scanner.getSensors();
SensorInfo? sensorInfo = sensors[0];

// BrainBit
BrainBit? sensor = await scanner.createSensor(info!) as BrainBit;
            

            

Device creation is a blocking method, so it must be called from separate thread.

For all types of devices, you can use the same methods to control the device's connection status, invoke commands, and check for functionality.

Manage connection state

Connection status can be obtained in two ways.

The first one is using the sensor parameter `State`.

The second way is in real time using a callback:

void onConnectionStateCallback(Sensor* sensor, SensorState State, void* userInfo)
{

}

OpStatus outStatus;
SensorStateListenerHandle lhandle = nullptr;
addConnectionStateCallback(sensor, onConnectionStateCallback, &lhandle, nullptr, &outStatus);
            
sensor.sensorStateChanged = state -> {
    Log.i("TAG", state.toString());
};
...
sensor.sensorStateChanged = null;
            
sensor.sensorStateChanged = Sensor.SensorStateChanged { state ->
    Log.i("TAG", state.toString())
}
...
sensor.sensorStateChanged = null
            
private void EventSensorStateChanged(ISensor sensor, SensorState sensorState)
{
    Console.WriteLine(sensorState);
}
...
sensor.EventSensorStateChanged += EventSensorStateChanged;
...
sensor.EventSensorStateChanged -= EventSensorStateChanged;
            
sensor.AddConnectionChanged((state)=>{           
    console.log(SensorState(state))
})
...
sensor.RemoveConnectionChanged()
            
def on_sensor_state_changed(sensor, state):
    print('Sensor {0} is {1}'.format(sensor.Name, state))

sensor.sensorStateChanged = on_sensor_state_changed
...
sensor.sensorStateChanged = None
            
sensor?.setConnectionStateCallback({ state in
    print(state)
})
...
sensor?.setConnectionStateCallback(nil)
            
[sensor setConnectionStateCallback:^(NTSensorState state) {
    NSLog(@"%@", @(state));
}];
...
[sensor setConnectionStateCallback:nil];
            
StreamSubscription<SensorState>? stateSubscription = sensor.sensorStateStream.listen((state) => print("State: $state"));
...
stateSubscription.cancel();
            

            

Important! The state change callback will not come after the device is created, only after disconnecting (device lost from the scope or using a method) and then connected. The device does not automatically reconnect.

You can connect and disconnect from device manually by methods `Connect()` and `Disconnect()`. To receive connection state in real time you need to subscribe to `stateChanged` event. Also you can get connection state by sensor's parameter.

OpStatus outStatus;
connectSensor(sensor, &outStatus);

disconnectSensor(sensor, &outStatus);
            
sensor.disconnect();
...
sensor.connect();
            
sensor.disconnect()
...
sensor.connect()
            
sensor.Disconnect();
...
sensor.Connect();
            
sensor.Disconnect()
...
sensor.Connect()
            
sensor.disconnect()
...
sensor.connect()
            
sensor.disconnect()
...
sensor.connect()
            
[sensor Disconnect];
...
[sensor Connect];
            
await sensor.disconnect();
...
await sensor.connect();
            

            

Method `Connect` is blocking too, so it need to be call from separate thread. 

Device finalization

When you are done with the device, it is recommended to disconnect from the device and clear the taken resources.

OpStatus outStatus;
disconnectSensor(sensor, &outStatus);
freeSensor(sensor);
            
sensor.disconnect();
sensor.close();
            
sensor.disconnect()
sensor.close()
            
sensor.Disconnect();
sensor.Dispose();
            
sensor.disconnect()
sensor.close()
            
sensor.disconnect()
del sensor
            
sensor.disconnect()
sensor = nil
            
[sensor Disconnect];
sensor = nil;
            
await sensor.disconnect();
sensor.dispose();
            

            

Battery

Also, you can get power value from each device by sensor parameter`BattPower` or by callback in real time:

void onBatteryCallback(Sensor* sensor, int32_t battery, void* userData)
{

}

OpStatus outStatus;
BattPowerListenerHandle lhandle = nullptr;
addBatteryCallback(sensor, onBatteryCallback, &lhandle, nullptr, &outStatus);
            
sensor.batteryChanged = battery -> {
    Log.i("TAG", "Power: " + battery);
};
...
sensor.batteryChanged = null
            
sensor.batteryChanged = Sensor.BatteryChanged { power ->
    Log.i("TAG", power.toString())
}
...
sensor.batteryChanged = null
            
private void EventBatteryChanged(ISensor sensor, int battPower)
{
    Console.WriteLine("Power: " + battPower);
}
...
sensor.EventBatteryChanged += EventBatteryChanged;
...
sensor.EventBatteryChanged -= EventBatteryChanged;
            
sensor.AddBatteryChanged((power)=>{
    console.log(power)
})
...
sensor.RemoveBatteryChanged()
            
def on_battery_changed(sensor, battery):
    print('Battery: {0}'.format(battery))
...
sensor.batteryChanged = on_battery_changed
...
sensor.batteryChanged = None
            
sensor?.setBatteryCallback({ battPower in
    print("Battery: \(battPower.intValue)")
})
...
sensor?.setBatteryCallback(nil)
            
[sensor setBatteryCallback:^(NSNumber * _Nonnull battPower) {
    NSLog(@"Battery %d", [battPower intValue]);
}];
...
[sensor setBatteryCallback:nil];
            
StreamSubscription<int>? powerSubscription = sensor.batteryPowerStream.listen((power) => print("Battery: $power"));
...
powerSubscription.cancel();