13

I am trying to connect to a BLE device using the MAC address.

BluetoothDevice device = bluetoothAdapter.getRemoteDevice(rememberedDeviceAddress)
bluetoothDevice.connectGatt(context, false, bluetoothGattCallback);

I get a callback in BluetoothGattCallback.onConnectionStateChange with status = 133 and newState = 2 even when my BLE device is switched off.

newState = 2 refers to BluetoothProfile.STATE_CONNECTED which means that i am connected to the device and status = 133 is GATT_ERROR (instead of status = 0 SUCCESS)

I do not get the Failed to register callback error.

Device : One plus one (Android 4.4)

Any pointers on what might be causing this issue would be helpful.

Note: Issue does not happen on all device. Everything seems to be working fine on Nexus 5 with Android 5.0

Please find below the stack trace:

03-06 13:00:11.994: D/BluetoothGatt(26771): registerApp()
03-06 13:00:11.994: D/BluetoothGatt(26771): registerApp() - UUID='uuid comes here'
03-06 13:00:12.004: D/BluetoothGatt(26771): onClientRegistered() - status=0 clientIf=5
03-06 13:00:42.004: D/BluetoothGatt(26771): onClientConnectionState() - status=133 clientIf=5 device='device id comes here'
D.J
  • 1,439
  • 1
  • 12
  • 23
dnivra
  • 749
  • 4
  • 12
  • 30
  • 1
    Have a look at the answer suggested for this question: http://stackoverflow.com/questions/27280402/every-connection-request-is-being-treated-as-direct-connect-request-android-bl – PaulT Jun 03 '15 at 21:33
  • Could you figure this out? I have the same problem. – Borzh May 09 '16 at 19:15

1 Answers1

2

Certain devices require Bluetooth LE interactions to be run on the UI thread. So I would recommend trying something like this:

// Create handler for main thread where mContext is application context
mHandler = new Handler(mContext.getMainLooper());
...
// Connect to BLE device from mHandler
mHandler.post(new Runnable() {
@Override
public void run() {
    mBTGatt = mBTDevice.connectGatt(mContext, false, mGattCallback);
}
});

Of course you could use Activity.runOnUiThread as well. Source: https://stackoverflow.com/a/23478737

Community
  • 1
  • 1
Emeritus
  • 59
  • 6