don / cordova-plugin-ble-central

Bluetooth Low Energy (BLE) Central plugin for Apache Cordova (aka PhoneGap)
Apache License 2.0
942 stars 603 forks source link

iOS - startNotification callback not raised after disconnection from the peripheral #577

Closed zabojad closed 2 years ago

zabojad commented 6 years ago

Hi !

I'm having this issue only on iOS with cordova-plugin-ble-central 1.1.9.

I'm connecting to a peripheral and listens to its notifications. Everything works fine.

Now, if I:

Can you please fix it?

zabojad commented 6 years ago

Hi @don ! Any thought on this one?

don commented 6 years ago

@zabojad I can't duplicate this

I'm using this project https://github.com/don/ITP-BluetoothLE/tree/master/phonegap/thermometer

cordova platform add ios
cordova plugin add cordova-plugin-ble-central
cordova plugin add cordova-plugin-dialogs

Run the app, connect to the thermometer peripheral. After a few notifications, pull the power on the peripheral. App gets the disconnect dialog then goes back to scan screen after clicking ok. Turn the peripheral back on. Connect in the app. I see notifications.

Do you see the same behavior if you use LightBlue Explorer?

Can you run a BLE sniffer to see if the peripheral if the 2902 descriptor is being set and the peripheral is sending a notification after the reconnect?

zabojad commented 5 years ago

Hey @don !

I do not reproduce the issue with LightBlue Explorer (nor with cordova-plugin-ble-central on Android).

Can you run a BLE sniffer to see if the peripheral if the 2902 descriptor is being set and the peripheral is sending a notification after the reconnect?

What BLE sniffer do you use?

zabojad commented 5 years ago

I'm trying to debug the problem and for a reason I do not understand, didUpdateValueForCharacteristic is never raised when the problem occurs...

zabojad commented 5 years ago

Can you run a BLE sniffer to see if the peripheral if the 2902 descriptor is being set and the peripheral is sending a notification after the reconnect?

If it works with Android, it should be OK on the peripheral side no?

zabojad commented 5 years ago

For the time being, I've applied a workaround on the app side by detecting if no notification come from the peripheral. If that's the case, I disconnect from the app side, scan/discover and reconnect to the peripheral. Doing that, it works back as it should...

don commented 5 years ago

@zabojad For iOS I like using the Nordic nRF Sniffer with the nRF51/51 SDK to man-in-the-middle the connection. I think that only works if your peripheral is nRF5x based.

That's odd that didUpdateValueForCharacteristic is never called. After you re-connect, are you calling ble.subscribe again? Is it possible to make a very simple app that causes the problem?

zabojad commented 5 years ago

@don

After you re-connect, are you calling ble.subscribe again?

You mean ble.startnotification?

Yes, I call it right in the success callback of the ble.connect method...

don commented 5 years ago

You mean ble.startnotification? yes

don commented 5 years ago

Is this the same problem at #476?

zabojad commented 5 years ago

No unfortunately, I've tried this fix and it did not solve my issue... In my case, I do not call ble.stopNotification...

don commented 5 years ago

@zabojad Can you try the latest commit ac55995dc9e9d19f231c13e6bbfb9ba24b076480 to see it makes a difference? If it's still broken, can you share some code with me that causes the issue?

Also what version of iOS and Xcode are you running?

zabojad commented 5 years ago

@don please let this ticket open, I'm going to vacations and won't be able to work on this until August...

subratpalhar92 commented 3 years ago

make sure you are using write() instead of writeWithoutResponse(), this solved my issue on ios

gopaljatiya commented 2 years ago

Hi , has anyone solved this issue. We are facing the same issue on iOS only, where if we reconnect bluetooth and call startNotification, the subscribe event is never called , i.e. we are not getting any response. It works fine for the first time connection. Any pointers would really help.

peitschie commented 2 years ago

I've noticed that if the startNotification fails during setup on iOS that the error is never passed back to the javascript layer. I'm not sure why reconnecting would impact this or make it more likely to fail however, unless the peripheral is rejecting the request to activate notifications for some reason.

There's an obvious bug to fix at least, so I'll put up a PR for this.

peitschie commented 2 years ago

I've raised a fix at https://github.com/don/cordova-plugin-ble-central/pull/893 that improves the situation for me locally. Would be great to hear if it makes any difference to the cases identified here.

peitschie commented 2 years ago

A fix is available in pre-release in case anyone has time to test it: https://www.npmjs.com/package/cordova-plugin-ble-central/v/1.4.3-alpha.1

tigohenryschultz commented 2 years ago

I tested 1.4.3-alpha.1 on iOS and my startNotification is not returning success scenarios.

The only thing different from this(https://github.com/don/ITP-BluetoothLE/blob/master/phonegap/thermometer/www/js/index.js) code to mine is it calls read after startNotification, which I do not.

Trying to see if calling stopNotification when disconnecting helps iOS as this bug is preventing us from using this plugin atm :(

Tested 1.4.4-alpha.1 and still having the issue

peitschie commented 2 years ago

@tigohenryschultz are you able to move that to a new issue... that sounds unrelated to the described bug here.

tigohenryschultz commented 2 years ago

Yeah, I wasn't sure if it was the same because we were not disconnecting first.