Bluetooth Operations
Flutter ==> Bluetooth module
Pair
Some peripherals require pairing with the end user's phone before they can be read. These BluetoothPeripherals will have a requiresPairing
value of true
and will have PairingInstruction
that can be used to display to the end user.
To pair a peripheral, pass its ID
to the pair
method:
// Pair with a peripheral
BluetoothPeripheral peripheral = await ValidicBluetooth.getPeripheral(peripheralID);
if(peripheral.requiresPairing) {
BluetoothResult result = await ValidicBluetooth.pair(peripheralID).value;
}
Read
To read a peripheral, pass its ID
to the read
method:
// Read Measurements from a BluetoothPeripheral instance
BluetoothResult readResult = await ValidicBluetooth.read(peripheralID, ReadOptions(autoSubmit: false)).value;
Cancel An Operation
Pairing and Reading operations are asynchronous and can be cancelled using the CancelableOperation returned by each method.
Cancel Pair:
// Start a pairing operation
CancelableOperation<BluetoothResult> pairingOperation = ValidicBluetooth.pair(peripheralID);
// Cancel the pairing operation if needed
pairingOperation.cancel();
Cancel Read:
// Start a read operation
CancelableOperation<BluetoothResult> readOperation = ValidicBluetooth.read(peripheralID);
// Cancel the read operation if needed
readOperation.cancel();
Events
Events can be listened for by attaching a listener:
// Reading was attempted with a device in the background but failed
ValidicBluetooth.passiveFailureEvents().listen((event) {
// Handle passive read error event
});
// Readings were collected from a device in the background and automatically submitted to the Validic API.
ValidicBluetooth.passiveReadSuccessEvents().listen((result) {
// Handle passive read success event
});
// A device has been discovered in the background and readings are pending to be collected
ValidicBluetooth.passiveReadyToReadEvents().listen((event) {
// Handle passive ready to read event
});
Updated 1 day ago