discoverServices method
Discover services of the connected device
Discovers the services of the connected device and updates the state with the discovered services. This method also updates the characteristics from the device.
Implementation
Future<void> discoverServices() async {
final device = state is BleDeviceConnecting
? (state as BleDeviceConnecting).device
: state is BleDeviceConnected
? (state as BleDeviceConnected).device
: null;
if (device == null) {
return;
}
emit(BleDeviceGettingServices(device: device));
bool isPaired = false;
if (Platform.isAndroid) {
isPaired = await device.bondState.first == BluetoothBondState.bonded;
} else {
// NOTE: this is a workaround to check if the device is paired for macOS and iOS
final systemDevices = await FlutterBluePlus.systemDevices;
isPaired = systemDevices.contains(device);
}
logger.info("device is ${isPaired ? "" : "not "}paired");
final success =
await _tryFbpOperation(device, "discovering services", () async {
final services = await device.discoverServices();
emit(BleDeviceGettingCharacteristics(
device: device,
services: services,
));
});
// Android-only
// await device.createBond(timeout: 10);
if (success) {
await _updateCharacteristics();
}
}