我正在尝试使用 BluetoothGattCharacteristic 方法 setValue(..) 在香水分配器设备中写入十六进制值 0xFF 。我在回调方法 onCharacteristicWrite() 中获得成功状态代码 0 但设备不执行任何操作,理想情况下它应该发出香味.
下面是我写入特性的示例代码
private void writeCharacteristic(CallbackContext callbackContext, UUID serviceUUID, UUID characteristicUUID, byte[] data, int writeType) {
boolean success = false;
if (gatt == null) {
callbackContext.error("BluetoothGatt is null");
return;
}
BluetoothGattService service = gatt.getService(serviceUUID);
BluetoothGattCharacteristic characteristic = findWritableCharacteristic(service, characteristicUUID, writeType);
if (characteristic == null) {
callbackContext.error("Characteristic " + characteristicUUID + " not found.");
} else {
int data2=0xFF;
characteristic.setValue(data2, BluetoothGattCharacteristic.FORMAT_UINT16, 0);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
writeCallback = callbackContext;
if (gatt.writeCharacteristic(characteristic)) {
success = true;
System.out.println(" writeCharacteristic success");
} else {
writeCallback = null;
callbackContext.error("Write failed");
}
}
Run Code Online (Sandbox Code Playgroud)
请建议在 BluetoothGattCharacteristic 的 setValue() 方法中写入十六进制数据的方法。
谢谢
0xFFin
BluetoothGattCharacteristic.FORMAT_UINT16表示您将发送,FF 00因为您将其设置为发送 16 位无符号数。要仅发送0xFF(我不知道这是否有区别),您必须将格式设置为UINT8.
characteristic.setValue(data2, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6489 次 |
| 最近记录: |