BrainBit for developers Subscribe for updates Visit website

Sensor

Creating

You need to create a device using a scanner. All manipulations with the device will be performed without errors only if the device is connected.

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];
            

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];
            

A connection can be in two states: connected (InRange) and disconnected (OutOfRange).

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];
            

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

Manage device parameters

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];
            

 

Parameters

Each device has its own settings, and some of them can be configured as you need. Not all settings can be changed and not every device supports all settings.

First you need to find out what parameters the device supports and whether they can be changed:

int32_t szSensorParametersInOut = getParametersCountSensor(sensor);
ParameterInfo sensor_parameter[szSensorParametersInOut];
OpStatus outStatus;
getParametersSensor(sensor, sensor_parameter, &szSensorParametersInOut, &outStatus);
            
List<ParameterInfo> parameters = sensor.getSupportedParameters();
parameters.forEach(parameter -> Log.i("TAG", parameter.getParam() + ": " + parameter.getParamAccess()));

// Output:
// 
// ParameterGain: ReadWrite  
// ParameterState: ReadNotify
            
val parameters = sensor.supportedParameters
parameters?.forEach { parameter ->
    Log.i(
        "TAG",
        "${parameter.param}: ${parameter.paramAccess}"
    )
}

// Output:
// 
// ParameterGain: ReadWrite  
// ParameterState: ReadNotify
            
var parameters = sensor.Parameters;
            
console.log(sensor.Parameters.map(parameter => SensorParameter[parameter.Param] + ": " + SensorParamAccess[parameter.ParamAccess]))

// Output:
//
// ["Name: Read", 
// "FirmwareMode: Read", 
// "FirmwareVersion: Read", 
// "State: ReadNotify", ... ]
            
print(sensor.parameters) 

# Output:
# 
# [ParameterInfo(Param=<SensorParameter.ParameterOffset: 8>, ParamAccess=<SensorParamAccess.ParamAccessRead: 0>),  
# ParameterInfo(Param=<SensorParameter.ParameterState: 1>,  ParamAccess=<SensorParamAccess.ParamAccessReadNotify: 2>)
# ...]
            
var parameters = sensor?.parameters
            
NSArray<NTParameterInfo*>* parameters = [sensor Parameters];
            

 

Info about parameter includes two fields:

**ParameterInfo**
| Field | Type | Description |
|--|--|--|
|Param|SensorParameter|the name of the parameter, represented by an enumeration|
|ParamAccess|SensorParamAccess|parameter availability for manipulation|

**SensorParamAccess**
| Field | Value | Description |
|--|--|--|
|ParamAccessRead|0|read-only|
|ParamAccessReadWrite|1|parameter can be changed|
|ParamAccessReadNotify|2|parameter is updated over time|
            
**ParameterInfo**
| Field | Type | Description |
|--|--|--|
|Param|SensorParameter|the name of the parameter, represented by an enumeration|
|ParamAccess|SensorParamAccess|parameter availability for manipulation|

**SensorParamAccess**
| Field | Value | Description |
|--|--|--|
|Read|0|read-only|
|ReadWrite|1|parameter can be changed|
|ReadNotify|2|parameter is updated over time|
            
**ParameterInfo**
| Field | Type | Description |
|--|--|--|
|param|SensorParameter|the name of the parameter, represented by an enumeration|
|paramAccess|SensorParamAccess|parameter availability for manipulation|

**SensorParamAccess**
| Field | Value | Description |
|--|--|--|
|Read|0|read-only|
|ReadWrite|1|parameter can be changed|
|ReadNotify|2|parameter is updated over time|
            
**ParameterInfo**
| Field | Type | Description |
|--|--|--|
|Param|SensorParameter|the name of the parameter, represented by an enumeration|
|ParamAccess|SensorParamAccess|parameter availability for manipulation|

**SensorParamAccess**
| Field | Value | Description |
|--|--|--|
|Read|0|read-only|
|ReadWrite|1|parameter can be changed|
|ReadNotify|2|parameter is updated over time|
            
**ParameterInfo**
| Field | Type | Description |
|--|--|--|
|Param|SensorParameter|the name of the parameter, represented by an enumeration|
|ParamAccess|SensorParamAccess|parameter availability for manipulation|

**SensorParamAccess**
| Field | Value | Description |
|--|--|--|
|Read|0|read-only|
|ReadWrite|1|parameter can be changed|
|ReadNotify|2|parameter is updated over time|
            
**ParameterInfo**
| Field | Type | Description |
|--|--|--|
|Param|SensorParameter|the name of the parameter, represented by an enumeration|
|ParamAccess|SensorParamAccess|parameter availability for manipulation|

**SensorParamAccess**
| Field | Value | Description |
|--|--|--|
|Read|0|read-only|
|ReadWrite|1|parameter can be changed|
|ReadNotify|2|parameter is updated over time|
            
**NTParameterInfo**
| Field | Type | Description |
|--|--|--|
|param|NTSensorParameter?|the name of the parameter, represented by an enumeration|
|paramAccess|NTSensorParamAccess?|parameter availability for manipulation|

**NTSensorParamAccess**
| Field | Value | Description |
|--|--|--|
|read|0|read-only|
|readWrite|1|parameter can be changed|
|readNotify|2|parameter is updated over time|
            
**NTParameterInfo**
| Field | Type | Description |
|--|--|--|
|Param|enum NTSensorParameter|the name of the parameter, represented by an enumeration|
|ParamAccess|enum NTSensorParamAccess|parameter availability for manipulation|

**NTSensorParamAccess**
| Field | Value | Description |
|--|--|--|
|NTSensorParamAccessRead|0|read-only|
|NTSensorParamAccessReadWrite|1|parameter can be changed|
|NTSensorParamAccessReadNotify|2|parameter is updated over time|
            

 

You can also check if the parameter is supported, for example `Gain`:

if(isSupportedParameterSensor(sensor, SensorParameter::ParameterGain)){
    ...
}
            
if(sensor.isSupportedParameter(SensorParameter.ParameterGain)){
...
}
            
if(sensor.isSupportedParameter(SensorParameter.ParameterGain) == true){
...
}
            
if(sensor.IsSupportedParameter(SensorParameter.ParameterGain)){
...
}
            
if(sensor.IsSupportedParameter(SensorParameter.Gain)){
    ...
}
            
if sensor.is_supported_parameter(SensorParameter.Gain):
    ...
            
if(sensor?.isSupportedParameter(.gain)){
...
}
            
if([sensor IsSupportedParameter:NTSensorParameterGain]){
...
}