flutter_reactive_ble icon indicating copy to clipboard operation
flutter_reactive_ble copied to clipboard

Error Disconnecting To Device in iOS

Open yuriboeira11tx opened this issue 3 years ago • 16 comments

Describe the bug The documentation explains that to disconnect from a device, just cancel the subscription with deviceSubscription, for example. However, on iOS, the following logs are being triggered:

Error unsubscribing from notifications: PlatformException(reactive_ble_mobile.Central.(unknown context at $104e330e4).Failure:1, The operation could not be completed.(reactive_ble_mobile.Central.(unknown context at $104e330e4).Failure erro 1.), {}, null)

To Reproduce Steps to reproduce the behavior:

  1. Connect to one using connectToDevice();
  2. Disconnecting to device with await deviceSubscription!.cancel();
  3. Observe a failure with exception:
Error unsubscribing from notifications: PlatformException(reactive_ble_mobile.Central.(unknown context at $104e330e4).Failure:1, The operation could not be completed.(reactive_ble_mobile.Central.(unknown context at $104e330e4).Failure erro 1.), {}, null)

Smartphone / tablet

  • Device: iPhone XS
  • OS: iOS 15.2
  • Package version: 5.0.2

Peripheral device

  • Does it run a custom firmware: yes

yuriboeira11tx avatar Feb 10 '22 13:02 yuriboeira11tx

@yuriboeira11tx does this happen all the time or occasionally?

remonh87 avatar Feb 14 '22 10:02 remonh87

@remonh87 all the time, but only in iOS!

yuriboeira11tx avatar Feb 14 '22 14:02 yuriboeira11tx

I'm experiencing the same issue. Error occurs on every disconnect, only on iOS devices. Using package version 5.0.2.

tomstokes avatar Feb 15 '22 20:02 tomstokes

I am experiencing this as well.

bretmh avatar Feb 16 '22 00:02 bretmh

Did some quick debugging.

On my iOS device, the disconnectFromDevice platform method (here) gets called before the stopNotification platform method (here)

The disconnectFromDevice method ejects the peripheral from activePeripherals, which causes the stopNotification call chain to fail when trying to find the peripheral to turn off notifications.

I've run out of time to debug this for now. Hopefully someone else can run with these clues.

tomstokes avatar Feb 16 '22 17:02 tomstokes

This is a major problem for me too, but only on iOS. I know you guys are busy, but is there any update on this or do you know a workaround?

ozzy1873 avatar Feb 20 '22 13:02 ozzy1873

After this exception occurs, you can reconnect the peripheral but discoverServices and subscribeToCharacteristic fail. I found the workaround is to disconnect/reconnect the peripheral after the failure.

ozzy1873 avatar Mar 12 '22 20:03 ozzy1873

I am experiencing the same issue. Is there any update or workaround to fix this issue?

shobhit14gupta avatar Mar 15 '22 15:03 shobhit14gupta

@shobhit14gupta not yet.

yuriboeira11tx avatar Mar 16 '22 16:03 yuriboeira11tx

I am also experiencing the same issue. Has anyone got fix for this issue?

vaibhiarora03 avatar Mar 17 '22 06:03 vaibhiarora03

Yep, still seeing this as a problem.

hunterdowns avatar Mar 23 '22 15:03 hunterdowns

Still happens to me on iOS 15 and iPadOS 15

mrRedSun avatar May 27 '22 10:05 mrRedSun

I see a lot of pull requests being stuck, if I fix this and make a PR what are the chances this gets merged and released?

mrRedSun avatar Jun 08 '22 08:06 mrRedSun

I have the same Issue. On IOS only as well.

I found something that might be helpfull in another issue : #414 https://github.com/PhilipsHue/flutter_reactive_ble/issues/414#issuecomment-1013830555

The comment says : "_I had the same error. I was able to fix it by canceling the BLE Characteristic Streams first, adding a delay of a few hundred milliseconds then canceling the ConnectionStateUpdate stream.

example

await characteristicStream?.cancel(); await Future.delayed(const Duration(milliseconds: 300)); await deviceConnectionStateStream?.cancel()_; "

I am in the same situation of having subscribed to a characteristic. [EDIT] I tried the fix, and it is WORKING for me ! :D

I though that might help some of you.

GaelleJoubert avatar Jun 15 '22 11:06 GaelleJoubert

Thanks for the sample @GaelleJoubert. I am using the mcumgr-dart library and found that adding a delay between closing the mcumgr client and cancelling the connection solved the problem for me.

BikeBr0 avatar Jun 21 '22 20:06 BikeBr0

Suggestion from @GaelleJoubert works. I had to add delay after canceling characteristic stream before I disconnected from the device.

pawlowskim avatar Jun 13 '23 08:06 pawlowskim