Bluetooth Operations
React Native ==> Bluetooth module
Pair
Some BluetoothPeripheral
profiles require pairing with the device before measurements can be retrieved from the device. These BluetoothPeripheral
s will have RequiresPairing == true
and will have PairingInstruction
that can be used to display to the end user
try{
const peripheral = await ValidicBluetooth.getPeripheral(2); // A&D BP cuff
// do something with peripheral.pairingInstructions
console.log(peripheral.pairingInstructions);
// then pair
const result = await ValidicBluetooth.pair(peripheral.id)
} catch (e) {
console.log(e)
}
Read
To take a reading from a BluetoothPeripheral
device call readPeripheral
try{
const readResult = await ValidicBluetooth.readPeripheral(2)
} catch (e) {
console.log(e)
}
By default, measurements that are collected are automatically submitted to the Validic API. To prevent automatic submission to the server, pass an object with the autoSubmit
option set to false
, and use ValidicCore.submit()
to manually submit the collected records.
try {
const result = await ValidicBluetooth.readPeripheral(2, { autoSubmit: false });
result.measurements.forEach(async (measurement) => {
await ValidicCore.submit(measurement);
});
} catch (e) {
console.log(e);
}
Read With A Filter Applied
When reading from a peripheral, the first bluetooth device that matches the peripheral will be read. To limit the devices that are read from, pass an array of identifiers as the allowedIdentifiers
array. Note that device identifiers are different between Android and iOS, and iOS identifiers are only valid for the iOS device they are received on. If an empty array is passed, no device will be read.
try {
// Pair with the peripheral (e.g., A&D BP cuff with peripheralId 2)
const pairResult = await ValidicBluetooth.pairPeripheral(2);
// For iOS, this will be a UUID string, e.g.:
// const deviceIdentifier = "12345678-1234-1234-1234-1234567890AB";
// For Android, this will be a MAC address string, e.g.:
// const deviceIdentifier = "AA:BB:CC:DD:EE:FF";
const deviceIdentifier = pairResult.deviceIdentifier;
// Read from the paired device, restricting to the allowedIdentifiers
const result = await ValidicBluetooth.readPeripheral(2, {
allowedIdentifiers: [deviceIdentifier], // Only allow this device
});
console.log("Records:", readResult.measurements);
} catch (e) {
console.error("Error:", e);
}
Canceling an operation
To cancel an inflight Promise
after calling ValidicBluetooth.pairPeripheral
or ValidicBluetooth.readPeripheral
call the ValidicBluetooth.cancelOperation
function. If an operation is currently in place, then the Promise
returned will call its reject
function with a cancellation method.
ValidicBluetooth.readPeripheral(1, {autoSubmit: true})
.then(()=>{
console.log("Paired with Peripheral");
})
.catch(e=>{
console.log(e);
});
// ...
ValidicBluetooth.cancelOperation();
Custom global error handler
For more details on handling errors, refer to the Native Android Inform SDK documentation on Custom Global Error Handler.
Special considerations
Most supported Bluetooth devices function similarly and you can follow the instructions in Bluetooth permissions, Bluetooth Operations and Passive Read to work with them.
However, a few of our supported devices have additional requirements and/or considerations that you need to be aware of. Those are covered in the Native Android Inform SDK Bluetooth Behavior documentation.
Updated 13 days ago