我遵循Android 4.3中针对蓝牙低功耗开发的指南。
尝试从Android手机连接BLE设备时,有时连接到BLE设备后会立即断开连接。
连接代码是:
public void connect(final String address) {
// TODO Auto-generated method stub
Log.w(TAG, "BluetoothLeService Connect function.");
if(mBluetoothAdapter == null || address == null){
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
int connectState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
}
生成的日志是:
D/BluetoothGatt(13308): onClientConnectionState() - status=0 clientIf=4 device=20:73:20:00:6C:B4
I/BluetoothLeService(13308): BluetoothGattCallback-----newState = 2
I/BluetoothLeService(13308): STATE_CONNECTED:
I/Device_information(13308): BroadcastReceiver---action = ti.android.ble.common.ACTION_GATT_CONNECTED
I/BluetoothLeService(13308): ServiceDiscover
D/BluetoothGatt(13308): discoverServices() - device: 20:73:20:00:6C:B4
D/BtGatt.GattService(12125): discoverServices() - address=20:73:20:00:6C:B4, connId=4
D/BtGatt.btif(12125): btif_gattc_search_service
D/BtGatt.btif(12125): btgattc_handle_event: Event 1006
E/MP-Decision( 2172): Error setting a sleep mode for secondary cores - -38
E/BtGatt.GattService(12125): getService() - Service requested, but not available!
W/bt-l2cap(12125): L2CA_EnableUpdateBleConnParams - unknown BD_ADDR 207320006cb4
E/bt-btm (12125): btm_sec_disconnected - Clearing Pending flag
D/BtGatt.btif(12125): btif_gattc_upstreams_evt: Event 6
D/BtGatt.GattService(12125): onSearchCompleted() - connId=4, status=129
D/BluetoothGatt(13308): onSearchComplete() = Device=20:73:20:00:6C:B4 Status=129
I/BluetoothLeService(13308): onServicesDiscovered@@@@@@-----State = 129 =
W/BluetoothLeService(13308): BluetoothLeService Connect function.
D/BluetoothManager(13308): getConnectionState()
D/BluetoothManager(13308): getConnectedDevices
D/BluetoothAdapterService(1107670808)(12125): Get Bonded Devices being called
D/BluetoothAdapterProperties(12125): getBondedDevices: length=1
D/BtGatt.GattService(12125): getDeviceType() - device=20:73:20:00:6C:B4, type=2
D/BluetoothGatt(13308): connect() - device: 20:73:20:00:6C:B4, auto: true
D/BluetoothGatt(13308): registerApp()
D/BluetoothGatt(13308): registerApp() - UUID=de8cc9a9-98e5-45d7-84f5-709bbd76e680
D/BtGatt.GattService(12125): registerClient() - UUID=de8cc9a9-98e5-45d7-84f5-709bbd76e680
D/BtGatt.btif(12125): btif_gattc_register_app
D/BtGatt.btif(12125): btif_gattc_upstreams_evt: Event 5
D/BtGatt.GattService(12125): onDisconnected() - clientIf=4, connId=4, address=20:73:20:00:6C:B4
D/BluetoothGatt(13308): onClientConnectionState() - status=133 clientIf=4 device=20:73:20:00:6C:B4
I/BluetoothLeService(13308): BluetoothGattCallback-----newState = 0
I/BluetoothLeService(13308): STATE_DISCONNECTED:
似乎断开连接的主要原因是:
E/BtGatt.GattService(12125): getService() - Service requested, but not available!
W/bt-l2cap(12125): L2CA_EnableUpdateBleConnParams - unknown BD_ADDR 207320006cb4
E/bt-btm (12125): btm_sec_disconnected - Clearing Pending flag
有人可以解释为什么会这样吗?
答案 0 :(得分:-4)
首先你应该意识到Android BLE unstable。你的任务怎么样,你的BLE设备似乎无法提供一些被发现的服务。它可能是设备脑损坏的原因。如果在调用该服务后您有稳定的丢失连接,可能根本不应该调用它。