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

[Bug]: Example: Cannot connect to device after device.disconnect() #492

Closed
project2021-code opened this issue Aug 11, 2023 · 33 comments
Closed
Labels
bug Something isn't working

Comments

@project2021-code
Copy link

project2021-code commented Aug 11, 2023

FlutterBluePlus Version

1.14.3

Flutter Version

3.10.6

What OS?

Android

OS Version

Android 8

What happened?

In FlutterBluePlus 1.13.4, I can connect to device after disconnection.

If I use the latest 1.14.3, I cannot connect to device after disconnection. It keeps loop the following msg on Android Studio Console.

Logs

D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: setLogLevel
D/[FBP-Android]( 9945): [FBP-Android] initializing BluetoothAdapter
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: startScan
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothLeScanner( 9945): Start Scan with callback
D/BluetoothLeScanner( 9945): onScannerRegistered() - status=0 scannerId=6 mScannerId=0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothLeScanner( 9945): Stop Scan with callback
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: connect
D/BluetoothGatt( 9945): connect() - device: 30:AE:A4:28:2C:CE, auto: false
D/BluetoothAdapter( 9945): isSecureModeEnabled
D/BluetoothGatt( 9945): registerApp()
D/BluetoothGatt( 9945): registerApp() - UUID=031c13c2-375c-43d2-b127-4815ea4ff823
D/BluetoothGatt( 9945): onClientRegistered() - status=0 clientIf=6
D/BluetoothGatt( 9945): onClientConnectionState() - status=0 clientIf=6 device=30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: connected
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: connect
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: requestMtu
D/BluetoothGatt( 9945): configureMTU() - device: 30:AE:A4:28:2C:CE mtu: 185
D/BluetoothGatt( 9945): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=6 latency=0 timeout=500 status=0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager( 9945): getConnectedDevices
D/BluetoothGatt( 9945): onConfigureMTU() - Device=30:AE:A4:28:2C:CE mtu=185 status=0
D/[FBP-Android]( 9945): [FBP-Android] onMtuChanged: mtu: 185 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: discoverServices
D/BluetoothGatt( 9945): discoverServices() - device: 30:AE:A4:28:2C:CE
D/BluetoothGatt( 9945): onSearchComplete() = Device=30:AE:A4:28:2C:CE Status=0
D/[FBP-Android]( 9945): [FBP-Android] onServicesDiscovered: count: 3 status: 0
D/BluetoothGatt( 9945): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=39 latency=0 timeout=500 status=0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: setNotification
D/BluetoothGatt( 9945): setCharacteristicNotification() - uuid: beb5483e-36e1-4688-b7f5-ea07361b26a2 enable: true
D/[FBP-Android]( 9945): [FBP-Android] onDescriptorWrite: uuid: 00002902-0000-1000-8000-00805f9b34fb status: 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: setNotification
D/BluetoothGatt( 9945): setCharacteristicNotification() - uuid: beb5483e-36e1-4688-b7f5-ea07361b26a1 enable: true
D/[FBP-Android]( 9945): [FBP-Android] onDescriptorWrite: uuid: 00002902-0000-1000-8000-00805f9b34fb status: 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: setNotification
D/BluetoothGatt( 9945): setCharacteristicNotification() - uuid: beb5483e-36e1-4688-b7f5-ea07361b26a4 enable: true
D/[FBP-Android]( 9945): [FBP-Android] onDescriptorWrite: uuid: 00002902-0000-1000-8000-00805f9b34fb status: 0
D/[FBP-Android]( 9945): [FBP-Android] onCharacteristicChanged: uuid: beb5483e-36e1-4688-b7f5-ea07361b26a1
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -59 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager( 9945): getConnectedDevices
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -55 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onCharacteristicChanged: uuid: beb5483e-36e1-4688-b7f5-ea07361b26a1
D/[FBP-Android]( 9945): [FBP-Android] onCharacteristicChanged: uuid: beb5483e-36e1-4688-b7f5-ea07361b26a4
D/[FBP-Android]( 9945): [FBP-Android] onCharacteristicChanged: uuid: beb5483e-36e1-4688-b7f5-ea07361b26a4
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -64 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -59 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onCharacteristicChanged: uuid: beb5483e-36e1-4688-b7f5-ea07361b26a1
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -63 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -59 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager( 9945): getConnectedDevices
D/[FBP-Android]( 9945): [FBP-Android] onCharacteristicChanged: uuid: beb5483e-36e1-4688-b7f5-ea07361b26a1
D/ViewRootImpl@cb50ea8[MainActivity]( 9945): ViewPostIme pointer 0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -59 status: 0
D/ViewRootImpl@cb50ea8[MainActivity]( 9945): ViewPostIme pointer 1
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -53 status: 0
D/[FBP-Android]( 9945): [FBP-Android] onCharacteristicChanged: uuid: beb5483e-36e1-4688-b7f5-ea07361b26a1
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt( 9945): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onReadRemoteRssi: rssi: -54 status: 0
D/ViewRootImpl@cb50ea8[MainActivity]( 9945): ViewPostIme pointer 0
D/ViewRootImpl@cb50ea8[MainActivity]( 9945): ViewPostIme pointer 1
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: disconnect
D/BluetoothGatt( 9945): cancelOpen() - device: 30:AE:A4:28:2C:CE
D/BluetoothGatt( 9945): onClientConnectionState() - status=0 clientIf=6 device=30:AE:A4:28:2C:CE
D/[FBP-Android]( 9945): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: disconnected
D/BluetoothGatt( 9945): close()
D/BluetoothGatt( 9945): unregisterApp() - mClientIf=6
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: disconnect
D/ViewRootImpl@cb50ea8[MainActivity]( 9945): ViewPostIme pointer 0
D/ViewRootImpl@cb50ea8[MainActivity]( 9945): ViewPostIme pointer 1
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: startScan
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothLeScanner( 9945): Start Scan with callback
D/BluetoothLeScanner( 9945): onScannerRegistered() - status=0 scannerId=6 mScannerId=0
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothAdapter( 9945): STATE_ON
D/BluetoothLeScanner( 9945): Stop Scan with callback
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager( 9945): getConnectedDevices
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager( 9945): getConnectedDevices
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager( 9945): getConnectedDevices
D/[FBP-Android]( 9945): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager( 9945): getConnectedDevices
@project2021-code project2021-code added the bug Something isn't working label Aug 11, 2023
@chipweinberger
Copy link
Owner

can you post all the logs?

@project2021-code
Copy link
Author

project2021-code commented Aug 11, 2023

Updated the log.

If I Hot Restart the app (Debug Mode), I can connect to device again, and I cannot connect to device after disconnection (same behavior occur).

@joaogbp
Copy link

joaogbp commented Aug 11, 2023

I have this same problem.

@chipweinberger
Copy link
Owner

chipweinberger commented Aug 11, 2023

I just tried to reproduce this (Android 12), and I did not have a problem. Maybe limited to older android?

@chipweinberger chipweinberger changed the title [Bug]: Cannot connect to device after device.disconnect() [Bug]: Example: Cannot connect to device after device.disconnect() Aug 11, 2023
@chipweinberger
Copy link
Owner

chipweinberger commented Aug 11, 2023

I can see in the logs, [FBP-Android] onMethodCall: connect is never called after disconnection.

This could be a UI problem in the example app. Can you please share a video recording of what is happening? Thank you.

Also, after taking a recording, please try updating to 1.14.4

@joaogbp
Copy link

joaogbp commented Aug 11, 2023

I am using flutter_blue_plus: ^1.14.1 and Android 7.0.

That is the error when I try to connect again after disconnecting:

E/flutter ( 8401): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FlutterBluePlusException: connect: (code: 6) Timed out after 5s
E/flutter ( 8401): #0      FutureTimeout.fbpTimeout.<anonymous closure> (package:flutter_blue_plus/src/utils.dart:59:7)
E/flutter ( 8401): #1      _RootZone.run (dart:async/zone.dart:1655:54)
E/flutter ( 8401): #2      Future.timeout.<anonymous closure> (dart:async/future_impl.dart:882:34)

I/art     ( 9753): Do partial code cache collection, code=14KB, data=30KB
I/art     ( 9753): After code cache collection, code=14KB, data=30KB
I/art     ( 9753): Increasing code cache capacity to 128KB

I/BluetoothLeScanner( 9753): startRegisteration: mLeScanClients={com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin$3@3dbc3cc=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@c759e93} ,callback=com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin$3@3dbc3cc
I/BluetoothLeScanner( 9753): startRegisteration: mLeScanClients={} ,callback=com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin$3@3dbc3cc

I also try the 1.14.4 but got the error:

I/flutter (10990): [FBP] <connect> args: {remote_id: 08:3A:F2:43:ED:2A, auto_connect: 0}
I/flutter (10990): [FBP] <connect> result: null
E/flutter (10990): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FlutterBluePlusException: connect: (code: 6) Timed out after 5s
E/flutter (10990): #0      FutureTimeout.fbpTimeout.<anonymous closure> (package:flutter_blue_plus/src/utils.dart:59:7)
E/flutter (10990): #1      _RootZone.run (dart:async/zone.dart:1655:54)
E/flutter (10990): #2      Future.timeout.<anonymous closure> (dart:async/future_impl.dart:882:34)

@chipweinberger
Copy link
Owner

chipweinberger commented Aug 11, 2023

5s timeout is too short.

try 35s

also: i thought i changed the default timeout in 1.14.4, did you set 5s yourself?

@project2021-code
Copy link
Author

project2021-code commented Aug 12, 2023

ScreenRecording_20230812_155646.mp4

If I Hot Reload the App after disconnection, it will show the following log:

Performing hot reload...
Syncing files to device SM C9000...
Reloaded 2 of 1163 libraries in 2,443ms (compile: 324 ms, reload: 775 ms, reassemble: 607 ms).
D/[FBP-Android](18488): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android](18488): [FBP-Android] onMethodCall: startScan
D/BluetoothAdapter(18488): STATE_ON
D/BluetoothAdapter(18488): STATE_ON
D/BluetoothLeScanner(18488): Start Scan with callback
D/BluetoothLeScanner(18488): onScannerRegistered() - status=0 scannerId=6 mScannerId=0
D/[FBP-Android](18488): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(18488): getConnectedDevices
D/[FBP-Android](18488): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(18488): getConnectedDevices
D/[FBP-Android](18488): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter(18488): STATE_ON
D/BluetoothAdapter(18488): STATE_ON
D/BluetoothLeScanner(18488): Stop Scan with callback
D/[FBP-Android](18488): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(18488): getConnectedDevices
D/[FBP-Android](18488): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(18488): getConnectedDevices

Also, it will not recover even I switch the Bluetooth On/Off.

The Console keeps show

D/[FBP-Android](18488): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(18488): getConnectedDevices

@chipweinberger
Copy link
Owner

Thanks for sharing the video :)

It looks like you are making a custom app based on the example app?

Does the stock example app have this problem?

@project2021-code
Copy link
Author

project2021-code commented Aug 12, 2023

I am not sure my app is based on the example app. My app use another project (ancient flutter_blue) as a background.

Do you have any guideline of the example app? It seems that it does not include GATT function.
Update 1
The example app does not have problem.

@project2021-code
Copy link
Author

project2021-code commented Aug 12, 2023

I may find the problem.

In v1.13.4, the await device.disconnect(); cannot be called twice, while in v1.14.x, the await device.disconnect(); can be called twice.
The second device.disconnect() may cause the library fail to connect after disconnection.

In example/main.dart:
Add one to three await device.disconnect(); after Line 404 and
Comment return "$prefix ${e.description}"; in Line 600

In example/pubspec.yaml"
Add version to Line 16 flutter_blue_plus: 1.14.7
Comment # path: ../ in Line 22

Log using v1.13.4

D/[FBP-Android](27605): [FBP-Android] onMethodCall: setLogLevel
D/[FBP-Android](27605): [FBP-Android] initializing BluetoothAdapter
D/[FBP-Android](27605): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter(27605): STATE_ON
D/BluetoothAdapter(27605): STATE_ON
D/BluetoothLeScanner(27605): could not find callback wrapper
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(27605): getConnectedDevices
D/[FBP-Android](27605): [FBP-Android] onMethodCall: startScan
D/BluetoothAdapter(27605): STATE_ON
D/BluetoothAdapter(27605): STATE_ON
D/BluetoothLeScanner(27605): Start Scan with callback
D/BluetoothLeScanner(27605): onScannerRegistered() - status=0 scannerId=6 mScannerId=0
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(27605): getConnectedDevices
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectionState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectionState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: connect
D/BluetoothGatt(27605): connect() - device: 30:AE:A4:28:2C:CE, auto: false
D/BluetoothAdapter(27605): isSecureModeEnabled
D/BluetoothGatt(27605): registerApp()
D/BluetoothGatt(27605): registerApp() - UUID=3901329a-ae7e-4a19-8464-c67a001569b6
D/BluetoothGatt(27605): onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt(27605): onClientConnectionState() - status=0 clientIf=7 device=30:AE:A4:28:2C:CE
D/[FBP-Android](27605): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: connected
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getMtu
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectionState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt(27605): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android](27605): [FBP-Android] onReadRemoteRssi: rssi: -70 status: 0
D/BluetoothGatt(27605): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt(27605): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=39 latency=0 timeout=500 status=0
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectionState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: disconnect
D/BluetoothGatt(27605): cancelOpen() - device: 30:AE:A4:28:2C:CE
D/BluetoothGatt(27605): onClientConnectionState() - status=0 clientIf=7 device=30:AE:A4:28:2C:CE
D/[FBP-Android](27605): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: disconnected
D/BluetoothGatt(27605): close()
D/BluetoothGatt(27605): unregisterApp() - mClientIf=7
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectionState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectionState
D/[FBP-Android](27605): [FBP-Android] onMethodCall: getConnectionState

Log using v1.14.7

D/[FBP-Android](27929): [FBP-Android] onMethodCall: setLogLevel
D/[FBP-Android](27929): [FBP-Android] initializing BluetoothAdapter
D/[FBP-Android](27929): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter(27929): STATE_ON
D/BluetoothAdapter(27929): STATE_ON
D/BluetoothLeScanner(27929): could not find callback wrapper
D/[FBP-Android](27929): [FBP-Android] onMethodCall: getAdapterState
D/libGLESv2(27929): STS_GLApi : DTS, ODTC are not allowed for Package : com.boskokg.flutter_blue_plus_example
D/[FBP-Android](27929): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(27929): getConnectedDevices
D/[FBP-Android](27929): [FBP-Android] onMethodCall: startScan
D/BluetoothAdapter(27929): STATE_ON
D/BluetoothAdapter(27929): STATE_ON
D/BluetoothLeScanner(27929): Start Scan with callback
D/BluetoothLeScanner(27929): onScannerRegistered() - status=0 scannerId=6 mScannerId=0
D/[FBP-Android](27929): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(27929): getConnectedDevices
D/[FBP-Android](27929): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android](27929): [FBP-Android] onMethodCall: connect
D/BluetoothGatt(27929): connect() - device: 30:AE:A4:28:2C:CE, auto: false
D/BluetoothAdapter(27929): isSecureModeEnabled
D/BluetoothGatt(27929): registerApp()
D/BluetoothGatt(27929): registerApp() - UUID=6a2c2caa-7916-4c0f-8f37-e415512e2cba
D/BluetoothGatt(27929): onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt(27929): onClientConnectionState() - status=0 clientIf=7 device=30:AE:A4:28:2C:CE
D/[FBP-Android](27929): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: connected
D/[FBP-Android](27929): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt(27929): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android](27929): [FBP-Android] onReadRemoteRssi: rssi: -63 status: 0
D/BluetoothGatt(27929): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt(27929): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=39 latency=0 timeout=500 status=0
D/[FBP-Android](27929): [FBP-Android] onMethodCall: disconnect
D/BluetoothGatt(27929): cancelOpen() - device: 30:AE:A4:28:2C:CE
D/BluetoothGatt(27929): onClientConnectionState() - status=0 clientIf=7 device=30:AE:A4:28:2C:CE
D/[FBP-Android](27929): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: disconnected
D/BluetoothGatt(27929): close()
D/BluetoothGatt(27929): unregisterApp() - mClientIf=7
D/[FBP-Android](27929): [FBP-Android] onMethodCall: disconnect
D/[FBP-Android](27929): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter(27929): STATE_ON
D/BluetoothAdapter(27929): STATE_ON
D/BluetoothLeScanner(27929): Stop Scan with callback

@chipweinberger
Copy link
Owner

In v1.13.4, the await device.disconnect(); cannot be called twice, while in v1.14.x, the await device.disconnect(); can be called twice.

Can you explain this more? what do you mean "can be called twice"?

@chipweinberger
Copy link
Owner

chipweinberger commented Aug 13, 2023

in 1.14.x, if you call connect on an already connected device connectionState will emit another value, and same for disconnect

in 1.13.x if you call connect on an already connected device, it will just do nothing, and same for disconnect

perhaps this is causing your app to misbehave.

I should probably undo this change.

@project2021-code
Copy link
Author

v1.13.4
https:/boskokg/flutter_blue_plus/assets/124794512/a0c522bc-e8a4-4b77-a930-68efa3a13aa1
If I call disconnect twice, the second disconnect does nothing.
In the log, only one [FBP-Android] onMethodCall: disconnect exists.

v1.14.7
https:/boskokg/flutter_blue_plus/assets/124794512/dcfc0c61-cbe6-4e7f-980b-23d3536c29cb
If I call disconnect twice, the second disconnect will make the CONNECT unavailable.
In the log, there are two [FBP-Android] onMethodCall: disconnect.

@joaogbp
Copy link

joaogbp commented Aug 13, 2023

I changed the timeout but it continues with the problem, also getting another error:

`I/BluetoothLeScanner(23774): startRegisteration: mLeScanClients={com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin$3@2c7e4e0=android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper@c18e399} ,callback=com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin$3@2c7e4e0
I/BluetoothLeScanner(23774): startRegisteration: mLeScanClients={} ,callback=com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin$3@2c7e4e0

I/View (23774): Key down dispatch to io.flutter.embedding.android.FlutterView{c9dce02 VFE...... .F...... 0,0-720,1184 #1}, event = KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=4685789, downTime=4685789, deviceId=-1, source=0x101 }
I/View (23774): Key up dispatch to io.flutter.embedding.android.FlutterView{c9dce02 VFE...... .F...... 0,0-720,1184 #1}, event = KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x248, repeatCount=0, eventTime=4685831, downTime=4685789, deviceId=-1, source=0x101 }

I/art (23774): Do partial code cache collection, code=14KB, data=30KB
I/art (23774): After code cache collection, code=14KB, data=30KB
I/art (23774): Increasing code cache capacity to 128KB

E/flutter (23774): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FlutterBluePlusException: connect: (code: 6) Timed out after 35s
E/flutter (23774): #0 FutureTimeout.fbpTimeout. (package:flutter_blue_plus/src/utils.dart:59:7)
E/flutter (23774): #1 _RootZone.run (dart:async/zone.dart:1655:54)
E/flutter (23774): #2 Future.timeout. (dart:async/future_impl.dart:882:34)`

@chipweinberger
Copy link
Owner

why does connect become unavailable?

are you able to add some more print statements and debug breakpoints to analyze what is happening? where does connect get stuck?

@chipweinberger
Copy link
Owner

@joaogbp please open a new issue.

Are you able to connect using other BLE scanner apps?

@Riyanp
Copy link

Riyanp commented Aug 14, 2023

why does connect become unavailable?

are you able to add some more print statements and debug breakpoints to analyze what is happening? where does connect get stuck?

I'm also having the same issue after updating to v1.14.7

when i tried to re-connect after disconnected, the connect() function does nothing, the bluetoothDevice.connectionState.listen stream suddenly return disconnected state, even when it called multiple times. The only way it can connect again is by hot restart the app.

somehow @project2021-code workaround is working for me, making sure the disconnect doesn't called twice

v1.14.7
https:/boskokg/flutter_blue_plus/assets/124794512/dcfc0c61-cbe6-4e7f-980b-23d3536c29cb
If I call disconnect twice, the second disconnect will make the CONNECT unavailable.
In the log, there are two [FBP-Android] onMethodCall: disconnect.

@joaogbp
Copy link

joaogbp commented Aug 14, 2023

Yes.

@project2021-code
Copy link
Author

project2021-code commented Aug 14, 2023

Not sure if this help

D/[FBP-Android](28051): [FBP-Android] onMethodCall: setLogLevel
D/[FBP-Android](28051): [FBP-Android] initializing BluetoothAdapter
D/[FBP-Android](28051): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothLeScanner(28051): could not find callback wrapper
D/[FBP-Android](28051): [FBP-Android] onMethodCall: getAdapterState
D/libGLESv2(28051): STS_GLApi : DTS, ODTC are not allowed for Package : com.boskokg.flutter_blue_plus_example
D/[FBP-Android](28051): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(28051): getConnectedDevices
D/[FBP-Android](28051): [FBP-Android] onMethodCall: startScan
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothLeScanner(28051): Start Scan with callback
D/BluetoothLeScanner(28051): onScannerRegistered() - status=0 scannerId=6 mScannerId=0
D/[FBP-Android](28051): [FBP-Android] onMethodCall: getConnectedSystemDevices
D/BluetoothManager(28051): getConnectedDevices
D/[FBP-Android](28051): [FBP-Android] onMethodCall: getAdapterState
D/[FBP-Android](28051): [FBP-Android] onMethodCall: connect
D/BluetoothGatt(28051): connect() - device: 30:AE:A4:28:2C:CE, auto: false
D/BluetoothAdapter(28051): isSecureModeEnabled
D/BluetoothGatt(28051): registerApp()
D/BluetoothGatt(28051): registerApp() - UUID=fd77ae4a-72f5-487e-bb21-9a49daf46b5d
D/BluetoothGatt(28051): onClientRegistered() - status=0 clientIf=7
D/BluetoothGatt(28051): onClientConnectionState() - status=0 clientIf=7 device=30:AE:A4:28:2C:CE
D/[FBP-Android](28051): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: connected
D/[FBP-Android](28051): [FBP-Android] onMethodCall: readRssi
D/BluetoothGatt(28051): readRssi() - device: 30:AE:A4:28:2C:CE
D/[FBP-Android](28051): [FBP-Android] onReadRemoteRssi: rssi: -60 status: 0
D/BluetoothGatt(28051): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt(28051): onConnectionUpdated() - Device=30:AE:A4:28:2C:CE interval=39 latency=0 timeout=500 status=0
I/flutter (28051): start 1st disconnect
I/flutter (28051): BluetoothConnectionState.connected
D/[FBP-Android](28051): [FBP-Android] onMethodCall: disconnect
D/BluetoothGatt(28051): cancelOpen() - device: 30:AE:A4:28:2C:CE
D/BluetoothGatt(28051): onClientConnectionState() - status=0 clientIf=7 device=30:AE:A4:28:2C:CE
D/[FBP-Android](28051): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: disconnected
D/BluetoothGatt(28051): close()
D/BluetoothGatt(28051): unregisterApp() - mClientIf=7
I/flutter (28051): start 2nd disconnect
I/flutter (28051): BluetoothConnectionState.connected
D/[FBP-Android](28051): [FBP-Android] onMethodCall: disconnect
D/[FBP-Android](28051): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothAdapter(28051): STATE_ON
D/BluetoothLeScanner(28051): Stop Scan with callback

example/lib/main.dart Line 404

                        print("start 1st disconnect");
                        print(snapshot.data);
                        await device.disconnect();
                        print("start 2nd disconnect");
                        print(snapshot.data);
                        await device.disconnect();
                        print("start 3rd disconnect");
                        print(snapshot.data);
                        await device.disconnect();
                        final snackBar = snackBarGood("Disconnect: Success");
                        snackBarKeyC.currentState?.removeCurrentSnackBar();
                        snackBarKeyC.currentState?.showSnackBar(snackBar);

It seems that it stopped at the second await device.disconnect();.

Update
Please ignore the second print(snapshot.data);, since it is on the same stream data, so this value should be same as the first print(snapshot.data);.

@joaogbp
Copy link

joaogbp commented Aug 14, 2023

Hey @chipweinberger the v1.14.7 now solved my problem. Now I can connect after disconnect without need to rebuild the App.

Thanks for your attention @chipweinberger! That's the best BLE plugin for Flutter!

@joaogbp
Copy link

joaogbp commented Aug 14, 2023

Ops! I just got the error again, now I can connect 2 times (connect and disconnect) but then i can not connect again.

@chipweinberger
Copy link
Owner

chipweinberger commented Aug 14, 2023

@joaogbp , can you try master branch? I am not reproducing the problem on android on master branch. It may be fixed now. Please post logs from master branch too.

dependencies:
  flutter_blue_plus:
    git:
      url: https:/boskokg/flutter_blue_plus
      ref: master

To test, I changed the example app to call connect and disconnect twice in a row like this. But you should test in your app.

424: await.connect(...)
425: await.connect(...)

and

404: await.disconnect();
405: await.disconnect();

@chipweinberger
Copy link
Owner

@joaogbp once you report back i will release 1.14.8

@project2021-code
Copy link
Author

@chipweinberger I can confirm that the problem is solved in the master branch.

I have also tested in my app, and I can connect/disconnect the ESP32.

In the example app log:

D/ViewRootImpl@4afdc60[MainActivity]( 6852): ViewPostIme pointer 0
D/ViewRootImpl@4afdc60[MainActivity]( 6852): ViewPostIme pointer 1
I/flutter ( 6852): BluetoothConnectionState.connected
I/flutter ( 6852): start 1st disconnect
D/[FBP-Android]( 6852): [FBP-Android] onMethodCall: disconnect
D/BluetoothGatt( 6852): cancelOpen() - device: 30:AE:A4:28:2C:CE
D/BluetoothGatt( 6852): onClientConnectionState() - status=0 clientIf=7 device=30:AE:A4:28:2C:CE
D/[FBP-Android]( 6852): [FBP-Android] onConnectionStateChange: status: 0 (SUCCESS) newState: disconnected
D/BluetoothGatt( 6852): close()
D/BluetoothGatt( 6852): unregisterApp() - mClientIf=7
I/flutter ( 6852): start 2nd disconnect
D/[FBP-Android]( 6852): [FBP-Android] onMethodCall: disconnect
D/[FBP-Android]( 6852): [FBP-Android] already disconnected
I/flutter ( 6852): start 3rd disconnect
D/[FBP-Android]( 6852): [FBP-Android] onMethodCall: disconnect
D/[FBP-Android]( 6852): [FBP-Android] already disconnected
D/[FBP-Android]( 6852): [FBP-Android] onMethodCall: stopScan
D/BluetoothAdapter( 6852): STATE_ON
I/chatty  ( 6852): uid=10176(u0_a176) com.boskokg.flutter_blue_plus_example identical 2 lines
D/BluetoothAdapter( 6852): STATE_ON
D/BluetoothLeScanner( 6852): Stop Scan with callback

@joaogbp
Copy link

joaogbp commented Aug 15, 2023

@chipweinberger

It just worked on master branch!! I can now connect and disconnect more than 2 times...

@chipweinberger
Copy link
Owner

chipweinberger commented Aug 15, 2023

1.14.8 is released. Open a new issue if you still have problems.

@IgoSilvari
Copy link

@chipweinberger This error still persists, after disconnecting, it is not possible to connect again, on any other device, and can only be connected after restarting.

Log

D/FBP-Android: [FBP] onMethodCall: connect
D/BluetoothGatt(14060): connect() - device: 48:E7:29:A3:88:62, auto: false, eattSupport: false
D/BluetoothGatt(14060): registerApp()
D/BluetoothGatt(14060): registerApp() - UUID=51686bd4-a6f3-4c43-87f7-ca36eb66f2c9
D/BluetoothGatt(14060): onClientRegistered() - status=0 clientIf=10
D/BluetoothGatt(14060): onClientConnectionState() - status=255 clientIf=10 device=48:E7:29:A3:88:62
D/FBP-Android: [FBP] onConnectionStateChange:disconnected
D/FBP-Android: [FBP] status: UNKNOWN_HCI_ERROR (255)
D/BluetoothGatt(14060): close()
D/BluetoothGatt(14060): unregisterApp() - mClientIf=10
I/flutter (14060): FlutterBluePlusException | connect | android-code: 255 | UNKNOWN_HCI_ERROR (255)
D/FBP-Android: [FBP] onMethodCall: disconnect
D/FBP-Android: [FBP] already disconnected

@chipweinberger
Copy link
Owner

chipweinberger commented Jan 25, 2024

I don't have this problem on my app or my device.

Given the error UNKNOWN_HCI_ERROR, your device (or phone?) is malfunctioning.

There is not much FBP can do to help your situation. You probably need some special tools to figure it out.

@IgoSilvari
Copy link

@chipweinberger I tested on 3 different devices, with different android versions, tested with android 10, 11 and 13

@chipweinberger
Copy link
Owner

try different FBP versions?

but tbh, I don't think this is a problem with FBP

@IgoSilvari
Copy link

IgoSilvari commented Jan 25, 2024

@chipweinberger I tested version 1.31.8, but as I was giving this error, I updated to the latest version 1.31.10, I tested it on 3 devices as I said, but it was from the same brand as Xiaomi, the only difference from the error returned, in the Android versions was that on Android 10, the error code was 133, on Android 13, it was 255, as you can see, in my first message.

@dontaskit28
Copy link

I am experiencing the same issue, is there any update on this?

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

No branches or pull requests

6 participants