Ivan Ivanov
Ivan Ivanov

Reputation: 11

Android BLE, Can't connect to device

I'm making an application which has to talk to a BLE devices on Android.

I am running into issues establishing the connection to the BLE device.

Case 1) Scan: During a scan after a device has been discovered and reported via

BluetoothLEScanCallback.onScanResult(...)

I am attempting the connect via:

result.getDevice().connectGatt(...)

Problem is on some devices I test this (namely HTC One M8 running Android 5.0.1 and HTC Desire 516 running Android 4.3) the connection process seems to fail, i.e. the callback

BluetoothGattCallback.onConnectionStateChange(....)

is simply not called most of the time. Now this is not a consistent behavior, sometimes it is sometimes it is not.

The second problem is that often if the connection is established the

BluetoothGatt.discoverServices()

call fails, i.e. the BluetoothGattCallback.onServicesDiscovered(...) is also not called. I tried retrying the call a couple of times spaced by X number of milliseconds but it seems to consistently fail regardless of the number of attempts or spacing between them.

Case 2 Listening

The pretty much same thing happens when I attempt to simply "listen" for a device, again calling

BluetoothDevice device = BluetoothManager.getAdapter().getRemoteDevice(macAddress);
device.connectGatt(...)

results often in the same situation.

In both cases I am having issue with Android most of the time, I have a Nexus 5 device on which that same code seems to work for the most part (sometimes the BT stack seems to fail to do its job) A bit more disturbing that the iOS application we have has none of these problems.

Has anyone encountered these issues? Any ideas how to address them?

Upvotes: 0

Views: 6579

Answers (1)

Ivan Ivanov
Ivan Ivanov

Reputation: 11

EDIT: We did "solve" this one. Just in case somebody stumbles on such situation, the issue in my case is that ymmv a lot from device to device or even from connection to connection. Sometimes the process happens in a second or so other times it takes 5, in my tests it can climb up to like 15 on the devices I test with. As for why this is I can't tell.

Upvotes: 1

Related Questions