Synchroni sdk for react native
Synchroni SDK is the software development kit for developers to access Synchroni products.
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library
yarn add @synchroni/synchroni_sdk_react_nativeApplication will obtain bluetooth permission by itself. There are builtin code in SensorController for Android.
import {
SensorController,
SensorProfile,
DeviceStateEx,
DataType,
type BLEDevice,
type SensorData,
} from '@synchroni/synchroni_sdk_react_native';const SensorControllerInstance = SensorController.Instance;
//register scan listener
if (!SensorControllerInstance.hasDeviceCallback){
SensorControllerInstance.onDeviceCallback = (devices: BLEDevice[]) =>{
//return all devices doesn't connected
};
}Use public async startScan(periodInMs: number): Promise<boolean> to start scan
const success = await SensorControllerInstance.startScan(6000)returns true if start scan success, periodInMS means onDeviceCallback will be called every periodInMS, minium is 3000ms for iOS, 6000ms for Android
Use public async stopScan(): Promise<void> to stop scan
await SensorControllerInstance.stopScan();Use public get isScaning(): boolean to check scaning status
const isScaning = SensorControllerInstance.isScaning;Use public get isEnable(): boolean to check if bluetooth is enabled
const isEnable = SensorControllerInstance.isEnable;Use public requireSensor(device: BLEDevice): SensorProfile | undefined to create sensorProfile
If bleDevice is invalid, result is undefined
const sensorProfile = SensorControllerInstance.requireSensor(bleDevice);Use public getSensor(device: BLEDevice): SensorProfile | undefined to get sensorProfile
If SensorProfile didn't created, result is undefined
const sensorProfile = SensorControllerInstance.getSensor(bleDevice);Use public getConnectedSensors(): SensorProfile[] to get connected SensorProfiles
const sensorProfiles = SensorControllerInstance.getConnectedSensors();Use public getConnectedDevices(): SensorProfile[] to get connected BLE Devices
const bleDevices = SensorControllerInstance.getConnectedDevices();Please register callbacks for SensorProfile
let sensorProfile = SensorControllerInstance.requireSensor(bledevice);
//register callbacks
sensorProfile.onStateChanged = (sensor: SensorProfile, newstate: DeviceStateEx) => {
//please do logic when device disconnected unexpected
}
sensorProfile.onErrorCallback = (sensor: SensorProfile, reason: string) => {
//called when error occurs
}
sensorProfile.onPowerChanged = (sensor: SensorProfile, power: number) => {
//callback for get batter level of device, power from 0 - 100, -1 is invalid
}
sensorProfile.onDataCallback = (sensor: SensorProfile, data: SensorData) => {
//called after start data transfer
}Use public async connect(): Promise<boolean> to connect
const success = await sensorProfile.connect();Use public async disconnect(): Promise<boolean> to disconnect
const success = await sensorProfile.disconnect();Use public get connectionState(): DeviceStateEx to get device status
Please send command in 'Ready' state, should be after connect() return true
const deviceStateEx = sensorProfile.connectionState;
# deviceStateEx has define:
export enum DeviceStateEx {
Disconnected,
Connecting,
Connected,
Ready,
Disconnecting,
Invalid,
}Use public get BLEDevice(): BLEDevice to BLE device of SensorProfile
const bleDevice = sensorProfile.BLEDevice;Use public async deviceInfo(): Promise<DeviceInfo | undefined> to get device info of SensorProfile.
Please call after device in 'Ready' state, return undefined if it's not connected
const deviceInfo = await sensorProfile.deviceInfo();
# deviceInfo has defines:
export type DeviceInfo = {
DeviceName: string;
ModelName: string;
HardwareVersion: string;
FirmwareVersion: string;
EmgChannelCount: number;
EegChannelCount: number;
EcgChannelCount: number;
AccChannelCount: number;
GyroChannelCount: number;
BrthChannelCount: number;
MTUSize: number;
};Use public async init(packageSampleCount: number, powerRefreshInterval: number): Promise<boolean>.
Please call after device in 'Ready' state, return true if init succeed
const success = await sensorProfile.init(5, 60*1000);packageSampleCount: set sample counts of SensorData.channelSamples in onDataCallback() powerRefreshInterval: callback period for onPowerChanged()
Use public get hasInited(): boolean to check if init data transfer succeed
const hasInited = sensorProfile.hasInited;Use public async startDataNotification(): Promise<boolean> to start data notification.
Please call if hasInited() return true
const success = await sensorProfile.startDataNotification();Data type list:
export enum DataType {
NTF_ACC = 0x1, //unit is g
NTF_GYRO = 0x2, //unit is degree/s
NTF_EEG = 0x10, //unit is uV
NTF_ECG = 0x11, //unit is uV
NTF_BRTH = 0x15, //unit is uV
}Process data in onDataCallback.
sensorProfile.onDataCallback = (sensor: SensorProfile, data: SensorData) => {
if (data.dataType === DataType.NTF_EEG) {
} else if (data.dataType === DataType.NTF_ECG) {
}
//process data as you wish
data.channelSamples.forEach((oneChannelSamples) => {
oneChannelSamples.forEach((sample) => {
if (sample.isLost) {
//do some logic
} else {
//draw with sample.data & sample.channelIndex
// console.log(sample.channelIndex + ' | ' + sample.sampleIndex + ' | ' + sample.data + ' | ' + sample.impedance);
}
});
});
};Use public async stopDataNotification(): Promise<boolean> to stop data transfer
const success = await sensorProfile.stopDataNotification();Use public get isDataTransfering(): boolean to check if it's data transfering
const isDataTransfering = sensorProfile.isDataTransfering; Use public async batteryPower(): Promise<number> to get battery level. Please call after device in 'Ready' state
const batteryPower = await sensorProfile.batteryPower();
// batteryPower is battery level returned, value ranges from 0 to 100, 0 means out of battery, while 100 means full.Use public async setParam(key: string, value: string): Promise<string> to set parameter of sensor profile. Please call after device in 'Ready' state.
Below is available key and value:
result = await sensorProfile.setParam("FILTER_50HZ", "ON")
// set 50Hz notch filter to ON or OFF, result is "OK" if succeed
result = await sensorProfile.setParam("FILTER_60HZ", "ON")
// set 60Hz notch filter to ON or OFF, result is "OK" if succeed
result = await sensorProfile.setParam("FILTER_HPF", "ON")
// set 0.5Hz hpf filter to ON or OFF, result is "OK" if succeed
result = await sensorProfile.setParam("FILTER_LPF", "ON")
// set 80Hz lpf filter to ON or OFF, result is "OK" if succeed
result = await sensorProfile.setParam("DEBUG_BLE_DATA_PATH", "d:/temp/test.csv")
//# set debug ble data path, result is "OK" if succeed
// please give an absolute path and make sure it is valid and writeable by yourselfFilter Behavor on different devices:
For EEG devices, when power on, default filter is 50Hz notch filter ON , 60Hz notch filter ON, 0.5Hz hpf filter ON and 80Hz lpf filter ON.
For EMG devices, when power on, default filter is 50Hz notch filter ON , 60Hz notch filter ON, 10Hz hpf filter ON and 200Hz lpf filter ON.
For Breath Belt devices, when power on, default filter is 50Hz notch filter ON , 60Hz notch filter ON, 0.5Hz hpf filter OFF and 80Hz lpf filter ON.
Filter setting will be reset to default after reboot.
Please check SimpleTest function in App