如何编写&x201C;0xFF”;作为android BLE中的特征值?

如何编写&x201C;0xFF”;作为android BLE中的特征值?,android,Android,我正在尝试使用BluetoothGattCharacteristic方法setValue(..)在香水分配器设备中写入十六进制值0xFF。我确实在回调方法onCharacteristicWrite()中获得了成功状态代码0,但设备不执行任何操作,理想情况下它应该发出香水 下面是我编写的示例代码的特点 private void writeCharacteristic(CallbackContext callbackContext, UUID serviceUUID, UUID characteri

我正在尝试使用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");
        }
}
请建议在BluetoothGattCharacteristic的setValue()方法中写入十六进制数据的方法

谢谢

0xFF
in
BluetoothGattCharacteristic.FORMAT_UINT16
表示您将发送
FF 00
,因为您将其设置为发送16位无符号数字。要仅发送
0xFF
(我不知道这是否有区别),您必须将格式设置为
UINT8

characteristic.setValue(data2, BluetoothGattCharacteristic.FORMAT_UINT8, 0);

您可以向Characteristics发送字节数组

使用下面的方法将十六进制数组转换为字节数组


您正在以int形式分配0xFF,如果您想传递0xFF,它将以255内部转换它,然后以字符串形式传递它。@Smit K您找到如何写入它了吗?上述方法以字节为单位给出的值为-1。但是我需要在BLE设备中发送十六进制值0xFF,即255。我在readCharacteristic()方法中没有收到任何响应,因此不确定我是否能够成功写入BLE设备。@SmitK是否要向设备发送255号?是的,我正在尝试发送255号。实际上,我必须将十六进制值0xFF发送到设备,但是characteristic.setValue(..)只支持int、byte[]和string,所以在发送到设备之前,我将十六进制值0xFF转换为255。我尝试使用BluetoothGattCharacteristic.FORMAT_UINT8,但它也不起作用。回调方法readCharacteristic()也没有被调用,@SmitK我想你的问题不是0xff。不幸的是,Ble非常棘手。也许你有错误的特性,试着写错误的值,或者你的设备的协议需要你在上面做些别的事情
public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}
public static String toHex(String arg) 
{
    try 
    {
        return String.format("%01x", new BigInteger(1, arg.getBytes("UTF-8")));
    }
    catch (UnsupportedEncodingException e) 
    {
        e.printStackTrace();
        return "";
    }
}

//set data
characteristic.setValue(hexStringToByteArray(toHex(255+""));