【发布时间】:2015-05-30 12:31:56
【问题描述】:
我一直致力于在 google 的 nexus 6&9 上进行 BLE 读写。
我想在远程设备上写入一个特征,一旦写入就通知远程设备。
以下代码写特征
if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
byte[] test = hexStringToByteArray("3132333435363738393031323334353637383930");
//byte[] value = new byte[1];
//value[0] = (byte) (21 & 0xFF);
Log.d("text",characteristic.getDescriptors().size()+"");
BluetoothGattDescriptor descriptor1 = characteristic.getDescriptor(UUID.fromString(serviceOneCharUuid));
descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
characteristic.setValue(test);
if(mBluetoothGatt.writeDescriptor(descriptor1)){Log.d("text","writedescriptor");}
if(mBluetoothGatt.writeCharacteristic(characteristic)){Log.d("text","setValue");}}
我看到一些帖子告诉我先写描述符,但它不起作用。
我尝试写入 BLE USB 设备,它显示写入功能按预期工作。此外,本地设备上的回调函数也可以正常工作。
但是,远程设备上的 oncharacteristicchanged 函数没有被调用 这是编写在一个名为 BluetoothLeService 的类中的所有回调函数的代码
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status){
Log.d("text",new String(characteristic.getValue()));
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
Log.d("text","on read");
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
Log.d("text", new String(characteristic.getValue()));
}
任何人都知道这些代码中可能有什么问题,或者我错过了什么?
【问题讨论】: