Polidea / RxBluetoothKit

iOS & OSX Bluetooth library for RxSwift
Apache License 2.0
1.41k stars 370 forks source link

discoverCharacteristics not emitting the anything #287

Open eliekarouz opened 6 years ago

eliekarouz commented 6 years ago

Describe the bug I started characteristics discovery by calling this peripheral.discoverCharacteristics(nil, for: service) but when I subscribe I don't get any event. The bug can't be easily reproduced and you need to disconnect/reconnect for it to happen but here's my investigation: I found out that the filter operator was preventing the emitted event from the delegateWrapper to go through the rest of the chain. The filter operator (line 328 of Peripheral.swift) is checking if remainingCharacteristicsDiscoveryRequest doesn't reach 0 and in my case the value was still > 0 and it actually reached 5. I think that this can happen if you disconnect from the device in the middle of service or characteristic discovery because when subscribe the counter is incremented and when you receive the event it's decremented but when you disconnect before receiving the "didDiscoverCharacteristic" event it stays > 0. Any further attempt to reconnect and discover the characteristics would fail. I think this can happen either with discoverServices or discoverCharacteristics although I was only able to reproduce the issue with the discoverCharacteristics method.

I tried to understand why you need to track the number of requests but didn't get a clear response. I managed to bypass this issue by sending an empty array instead of nil as a first argument just because the code would skip the counter check and sticks only with if the all characteristics are there allowing the event to traverse the filter.

Environment:

Thanks!

bartoszstelmaszuk commented 6 years ago

Hi! Thanks for the feedback.

As far as we can see the solution is to just clear our internal cache on every peripheral disconnection or state change to powered off. We will try to reproduce the problem and implement a fix.

c0diq commented 5 years ago

Probably the same problem as https://github.com/Polidea/RxBluetoothKit/issues/309

c0diq commented 5 years ago

Any update on this issue?