2015-10-19 98 views
1

我正试图确定使用蓝牙低功耗与具有BLE功能的设备进行通信的应用程序出了什么问题。令人生气的是,问题只出现在某些设备上,例如来自欧洲的摩托罗拉Moto G3和来自中国的三星S5,但它可以很好地与来自欧洲的三星S5,来自欧洲的HTC One M7和来自中国的MI W3一起使用。由于加密问题,Android蓝牙文件无法写入描述符

当它不工作时,一切都会在成功写入描述符后停止,因为不会调用相应的onDescritorWrite(...)回调函数。据推测,它是与未能设置加密(以下完整日志):

W/bt-btif (18797): btif_gatt_set_encryption_cb() - Encryption failed (1) 

我已经通像确保蓝牙事件常见的东西都贴到处理器在主线程上,现在我需要新思路设法解决这个...

我的日志采集命令:

D/BtGatt.GattService(18797): registerClient() - UUID=9a90904a-84e8-4f30-9b10-de78ac49f236 
D/BtGatt.GattService(18797): onClientRegistered() - UUID=9a90904a-84e8-4f30-9b10-de78ac49f236, clientIf=5 
D/BtGatt.GattService(18797): start scan with filters 
D/BtGatt.ScanManager(18797): handling starting scan 
D/BtGatt.GattService(18797): onScanFilterEnableDisabled() - clientIf=5, status=0, action=1 
D/BtGatt.ScanManager(18797): callback done for clientIf - 5 status - 0 
D/BtGatt.GattService(18797): onScanFilterParamsConfigured() - clientIf=5, status=0, action=0, availableSpace=15 
D/BtGatt.ScanManager(18797): callback done for clientIf - 5 status - 0 
D/BtGatt.ScanManager(18797): configureRegularScanParams() - queue=1 
D/BtGatt.ScanManager(18797): configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648 
E/bt-btm (18797): Device already in IRK list 
D/BtGatt.GattService(18797): stopScan() - queue size =1 
D/BtGatt.GattService(18797): onScanFilterParamsConfigured() - clientIf=5, status=0, action=1, availableSpace=16 
D/BtGatt.ScanManager(18797): callback done for clientIf - 5 status - 0 
D/BtGatt.ScanManager(18797): stop scan 
D/BtGatt.ScanManager(18797): configureRegularScanParams() - queue=0 
D/BtGatt.ScanManager(18797): configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2 
D/BtGatt.ScanManager(18797): configureRegularScanParams() - queue emtpy, scan stopped 
D/BtGatt.GattService(18797): unregisterClient() - clientIf=5 
V/BLEService(21323): - connecting.... 
D/BluetoothGatt(21323): connect() - device: F4:B8:5E:51:A2:87, auto: false 
D/BluetoothGatt(21323): registerApp() 
D/BluetoothGatt(21323): registerApp() - UUID=4b8c32a4-4a74-49ff-a43f-a769c8200ab8 
D/BtGatt.GattService(18797): registerClient() - UUID=4b8c32a4-4a74-49ff-a43f-a769c8200ab8 
D/BtGatt.GattService(18797): onClientRegistered() - UUID=4b8c32a4-4a74-49ff-a43f-a769c8200ab8, clientIf=5 
D/BluetoothGatt(21323): onClientRegistered() - status=0 clientIf=5 
D/BtGatt.GattService(18797): clientConnect() - address=F4:B8:5E:51:A2:87, isDirect=true 
E/bt-btm (18797): Device already in IRK list 
W/bt-btm (18797): btm_acl_created hci_handle=4 link_role=0 transport=2 
W/bt-btif (18797): info:x0 
W/bt-l2cap(18797): L2CA_SetDesireRole() new:x1, disallow_switch:0 
D/BtGatt.GattService(18797): client onConnected() - clientIf=5, connId=5, address=F4:B8:5E:51:A2:87 
D/BluetoothGatt(21323): onClientConnectionState() - status=0 clientIf=5 device=F4:B8:5E:51:A2:87 
V/BLEService(21323): Service have changed with old status: 0 and new status: 2 
V/BLEService(21323): - discovering.... 
D/BluetoothGatt(21323): discoverServices() - device: F4:B8:5E:51:A2:87 
D/BtGatt.GattService(18797): discoverServices() - address=F4:B8:5E:51:A2:87, connId=5 
D/BtGatt.GattService(18797): onSearchCompleted() - connId=5, status=0 
D/BluetoothGatt(21323): onSearchComplete() = Device=F4:B8:5E:51:A2:87 Status=0 
V/BLEService(21323): Service have discovered with status: 0 
V/BLEService(21323): - discovered.... 
D/BluetoothGatt(21323): setCharacteristicNotification() - uuid: f048abf6-3315-4d59-bad7-7e23ac18ee85 enable: true 
D/BtGatt.GattService(18797): registerForNotification() - address=F4:B8:5E:51:A2:87 enable: true 
D/BtGatt.GattService(18797): onRegisterForNotifications() - address=null, status=0, registered=1, charUuid=f048abf6-3315-4d59-bad7-7e23ac18ee85 
I/BTConnectionReceiver(21037): onReceive(context, Intent { act=android.bluetooth.device.action.ACL_CONNECTED flg=0x4000010 cmp=com.google.android.googlequicksearchbox/com.google.android.search.core.service.BluetoothConnectionReceiver (has extras) }, [BluetoothDevice: address=F4:B8:5E:51:A2:87, alias=null, name=BLEDevice, majorDeviceClass=7936, deviceClass=7936] 
I/BluetoothClassifier(21037): Bluetooth Device Name: BLEDevice 
W/bt-btif (18797): btif_gatt_set_encryption_cb() - Encryption failed (1) 
W/bt-smp (18797): io_cap = 4 
W/bt-smp (18797): new io_cap = 4 p_cb->loc_enc_size = 16 
E/bt-smp (18797): LTK ready 
W/bt-smp (18797): smp_send_enc_info 
W/bt-smp (18797): smp_send_id_info 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=2 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=2 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=16 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=16 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=16 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=2 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=2 
E/bt-btif (18797): bta_dm_gatt_disc_result serivce_id len=16 
W/bt-btif (18797): bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0008 
W/bt-btif (18797): bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0008 
W/bt-btif (18797): bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0008 
E/bt-btm (18797): btm_sec_disconnected - Clearing Pending flag 
:从日志

​​

输出

如果有人有一些见解可以帮助我解决这个问题,我将不胜感激。

回答

0

对BLE处理代码的重构揭示了一些错误处理的步骤,这些步骤使实际问题变得模糊。

正确处理每一步都允许调用onDescriptorWrite回调,其日志所指示的状态为BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION

当状态BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION收到您需要注册键事件,以便您可以重试一次的结合是在像这样:

final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 
context.registerReceiver(receiver, filter); 

当接收器(的BroadcastReceiver子类)包含了这样的事情:

@Override 
public void onReceive(Context context, Intent intent) { 
    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
    final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR); 
    final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR); 
    final String action = intent.getAction(); 

    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) && bondState == BluetoothDevice.BOND_BONDED) { 
     // retry 
    } 
} 
+0

您能否详细说明您最终做了什么类型的“重构”来完成这项工作。我遇到了具有4个BLE服务和绑定的设备的相同问题。尝试在一个特性和设备上设置通知断开连接。 onDescriptorWrite永远不会被回调。谢谢。 – Ramin

+0

试图弄清楚Android上的BLE如何工作导致不可维护的代码以及对更干净的代码库进行重构,这些代码揭示了无意或无意中完成的某些事情。 – CodeReaper