When connect to an OBD device, setCharacteristicNotification and writeCharacteristic, onCharacteristicChanges received 3E 00 00 3F notification

Viewed 18

When connected to an OBD device, setCharacteristicNotification and writeCharacteristic. Notification was received onCharacteristicChanged. But the notification showed 3E 00 00 3F, same conditions for many write requests. It is expected to return the information about the device.

The codes are as following:

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;
}

Not sure how to get the correct response? I sent the AT command with 0D. Thank you.

0 Answers
Related