btleplug icon indicating copy to clipboard operation
btleplug copied to clipboard

Subscribe does not have the same behavior on Linux & WIndows

Open TristonJ opened this issue 2 years ago • 1 comments

Describe the bug Calling Peripheral.subscribe(char) has different behavior depending on if you are using the winrt backend or the bluez backend. On Windows each time you call subscribe a new stream of events is created (potentially resulting in duplicated events from a single characteristic). On linux, you can call subscribe as many times as you please, and each characteristic's events will only appear once in any stream returned from notifications().

This is actually pretty clear to see after looking at the Peripheral.subscribe impl for winrtble.

Expected behavior I think, ideally, Windows would behave the same as Linux, allowing you to call subscribe multiple times for a single characteristic with no effect.

Actual behavior Calling subscribe multiple times on windows will result in duplicated events in single stream returned by a call to notifications.

Additional context None - but thank you for the great library!

TristonJ avatar Jul 27 '23 00:07 TristonJ

In my limited testing, fixing this might be as simple as adding a check to the subscribe function in src/winrtble/ble/characteristic.rs:

pub async fn subscribe(&mut self, on_value_changed: NotifyEventHandler) -> Result<()> {
    if self.notify_token.is_some() {
        log::debug!("Already subscribed!");
        return Ok(())
    }
    ...

If that is a reasonable solution, I'm happy to raise a PR. Thanks!

TristonJ avatar Jul 27 '23 00:07 TristonJ