我正在使用一些 BLE 设备处理 BLE:首先发现它,然后尝试连接。但是我收到错误:
10-03 17:17:45.641 3854-3930/? E/bt-att﹕ Unsupported transport for background connection
10-03 17:17:45.641 3854-3930/? E/bt-btif﹕ bta_gattc_init_bk_conn failed
并且我无法连接(请注意奇怪的状态
133
在断开连接之前(0
)):
10-03 17:17:45.641 17474-18298/com.icrealtime.allie D/BluetoothGatt﹕ onClientConnectionState() - status=133 clientIf=7 device=08:00:00:3F:B5:F1
完整日志:
10-03 17:17:45.601 1029-1029/? D/STATUSBAR-WifiQuickSettingButton﹕ onWifiSignalChanged enabled=true enabledDesc:"IC"
10-03 17:17:45.611 17474-17474/com.icrealtime.allie D/BleRpcConnectionFactory﹕ Device found: name=Allie-RA222222222, mac_address=08:00:00:3F:B5:F1, other=08:00:00:3F:B5:F1
10-03 17:17:45.621 17474-17474/com.icrealtime.allie D/BleRpcConnectionFactory﹕ Found and accepted BLE device: 08:00:00:3F:B5:F1
10-03 17:17:45.621 17474-17474/com.icrealtime.allie D/BleRpcConnectionFactory﹕ Stopping discovery
10-03 17:17:45.621 17474-17474/com.icrealtime.allie D/BluetoothAdapter﹕ stopLeScan()
10-03 17:17:45.621 3854-4177/? D/BtGatt.GattService﹕ stopScan() - queue size =1
10-03 17:17:45.621 3854-4052/? D/BtGatt.ScanManager﹕ filter size is 1
10-03 17:17:45.621 3854-4052/? D/BtGatt.ScanManager﹕ delete FilterIndex - 4
10-03 17:17:45.621 1029-1029/? D/StatusBar.NetworkController﹕ refreshSignalCluster - setNWBoosterIndicators(false)
10-03 17:17:45.621 3854-3932/? D/bt_vendor﹕ op for 7
10-03 17:17:45.621 3854-3932/? D/bt_upio﹕ BT_WAKE is asserted already
10-03 17:17:45.621 3854-3929/? D/BtGatt.GattService﹕ onScanFilterParamsConfigured() - clientIf=7, status=0, action=1, availableSpace=16
10-03 17:17:45.621 3854-3929/? D/BtGatt.ScanManager﹕ callback done for clientIf - 7 status - 0
10-03 17:17:45.621 3854-3862/? D/BtGatt.GattService﹕ unregisterClient() - clientIf=7
10-03 17:17:45.621 3854-3932/? D/bt_vendor﹕ op for 7
10-03 17:17:45.621 3854-3932/? D/bt_upio﹕ BT_WAKE is asserted already
10-03 17:17:45.621 3854-4052/? D/BtGatt.ScanManager﹕ stop scan
10-03 17:17:45.621 3854-4052/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue=0
10-03 17:17:45.621 3854-3932/? D/bt_vendor﹕ op for 7
10-03 17:17:45.621 3854-3932/? D/bt_upio﹕ BT_WAKE is asserted already
10-03 17:17:45.621 3854-4052/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2
10-03 17:17:45.621 17474-17474/com.icrealtime.allie D/BleRpcConnectionFactory﹕ Connecting to device
10-03 17:17:45.621 3854-4052/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue emtpy, scan stopped
10-03 17:17:45.621 3854-3932/? D/bt_vendor﹕ op for 7
10-03 17:17:45.621 3854-3932/? D/bt_upio﹕ BT_WAKE is asserted already
10-03 17:17:45.631 1029-1029/? D/StatusBar.NetworkController﹕ applyOpen
10-03 17:17:45.631 17474-17474/com.icrealtime.allie D/BluetoothGatt﹕ connect() - device: 08:00:00:3F:B5:F1, auto: true
10-03 17:17:45.631 17474-17474/com.icrealtime.allie D/BluetoothGatt﹕ registerApp()
10-03 17:17:45.631 17474-17474/com.icrealtime.allie D/BluetoothGatt﹕ registerApp() - UUID=0b2a4a78-aaf0-4c95-832a-b94eb4513795
10-03 17:17:45.631 1029-1029/? D/StatusBar.NetworkController﹕ refreshSignalCluster - setNWBoosterIndicators(false)
10-03 17:17:45.631 1029-1029/? D/StatusBar.NetworkController﹕ applyOpen
10-03 17:17:45.631 3854-4178/? D/BtGatt.GattService﹕ registerClient() - UUID=0b2a4a78-aaf0-4c95-832a-b94eb4513795
10-03 17:17:45.641 3854-3929/? D/BtGatt.GattService﹕ onClientRegistered() - UUID=0b2a4a78-aaf0-4c95-832a-b94eb4513795, clientIf=7
10-03 17:17:45.641 17474-17488/com.icrealtime.allie D/BluetoothGatt﹕ onClientRegistered() - status=0 clientIf=7
10-03 17:17:45.641 3854-4177/? D/BtGatt.GattService﹕ clientConnect() - address=08:00:00:3F:B5:F1, isDirect=false set own addr = false own addr type:0
10-03 17:17:45.641 3854-3866/? D/BtGatt.GattService﹕ clientConnect() - address=08:00:00:3F:B5:F1, isDirect=false set own addr = false own addr type:0
10-03 17:17:45.641 3854-3929/? D/BtGatt.btif﹕ btif_get_device_type: Device [08:00:00:3f:b5:f1] type 3, addr. type 0
10-03 17:17:45.641 3854-3930/? E/bt-att﹕ Unsupported transport for background connection
10-03 17:17:45.641 3854-3930/? E/bt-btif﹕ bta_gattc_init_bk_conn failed
10-03 17:17:45.641 1029-1029/? D/StatusBar.NetworkController﹕ refreshSignalCluster - setNWBoosterIndicators(false)
10-03 17:17:45.641 3854-3929/? D/BtGatt.btif﹕ btif_get_device_type: Device [08:00:00:3f:b5:f1] type 3, addr. type 0
10-03 17:17:45.641 3854-3930/? E/bt-btif﹕ bta_gattc_process_api_open Failed, unknown client_if: 0
10-03 17:17:45.641 3854-3929/? D/BtGatt.GattService﹕ onConnected() - clientIf=7, connId=65535, address=08:00:00:3F:B5:F1
10-03 17:17:45.641 1029-1029/? D/StatusBar.NetworkController﹕ applyOpen
10-03 17:17:45.641 17474-18298/com.icrealtime.allie D/BluetoothGatt﹕ onClientConnectionState() - status=133 clientIf=7 device=08:00:00:3F:B5:F1
10-03 17:17:45.641 17474-18298/com.icrealtime.allie D/BleRpcConnectionFactory﹕ onConnectionStateChange()
10-03 17:17:45.641 1029-1029/? D/StatusBar.NetworkController﹕ refreshSignalCluster - setNWBoosterIndicators(false)
10-03 17:17:45.641 1029-1029/? D/StatusBar.NetworkController﹕ applyOpen
10-03 17:17:45.651 17474-20038/com.icrealtime.allie D/BleRpcConnectionFactory﹕ Connection state changed from 133 to 0 for 08:00:00:3F:B5:F1 (08:00:00:3F:B5:F1)
出了什么问题?我确定我从主线程开始连接。
这里有一点经验,如果BLEcentral处于扫描状态,可能需要先停止扫描再连接外设。我也遇到了133错误,https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.1_r13/stack/include/gatt_api.h请在这里查看,GATT_ERROR
记录活动可以帮助调试,
./adb logcat :S bt-att: bt-btm:* BluetoothLEint:* BtGatt.ContextMap:*
始终向“connectGatt()”Android API 指定“TRANSPORT_LE”参数为我修复了 133 错误。
通过扫描找到您的设备后,您必须通过调用 connectGatt() 连接到它。它返回一个 BluetoothGatt 对象,然后您将使用该对象来执行所有 GATT 相关操作,例如读取和写入特征。然而,connectGatt() 方法有 2 个版本! Android 的更高版本添加了更多变体,但由于我们希望兼容 Android 6,因此我们只关注这两个:
BluetoothGatt connectGatt(Context context, boolean autoConnect,
BluetoothGattCallback callback)
BluetoothGatt connectGatt(Context context, boolean autoConnect,
BluetoothGattCallback callback, int transport)
第一个调用的内部实现是,Android 使用传输参数的值 TRANSPORT_AUTO 来调用第二个调用。如果您想通过 BLE 连接,这不是正确的值。 TRANSPORT_AUTO 适用于同时支持 BLE 和经典蓝牙的设备。这意味着您对所建立的连接类型“没有偏好”,并且希望 Android 进行选择。完全不清楚 Android 如何选择,因此这可能会导致相当不可预测的结果,并且许多人报告了问题。这就是为什么您应该始终使用第二个版本并传递 TRANSPORT_LE 作为传输参数:
BluetoothGatt gatt = device.connectGatt(context, false, bluetoothGattCallback, TRANSPORT_LE);
参考: https://medium.com/@martijn.van.welie/making-android-ble-work-part-2-47a3cdaade07