yanshouwang / bluetooth_low_energy

A Flutter plugin for controlling the bluetooth low energy.
https://pub.dev/packages/bluetooth_low_energy
MIT License
50 stars 16 forks source link

BLE disconnect on Android issue #54

Open iBog opened 8 months ago

iBog commented 8 months ago

Trying to connect to BLE "HR monitor" device. App should auto search all devices, found by name, connect, subscribe characteristic to notify every data change. await CentralManager.instance.setCharacteristicNotifyState( characteristic, state: true, ); All this steps working fine, data start flowing.

On app close (hide) - App should auto disconnect from device. On this step I call two actions - 1) await CentralManager.instance.setCharacteristicNotifyState( characteristic, state: false, ); To stop characteristic subscription 2) await CentralManager.instance.disconnect(peripheral); to disconnect from BLE device itself

After application Resume, need to start from the beginning: Search, Connect... but BLE device not listed in search list anymore. As I understand, connection with BLE not stopped, and device still attached. If temporary switch OFF bluetooth on Android, and run Search BLE devices again - "HR monitor" appeared again, because "pairing" was interrupted and device is ready to be searched and connected

Guide me what i did wrong, or package "bluetooth_low_energy" has an issue when App using CharacteristicNotify

iBog commented 8 months ago

small addition, BLE device is not switched off, it always on me and working, flutter app should periodically connect to device and disconnect from it.

yanshouwang commented 8 months ago

Does the connection closed after you called disconnect method manully?

In my opinion, it's the system's duty to disconnect the BLE device when app closed, and our BLE device did disconnected when I close my app.

I found there some cases on the internet that the connection still connected after app closed, but I can't find anything useful... https://stackoverflow.com/questions/73107781/devices-with-android-12-keep-bluetooth-le-connection-even-when-app-is-closed https://github.com/evothings/cordova-ble/issues/136

iBog commented 8 months ago

@yanshouwang according to connectionStateChanged, connectionState become false. Agree with you, looks like system delay on releasing connection with device. Also found solution in quick.blue library: https://github.com/pauldemarco/flutter_blue/issues/317#issuecomment-522134619

github-actions[bot] commented 7 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 6 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 30 days with no activity.