question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scan for device -> connect to found device not working after first time (android)

See original GitHub issue

Describe 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:

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:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
squiocommented, Apr 12, 2021

This looks like an excellent work around, I’ll propose to document this #98 and close this issue?

1reaction
pwespicommented, Apr 10, 2021

FYI in the newest version, BleClient.disconnectdoesn’t fail anymore when the device is not connected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix Bluetooth problems on Android - Google Support
Step 1: Check Bluetooth basics · Turn Bluetooth off and then on again. Learn how to turn Bluetooth on and off. · Confirm...
Read more >
Android Studio doesn't see device - Stack Overflow
I recently had trouble with this, and regardless of what I did(restart adb, edit adb_usb.ini, restart computer+device+swap usb port, reinstall studio etc.
Read more >
Find Bluetooth devices - Android Developers
Using the BluetoothAdapter , you can find remote Bluetooth devices either through device discovery or by querying the list of paired devices. ...
Read more >
How to Fix Bluetooth Pairing Problems - Techlicious
1. Make sure Bluetooth is turned on ... You should see the little Bluetooth symbol at the top right of the screen if...
Read more >
How to fix "Android Studio doesn't see device" in Android ...
If you might have the device currently connected to your PC, then 'allow USB debug mode' option is disabled. Just disconnect the device...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found