如何将数据发送到ble?

时间:2014-10-14 08:24:01

标签: android

我使用过mBluetoothGatt.writeDescriptor(描述符)或writeCharacteristic(特征) 将数据发送到ble设备。

但设备无法接收。我不知道为什么?

这是我的代码:

 public void writeDataToBel() {

    if ((gattCharacteristics_send.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
        System.out.println("have permission");
    }
String  send_data = "d300000000060000000000000000d900";
    gattCharacteristics_send.setValue(send_data);

    gattCharacteristics_send
        .setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

    boolean sendStatus = mBluetoothGatt
            .writeCharacteristic(gattCharacteristics_send);

    System.out.println("status-->" + sendStatus);
}

2 个答案:

答案 0 :(得分:0)

确保您要写入的特征具有写入权限。您可以通过以下方式执行检查:

 if((gattCharacteristics_send.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE ) > 0) {
         gattCharacteristics_send.setValue(send_data);
         boolean sendStatus = mBluetoothGatt.writeCharacteristic(gattCharacteristics_send);
 }

答案 1 :(得分:0)

我已经解决了:

 "d300000000060000000000000000d900" chageTo byte[]


public static byte[] changeToByte(String sendData) {
        byte[] myByte = new byte[16];  
        int[] myInt = new int[16];
        for (int i = 0; i < myByte.length; i++) {
            myInt[i]=Integer.valueOf(sendData.substring(i*2, (i+1)*2), 16);
        } 
         for (int i = 0; i < myByte.length; i++) {
          myByte[i] = (byte) myInt[i];
        }
       return myByte;
}