连接到OBD设备时,setCharacteristicNotification和writeCharacteristic,onCharacteristicChanges收到3E 00 00 3F通知

时间:2018-09-20 07:03:50

标签: bluetooth-gatt

连接到OBD设备时,请设置CharacteristicNotification和writeCharacteristic。收到有关CharacteristicChanged的通知。 但是通知显示3E 00 00 3F,许多写请求的条件相同。预计将返回有关设备的信息。

代码如下:

public void writeCharacteristic(String commnd, BluetoothGattCharacteristic characteristic, boolean enabled) {
    int commandLength = commnd.length(); // length of the string used for the loop

    byte[] bytes = new byte[commnd.length()];
    for(int i = 0; i < commandLength ; i++){   // while counting characters if less than the length add one
        char character = commnd.charAt(i); // start on the first character
        int ascii = (int) character; //convert the first character
        bytes[i] = (byte) Integer.parseInt(String.valueOf(ascii));
        Log.w(TAG, "byte index:"+i+", ascii:"+ascii+", bytes i:"+bytes[i]);
    }

    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    characteristic.setValue(bytes);//ch);//messageBytes);
    boolean success = mBluetoothGatt.writeCharacteristic(characteristic);
    Log.w("Gatt", "write result:"+success);

    Log.w(TAG, "set characteric notification, enabled?"+enabled);
    setCharacteristicNotificationWithDescriptor(characteristic, true);
}

protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

public boolean setCharacteristicNotificationWithDescriptor(BluetoothGattCharacteristic characteristic, boolean enable) {
    Log.w(TAG, "setCharacteristicNotification(UUID=" + characteristic.getUuid().toString() + ", enable=" + enable + " )");
    mBluetoothGatt.setCharacteristicNotification(characteristic, enable);
    Log.w(TAG, "descriptor:"+CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 });
    Log.w(TAG, "notification value:"+BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE+", false:"+new byte[] { 0x00, 0x00 }.toString());
    boolean status = mBluetoothGatt.writeDescriptor(descriptor); //descriptor write operation successfully started?
    Log.w(TAG, "write descriptor:"+status);
    return status;
}

不确定如何获得正确的答案?我用0D发送了AT命令。谢谢。

0 个答案:

没有答案