【发布时间】:2016-08-08 12:18:31
【问题描述】:
我需要向 Android BLE 设备写入和读取特征值。我会写但不会读。这是我的写作方式:
public void writeCustomCharacteristic(int value) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("<MY SERVICE UUID>"));
if(mCustomService == null){
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("<MY CHARACTERSTIC UUID>"));
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
mWriteCharacteristic.setValue(value,BluetoothGattCharacteristic.FORMAT_UINT8,0);
if(!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)){
Log.w(TAG, "Failed to write characteristic");
}else{
Log.w(TAG, "Success to write characteristic");
}
}
如果我使用这个操作是成功的
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
如果我使用任何其他写入类型而不是WRITE_TYPE_NO_RESPONSE,那么它不会触发onCharacteristicWrite() 回调方法。
这是我阅读特征的方式:
private boolean readCharacteristics(int groupPosition,int childPosition){
try{
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(2).get(2);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(characteristic, true);
}
waitSometime(100);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicIndication(characteristic, true);
}
waitSometime(100);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
byte[] value = characteristic.getValue(); //this is being NULL always
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
mNotifyCharacteristic = null;
}
mBluetoothLeService.writeCharacteristic(characteristic, value);
if(value!=null&&value.length>0) {
Toast.makeText(DeviceControlActivity.this,"success reading data",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(DeviceControlActivity.this,"error reading data",Toast.LENGTH_LONG).show();
}
}
/* if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
mNotifyCharacteristic = characteristic;
//characteristic.setWriteType();
mBluetoothLeService.setCharacteristicIndication(
characteristic, true);
byte[] value=characteristic.getValue();
}*/
return true;
}
}catch (Exception e){
e.printStackTrace();
}
return false;
}
characteristic.getValue() 始终返回 null。我哪里错了?
【问题讨论】:
-
您错误地使用了 bitor 而不是 bitand。另外,不要睡觉,然后期望该值已被读取。而是等待通知读取何时完成的回调。
标签: android bluetooth-lowenergy android-bluetooth gatt