简体   繁体   中英

Unable to connect Android to a specific BLE device

I'm maintaining a legacy Android app that retrieves data from several BLE peripherals. Trying to add support for a new peripheral, I find the app won't connect to it. The basic problem seems to be that when it calls .connectGatt() I immediately get an "already connected" error.

I wrote some test code using the RxAndroidBle library. With the autoConnect flag off, the device reports DISCONNECTED , then CONNECTING , then (about 1.5 sec later) a connection error: Disconnected from <device address> . When autoConnect is on, it proceeds smoothly through DISCONNECTED , CONNECTING , and CONNECTED states and operates normally.

An earlier version of my test allowed scanning to continue during the connecting phase. It still worked if autoConnect was on, but it first (falsely) reported "already connected", then a few seconds later fixed itself and connected OK.

The legacy app won't connect regardless of the setting of autoConnect . My best guess so far is that the app isn't stopping scanning soon enough after finding the device (which shouldn't matter, but as we have learned, there are plenty of flaky BLE devices out there--and as noted, the problem only occurs with this device). Is it possible that simply discovering the device causes it to make some kind of half-broken connection that prevents a successful one?

I'm on API 21 so can't try the other forms of .connectGatt() . Nordic's mobile nRF scanner app connects successfully, but I assume that app is full of workarounds for misbehaving devices.

On Android 6.0 and later, the connectGatt method of BluetoothDevice supports a 'transport' parameter which allows forcing a BLE connection. On older versions of Android, this version of the method is accessible via reflection. Using the TRANSPORT_LE option solves connection problems for many dual-mode devices (for me at least - it's not documented what the default TRANSPORT_AUTO option actually does - some Bluetooth stacks may prefer BLE when both modes are available)

nRF Connect should have an option to prefer BLE connections over BR/EDR (or even default to it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM