chipweinberger / flutter_blue_plus

Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android, iOS, macOS
Other
781 stars 471 forks source link

[Help]: Characteristics send duplicate notifications when device is reconnected #559

Closed changewhite closed 1 year ago

changewhite commented 1 year ago

FlutterBluePlus Version

1.15.5

Flutter Version

3.10.6

What OS?

Android

OS Version

Android 13

What happened?

Characteristics are sending duplicate notifications when device is reconnected ad0c4192f19eda57ebf1feda47e4720

Logs

No error log output
chipweinberger commented 1 year ago

logs are needed. Ideally verbose logs.

please reopen when you have logs.

I'm not aware of any code in FBP that would cause this.

chipweinberger commented 1 year ago

you need to stop listening to the stream (i.e. call cancel) when you disconnect

// Setup Listener for characteristic reads
final subscription = characteristic.onValueReceived.listen((value) {
    // do something with new value
});

// listen for disconnection
device.connectionState.listen((BluetoothConnectionState state) async {
    if (state == BluetoothConnectionState.disconnected) {
        // stop listening to characteristic
        subscription.cancel();
    }
});

// enable notifications
await characteristic.setNotifyValue(true);