Scan for device -> connect to found device not working after first time (android)
See original GitHub issueDescribe the bug
Scanning for an existing and available BLE device and then connecting works the very first time my app is installed and run but fails always on subsequent runs.
To Reproduce In my code I basically have these steps:
- BleClient.initialize()
- BleClient.requestLEScan() -> scan for one device name and store the found device object
- BleClient.stopLEScan()
- BleClient.connect(deviceId) -> try to connect to the found device
This works the very first time the app is installed and run but fails always on subsequent runs.
I kept the default timeout for connecting (which is 30 seconds I believe) but the connection fails almost immediately. The console shows:
D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=8 device=B6:24:53:6D:8A:4D
Expected behavior The device is discovered (it is) and connected to (it isn’t)
work around I discovered a work-around: to disconnect before trying to connect
- BleClient.initialize()
- BleClient.requestLEScan() -> scan for one device name and store the found device object
- BleClient.stopLEScan()
- BleClient.disconnect(deviceId); -> this fails with error “device not connected”; ignore
- BleClient.connect(deviceId) -> now connect to the found device
This appears to solve the issue completely for my Android device
Plugin version:
- @capacitor-community/bluetooth-le: 1.0.0-3
Desktop (please complete the following information):
- OS: MacOS 11
- Browser N/A
- Version Ionic 6.13.1 / Angular 9.1.14 / Capacitor 3.0.0-rc.0
Smartphone (please complete the following information):
- Device: Nokia 6.1
- OS: Android 10
- Browser System Webview (chrome)
- Version N/A
Additional context Android studio console log output, the relevant part:
V/Capacitor/Plugin: To native (Capacitor plugin): callbackId: 91203091, pluginId: BluetoothLe, methodName: addListener
V/Capacitor: callback: 91203091, pluginId: BluetoothLe, methodName: addListener, methodData: {"eventName":"disconnected|C9:54:C0:6D:30:4C"}
V/Capacitor/Plugin: To native (Capacitor plugin): callbackId: 91203092, pluginId: BluetoothLe, methodName: connect
V/Capacitor: callback: 91203092, pluginId: BluetoothLe, methodName: connect, methodData: {"deviceId":"C9:54:C0:6D:30:4C"}
D/BluetoothGatt: connect() - device: C9:54:C0:6D:30:4C, auto: false
registerApp()
D/BluetoothGatt: registerApp() - UUID=15cda7ec-0be6-47ba-be2a-65c5e240fb58
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/Device: reject: connect Connection timeout.
D/Capacitor: Sending plugin error: {"save":false,"callbackId":"91203092","pluginId":"BluetoothLe","methodName":"connect","success":false,"error":{"message":"Connection timeout."}}
I/Capacitor/Console: File: http://localhost/ ... - Msg: Connect - Connection failed: Error: Connection timeout.
D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=8 device=C9:54:C0:6D:30:4C
V/Capacitor/BluetoothLe: Notifying listeners for event disconnected|C9:54:C0:6D:30:4C
D/Device: Disconnected from GATT server.
W/Device: Resolve callback not registered for key: disconnect
I/Capacitor/Console: File: capacitor-runtime.js - Line 427 - Msg: undefined
I/Capacitor/Console: File: http://localhost/... - Msg: onDisconnect event callback from BLE, device ID: C9:54:C0:6D:30:4C
V/Capacitor/Plugin: To native (Capacitor plugin): callbackId: 91203093, pluginId: BluetoothLe, methodName: stopNotifications
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
This looks like an excellent work around, I’ll propose to document this #98 and close this issue?
FYI in the newest version,
BleClient.disconnect
doesn’t fail anymore when the device is not connected.