关于连接设备的Android BLE GATT_ERROR(133)

dni*_*vra 12 android bluetooth bluetooth-lowenergy android-bluetooth

我正在尝试使用MAC地址连接到BLE设备.

BluetoothDevice device = bluetoothAdapter.getRemoteDevice(rememberedDeviceAddress)
bluetoothDevice.connectGatt(context, false, bluetoothGattCallback);
Run Code Online (Sandbox Code Playgroud)

我得到一个回调BluetoothGattCallback.onConnectionStateChangestatus = 133newState = 2 甚至当我的BLE装置被关闭.

newState = 2指的BluetoothProfile.STATE_CONNECTED是我连接到设备并且status = 133是GATT_ERROR(而不是status = 0 SUCCESS)

我没有得到注册回调错误的失败.

设备:一加一(Android 4.4)

任何可能导致此问题的指针都会有所帮助.

注意:问题不会发生在所有设备上.在Android 5.0的Nexus 5上,一切似乎都运行良好

请在下面找到堆栈跟踪:

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'
Run Code Online (Sandbox Code Playgroud)

小智 2

某些设备需要在 UI 线程上运行蓝牙 LE 交互。所以我建议尝试这样的事情:

// 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);
}
});
Run Code Online (Sandbox Code Playgroud)

当然,您也可以使用 Activity.runOnUiThread 。来源: https: //stackoverflow.com/a/23478737