Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中写入2字节或4字节的BLE特征值?_Android_Bluetooth Lowenergy_Android Bluetooth - Fatal编程技术网

如何在android中写入2字节或4字节的BLE特征值?

如何在android中写入2字节或4字节的BLE特征值?,android,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth Lowenergy,Android Bluetooth,我正在开发一个自动化系统,在这个系统中,我使用安卓手机(低能耗蓝牙)作为遥控器 我可以成功写入1字节(0xFF)特征值。这是我的示例代码 byte[] value= {(byte) 0xFF}; characteristic.setValue(value); mBluetoothGatt.writeCharacteristic(characteristic); 现在的问题是我想写多个字节的特征值,比如: byte[] value= {(byte) 0xFF,(byte) 0xFF,(byte)

我正在开发一个自动化系统,在这个系统中,我使用安卓手机(低能耗蓝牙)作为遥控器

我可以成功写入1字节(0xFF)特征值。这是我的示例代码

byte[] value= {(byte) 0xFF};
characteristic.setValue(value);
mBluetoothGatt.writeCharacteristic(characteristic);
现在的问题是我想写多个字节的特征值,比如:

byte[] value= {(byte) 0xFF,(byte) 0xFF,(byte) 0xFF};
characteristic.setValue(value);
mBluetoothGatt.writeCharacteristic(characteristic);
当我将值更改为2字节或3字节以写入特征值时,则在
onCharacteristicWrite()
回调方法
中,如果(status==BluetoothGatt.GATT\u INVALID\u ATTRIBUTE\u LENGTH)
执行条件语句的一部分。您可以在下面看到我的示例代码。现在,请指导我如何写2字节或3字节的特征值,我将非常感谢您在这方面。谢谢你在安德瓦奇

   public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) 
        {

            if (status == BluetoothGatt.GATT_SUCCESS) 
            {
                broadcastUpdate(ACTION_DATA_WRITE, characteristic);
                Log.e("WRITE SUCCESS", "onCharacteristicWrite() - status: " + status + "  - UUID: " + characteristic.getUuid());
            }
             ...
             ...
             ...
            else if (status == BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH) 
            {
                Log.e("WRITE PROB", "A write operation exceeds the maximum length of the attribute");
            }
        }

我有一些配置蓝牙低能量加密狗的经验,并且这些特性在默认情况下有一个长度定义(据我所知,至少有些实现允许可变长度)。 您试图编写的特征可能定义为一个字节长。
您应该尝试更改遥控器的固件,或者一次只发送一个字节。

您需要从远程设备(您的手机连接的设备)修改GATT服务器


很明显,服务器上的这个特性被定义为1字节长。这就是为什么不能写入多个字节。

当你说“一次只发送一个字节”时,你的意思是你在某处维护有效负载并一次写入一个字节。在成功写入特征的回调中,您只需增加位置,直到所有字节都已写入?@felhr这意味着您在任何其他写入时都将覆盖该字节,因此您最终的特征只包含您发送的最后一个字节。