Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot reobserve a characteristic after canceling a job containing a call to observe #677

Closed
mmeisel opened this issue May 16, 2024 · 2 comments · Fixed by #699
Closed
Labels
bug Something isn't working

Comments

@mmeisel
Copy link
Contributor

mmeisel commented May 16, 2024

I have the same use case as in Q&A answer #592 except that I sometimes need to re-observe the characteristic during the same connection.

Using kable 0.31.1 on Android, I did exactly as the answer suggested, using cancelAndJoin() to cancel a launched job. This does successfully write DISABLE_NOTIFICATION_VALUE to the descriptor. However, when I call observe() on the characteristic again, kable seems to think the characteristic is already being observed, and does not write ENABLE_NOTIFICATION_VALUE.

I've tracked this down (using the debugger) to the fact that didStartObservation is never set to false -- I set a breakpoint at this line, and it is never reached. AFAICT, this is because the cancellation of the job causes the next suspend function, which happens to be BluetoothDeviceAndroidPeripheral.setConfigDescriptor(), to throw a CancellationException on completion, which is propagated back to the previous line in Observation.kt.

@twyatt twyatt added the bug Something isn't working label May 16, 2024
@twyatt
Copy link
Member

twyatt commented May 16, 2024

Thanks for the thorough bug report! I'll try to take a look at it soon. PRs are also welcome.

@mmeisel
Copy link
Contributor Author

mmeisel commented May 16, 2024

Thanks for the quick response @twyatt! I'd be happy to submit a PR if I could figure out the root cause! I was able to work around this issue by using a different un-observation strategy -- takeWhile() { stillObserving } on the returned flow, where stillObserving is a volatile boolean that I just set to false when I want to stop observing. This seems to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants