dotintent / FlutterBleLib

Bluetooth Low Energy library for Flutter with support for simulating peripherals
Apache License 2.0
531 stars 195 forks source link

Notifications not being cancelled #570

Closed katilley closed 3 years ago

katilley commented 3 years ago

For monitoring characteristics the documentation says that "Cancelling the last subscription disables the notifications/indications on this characteristic." Using .cancel() on the stream only cancels the stream notification in the app but the console shows the notification still being registered and a read of the underlying descriptor shows the notification is still set.

  unsubscribeToServices(Peripheral device) async {
    _receivedNewAlertData.cancel(); // _receivedNewAlertData is the characteristic stream
    print('stream cancelled');
    DescriptorWithValue data = await device.readDescriptor(
      "00001811-0000-1000-8000-00805f9b34fb",
      "00002a46-0000-1000-8000-00805f9b34fb",
      "00002902-0000-1000-8000-00805f9b34fb",
    );
    print('CCCD cleared? read = ${data.value}');

I/flutter (22585): CCCD cleared? read = [1, 0]

Is there some other way I'm supposed to cancel notifications?

katilley commented 3 years ago

resolved this. may have been a sequencing issue. Added a .then and it seems to work....


Future<void> unsubscribeToServices(
  List<DeviceManagement> deviceList, int deviceIndex) async {
  print('Unsubscribing');
  await _receivedNewAlertData.cancel().then((valueNotUsed) async {
    deviceList[deviceIndex].subscribed = false;
    //
    // Read CCCD to confirm unsubscribe was successful:
    DescriptorWithValue data = await deviceList[deviceIndex]
        .foundBleAlertDevices
        .readDescriptor(
          "00001811-0000-1000-8000-00805f9b34fb",
          "00002a46-0000-1000-8000-00805f9b34fb",
          "00002902-0000-1000-8000-00805f9b34fb",
        )
        .then((data) {
      print('CCCD cleared? read = ${data.value}');
      return data;
    });
  });