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 scan and read a peripheral, pass its ID to the read method:

// Read Measurements from a BluetoothPeripheral
BluetoothResult readResult = await ValidicBluetooth.read(peripheralID).value;

By default all records collected are automatically submitted to the Validic API. To opt out of this behavior, pass a ReadOptions object with an autoSubmit value of false.

// Read Measurements from a BluetoothPeripheral, don't auto-submit to the Validic API
BluetoothResult readResult = await ValidicBluetooth.read(peripheralID, ReadOptions(autoSubmit: false)).value;

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 a ReadOptions object with an array of allowable device identifiers in allowedDeviceIdentifiers. 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.

// Read Measurements from a BluetoothPeripheral with a specific `deviceIdentifier`
// deviceIdentifier is a MAC address on Android and a UUID on iOS
BluetoothResult readResult = await ValidicBluetooth.read(peripheralID, ReadOptions(allowedDeviceIdentifiers: ['AA:BB:CC:DD:EE:FF'])).value;

Some peripherals require data be provided before an operation is executed.

// Store peripheral data for a specific device
await ValidicBluetooth.savePeripheralStringForDevice(peripheralID, deviceIdentifier, key, data);

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
});