Chase Roberts
Chase Roberts

Reputation: 9376

Android BluetoothLEGatt BLE not staying connected to device

I am running the sample BluetoothLeGatt app from google and having trouble keeping my device connected. I feel like I am having random behavior a little bit. The bluetooth doesn't usually doesn't connect on the first try (so usually I almost want to say never.)

About 50% of the time when I press the connect button, the app says that the state is Connected. However, the hardware device has an LED that is supposed to light up when it is connected and it doesn't light up. Then 5 or 6 seconds later it goes to disconnected. I suspect that it is connecting for a split second and then disconnecting and the app just doesn't get the memo for a while. Here is the logcat:

D/BluetoothGatt: connect() - device: 54:4A:16:2F:3C:F6, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=60edb0ed-90de-4d7d-b4d0-690bcc359fb7
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
D/BluetoothLeService: Trying to create a new connection.
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=54:4A:16:2F:3C:F6
I/BluetoothLeService: Connected to GATT server.
D/BluetoothGatt: discoverServices() - device: 54:4A:16:2F:3C:F6
I/BluetoothLeService: Attempting to start service discovery:true
D/BluetoothGatt: onGetService() - Device=54:4A:16:2F:3C:F6 UUID=00001800-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetService() - Device=54:4A:16:2F:3C:F6 UUID=00001801-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetService() - Device=54:4A:16:2F:3C:F6 UUID=0000ffe0-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetCharacteristic() - Device=54:4A:16:2F:3C:F6 UUID=00002a00-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetCharacteristic() - Device=54:4A:16:2F:3C:F6 UUID=00002a01-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetCharacteristic() - Device=54:4A:16:2F:3C:F6 UUID=00002a02-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetCharacteristic() - Device=54:4A:16:2F:3C:F6 UUID=00002a03-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetCharacteristic() - Device=54:4A:16:2F:3C:F6 UUID=00002a04-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetCharacteristic() - Device=54:4A:16:2F:3C:F6 UUID=00002a05-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetCharacteristic() - Device=54:4A:16:2F:3C:F6 UUID=0000ffe1-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetDescriptor() - Device=54:4A:16:2F:3C:F6 UUID=00002902-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetDescriptor() - Device=54:4A:16:2F:3C:F6 UUID=00002902-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onGetDescriptor() - Device=54:4A:16:2F:3C:F6 UUID=00002901-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onSearchComplete() = Device=54:4A:16:2F:3C:F6 Status=0
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=54:4A:16:2F:3C:F6
I/BluetoothLeService: Disconnected from GATT server.

Maybe 10% of the time I hit the connect button and don't see anything happen:

D/BluetoothGatt: connect() - device: 54:4A:16:2F:3C:F6, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=7dbe26bc-4cee-48d2-9f47-59af4edb33bb
D/BluetoothGatt: onClientRegistered() - status=133 clientIf=0
I/BluetoothLeService: Disconnected from GATT server.
D/BluetoothLeService: Trying to create a new connection.

and maybe 30% of the time it actually connects.

It's possible that it's a hardware/firmware problem. Although I have no problems when connecting to the device with my iPhone, so I want to conclude that the android code is broke somewhere. I could post all the BLE code but I literally just downloaded the sample so that I knew it was nothing that I broke.

Edit: for those keeping track of my math. The last 10% of the time the app doesn't find anything when it scans, and I have to restart the app.

Edit: I've downloaded and tried 2 other BLE apps from the play store and get the same connection problems as I do as the google sample BLE app. Is there some setting that android's implementation of BLE needs to have that would be different from iOS?

Upvotes: 1

Views: 1729

Answers (1)

Evgeny Erlihman
Evgeny Erlihman

Reputation: 458

Try using: BluetoothDevice.connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport)

with TRANSPORT_LE as transport.

That did the trick for me.

Upvotes: 2

Related Questions