我正在开发一款连接到自行车上的BLE设备的应用程序。该应用程序在Android 4.x上运行良好,但在Android 5上,我们在writeCharacterstic()调用上没有得到任何回调。任何帮助都非常感谢,因为我们现在卡住了!
所有对gatt的调用似乎都是真的。我已确保呼叫顺序正确,因此GATT服务器无法恢复状态:
device.connectGatt()
onConnectionStateChange() -> gatt.discoverServices()
onServicesDiscovered() -> gatt.writeDescriptor()
onDescriptorWrite() -> sendRequest()
sendRequest() -> gatt.writeCharacteristic()
我一直在测试以下设备:
代码:
private BluetoothGattCallback callback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothGatt.STATE_CONNECTED) {
Log.i(TAG, "onConnectionStateChange() - STATE_CONNECTED");
boolean discoverServicesOk = gatt.discoverServices();
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
Log.i(TAG, "onConnectionStateChange() - STATE_DISCONNECTED");
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG, "onServicesDiscovered()");
BluetoothGattService service = gatt.getService(UART_UUID);
characteristic = service.getCharacteristic(TX_UUID);
boolean result = gatt.setCharacteristicNotification(characteristic, true);
Log.i(TAG, "onServicesDiscovered() - setCharacteristicNotification " + result);
if (characteristic.getDescriptor(CLIENT_UUID) != null) {
BluetoothGattDescriptor desc = characteristic.getDescriptor(CLIENT_UUID);
result = desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Log.i(TAG, "onServicesDiscovered() - ENABLE_NOTIFICATION_VALUE " + result);
result = desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
Log.i(TAG, "onServicesDiscovered() - ENABLE_INDICATION_VALUE " + result);
result = gatt.writeDescriptor(desc);
Log.i(TAG, "onServicesDiscovered() - writeDescriptor " + result);
}
connectionState = STATE_CONNECTED;
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
mCurrentRequest = getNextRequest();
Log.i(TAG, "onDescriptorWrite() - sending request, type: " + mCurrentRequest.getType());
sendRequest(mCurrentRequest);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
// DON'T GET HERE ON LOLLIPOP
Log.d(TAG, "onCharacteristicChanged() - size: " + characteristic.getValue().length + ", type: " + mCurrentRequest.getType());
}
}
public void sendRequest(AsciiRequest asciiRequest) {
start = System.currentTimeMillis();
if (characteristic != null && connectionState == STATE_CONNECTED) {
Log.i(TAG, "sendRequest() - sending request, type: " + mCurrentRequest.getType());
characteristic.setValue(asciiRequest.getData());
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
boolean result = gatt.writeCharacteristic(characteristic);
Log.i(TAG, "sendRequest() - result writeCharacteristic: " + result);
} else {
Log.e(TAG, "sendRequest() - not connected");
}
}
日志:
03-31 14:08:28.614: I/BikeActivity(30045): onResume() - not connected yet, trying to connect
03-31 14:08:28.616: I/BikeActivity(30045): doConnect() - connecting to device Bike 2
03-31 14:08:28.634: I/BikeActivity(30045): doConnect() - connected to device Bike 2
03-31 14:08:29.137: I/BikeActivity(30045): onConnectionStateChange() - STATE_CONNECTED
03-31 14:08:29.151: I/BikeActivity(30045): onConnectionStateChange() - connected to device Bike 2
03-31 14:08:29.164: I/BikeActivity(30045): onServicesDiscovered()
03-31 14:08:29.170: I/BikeActivity(30045): onServicesDiscovered() - setCharacteristicNotification true
03-31 14:08:29.170: I/BikeActivity(30045): onServicesDiscovered() - ENABLE_NOTIFICATION_VALUE true
03-31 14:08:29.170: I/BikeActivity(30045): onServicesDiscovered() - ENABLE_INDICATION_VALUE true
03-31 14:08:29.172: I/BikeActivity(30045): onServicesDiscovered() - writeDescriptor true
03-31 14:08:29.173: I/BikeActivity(30045): onDescriptorWrite() - sending request, type: 25
03-31 14:08:29.182: I/BikeActivity(30045): sendRequest() - sending request, type: 25
03-31 14:08:29.186: I/BikeActivity(30045): sendRequest() - result writeCharacteristic: true
03-31 14:08:34.182: I/BikeActivity(30045): doDisconnect() - disconnecting from device Bike 2
03-31 14:08:34.233: I/BikeActivity(30045): onConnectionStateChange() - STATE_DISCONNECTED
03-31 14:08:34.233: I/BikeActivity(30045): onConnectionStateChange() - disconnected
03-31 14:08:34.263: I/BikeActivity(30045): doDisconnect() - toastText: No response from bike.
03-31 14:08:34.318: I/BikeActivity(30045): onPause()
03-31 14:08:34.712: I/BikeActivity(30045): onDestroy()
答案 0 :(得分:0)
我知道这是迟到的回答,但也许这会对其他人有所帮助。 我对Android 5设备有同样的错误 - 请注意,您有断线事件而不是写字符。 尝试添加空回调:
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
{
}
它帮助我使用Android 5设备。