我有一个BLE设备原型。我能够扫描设备,连接到它。我也可以将其密码写为BLE特征。密码是字符串。但是当我试图写其他十六进制的值时,它会让我在不同的时间写入状态4和状态13的不成功错误?同样的事情是使用nRF应用程序。我哪里错了。以下是我的代码。
public boolean writeCharacteristic(BluetoothGatt mBluetoothGatt) {
//check mBluetoothGatt is available
if (mBluetoothGatt == null) {
Log.e("++++", "lost connection");
return false;
}
String SERVICE_STRING = "3fc2d576-0249-11e7-93ae-----------";
UUID SERVICE_UUID = UUID.fromString(SERVICE_STRING);
BluetoothGattService Service = mBluetoothGatt.getService(SERVICE_UUID);
if (Service == null) {
Log.e("++++++", "service not found!");
return false;
}
BluetoothGattCharacteristic charac = Service
.getCharacteristic(UUID.fromString("3fc2d576-0249-11e7-93ae-------------"));
if (charac == null) {
Log.e("+++", "char not found!");
return false;
}
String mValue = "0x01";
byte[] value = mValue.getBytes();
//value[0] = (byte) (0x00);
String pwd = "WWW";
// byte[] value = mValue.getBytes();
charac.setValue(0x01,BluetoothGattCharacteristic.FORMAT_SINT8,0);
boolean status = mBluetoothGatt.writeCharacteristic(charac);
return status;
}
答案 0 :(得分:1)
让我们将charac.setValue(0x01,BluetoothGattCharacteristic.FORMAT_SINT8,0);
更改为charac.setValue(new byte[]{0x01});