mit-cml / appinventor-extensions

Source code of extensions published for MIT App Inventor
Apache License 2.0
86 stars 94 forks source link

Asymmetric RegisterForX and UnregisterForValues functions #26

Open sleven79 opened 5 years ago

sleven79 commented 5 years ago

Note that there is no call to "gatt.writeDescriptor()" in the "unsubscribe" function called by the "UnregisterForValues" function of the BluetoothLE extension. This means that while the callback gets disabled and the App Inventor app does not receive any notifications from the BLE device any more, the BLE device still generates notifications in the background. The notification bit in the corresponding CCCD on the BLE device has not been cleared.

This is probably not the intended behavior. As BLE devices often try to save power wherever possible, being able to disable notifications on the device is important.

Note that the call to "gatt.writeDescriptor()" is used as expected in the "subscribe" function called by the "RegisterForX" functions of the BluetoothLE extension. Notifications are enabled by a write request to the corresponding CCCD.

I suggest to review the internals of the "unsubscribe" function and to include a call to "gatt.writeDescriptor()" such as to disable the notification bit in the CCCD.

sleven79 commented 4 years ago

Hmm, unfortunately, the simple change that I proposed in the pull request has not been considered for the latest release of the extension. The bug is still alive.

ewpatton commented 4 years ago

@sleven79 There are a lot of changes in #27 that seem beyond the solution you've proposed here. If you want to clean up that PR to update the descriptor accordingly, that would be fine. We may want to balance the number of calls to RegisterForX(service, characteristic) and UnregisterForValues(service, characteristic) so that we only reset the notification flag once all existing registrations have been unwound.