PhilipsHue / flutter_reactive_ble

Flutter library that handles BLE operations for multiple devices.
https://developers.meethue.com/
Other
663 stars 326 forks source link

discoverServices throws error on iOS (only) -> PlatformException(reactive_ble_mobile.Central.(unknown context at $101e5b3c0).Failure:1 #724

Open keehoo opened 1 year ago

keehoo commented 1 year ago

DiscoverServices functionality throws exception on iOS, works well on android.

Steps to reproduce the behavior:

  1. Scan for devices

  2. call await flutterReactiveBle.discoverServices(device.id).then((value) { print(value); }).onError((error, stackTrace) { print(error); });

  3. Observe a failure :

  4. [ +77 ms] flutter: PlatformException(reactive_ble_mobile.Central.(unknown context at $101e5b3c0).Failure:1, The operation couldn’t be completed. (reactive_ble_mobile.Central.(unknown context at $101e5b3c0).Failure error 1.), {}, null)

Expected to recieve a list of DiscoveredService

Works on Android (Android 10)



// ANDROID
[  +29 ms] I/flutter ( 9929): [DiscoveredService(serviceId: 00001801-0000-1000-8000-00805f9b34fb, characteristicIds: [00002a05-0000-1000-8000-00805f9b34fb], characteristics:
[DiscoveredCharacteristic(characteristicId: 00002a05-0000-1000-8000-00805f9b34fb, serviceId: 00001801-0000-1000-8000-00805f9b34fb)], includedServices: []), DiscoveredService(serviceId:
00001800-0000-1000-8000-00805f9b34fb, characteristicIds: [00002a00-0000-1000-8000-00805f9b34fb, 00002a01-0000-1000-8000-00805f9b34fb, 00002a04-0000-1000-8000-00805f9b34fb], characteristics:
[DiscoveredCharacteristic(characteristicId: 00002a00-0000-1000-8000-00805f9b34fb, serviceId: 00001800-0000-1000-8000-00805f9b34fb), DiscoveredCharacteristic(characteristicId:
00002a01-0000-1000-8000-00805f9b34fb, serviceId: 00001800-0000-1000-8000-00805f9b34fb), DiscoveredCharacteristic(characteristicId: 00002a04-0000-1000-8000-00805f9b34fb, serviceId:
00001800-0000-1000-8000-00805f9b34fb)], includedServices: []), DiscoveredService(serviceId: 912ffff0-3d4b-11e3-a760-0002a5d5c51b, characteristicIds:
[  +61 ms] D/BluetoothGatt( 9929): onConnectionUpdated() - Device=00:22:36:11:BC:10 interval=36 latency=0 timeout=500 status=0
[+7664 ms] Application finished.

// IOS
[  +77 ms] flutter: PlatformException(reactive_ble_mobile.Central.(unknown context at $101e5b3c0).Failure:1, The operation couldn’t be completed. (reactive_ble_mobile.Central.(unknown context at
$101e5b3c0).Failure error 1.), {}, null)```

- device that works with other BLE libraries.

I'm trying to rewrite my app using this library.
DaemonChoejur commented 1 year ago

This issue is sort of related to #385 Please add a short delay before calling the discoverServices. Hope that helps.

keehoo commented 1 year ago

@DaemonChoejur - thanks for the reply. Unfortunately that didn't help. image

At some point gave it 10 seconds.

 print("Discovering services");
    await Future.delayed(const Duration(seconds: 10));
    await flutterReactiveBle.discoverServices(device.id).then((value) {
      print(value);
    }).onError((error, stackTrace) {
      print(error);
    });

Ooops, I just saw I had await and then in the same statement... Let me double check.

Nope. Still the same result, with only await and with only then.

ozzy1873 commented 1 year ago

I saw this problem when trying to discover services before connecting to the device.

chipweinberger commented 1 year ago

I'm hitting the same issue, but I have connected already:

iOS: 16.4.1

  Future<void> connect() async {
    _connection = _ble.connectToDevice(
      id: _device.id,
      connectionTimeout: const Duration(seconds: 10),
    );
    await _connection!.where((event) => event.connectionState == DeviceConnectionState.connected).first;
    final services = await _ble.discoverServices(_device.id.toString());
...
}
chipweinberger commented 1 year ago

So something interesting. I added a 3 second delay after connection, as suggestion.

It did not fix the problem, because the connection is immediately being disconnected after connection, before discoverServices is called. I'm not sure why, but my BLE peripheral makes a chime noise on disconnection so I know this is happening.

I also see this in the logs: Warning! No event channel set up to report a connection update