1

I am trying to read data from a obd2 Bluetooth LE adapter. I am able to connect to the adapter (Carista). I know this because onDescriptorWrite callback is called but I never get a response after sending a command using the write characteristic obtained from adapter's GATT service. I use the following command mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); mWriteCharacteristic.setValue(cmdStr);

mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)

However onCharacteristicRead is never called. Am I doing something wrong? I've tried different variations of characteristics but to no avail. Thanks in advance for your input.

@Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            if (status == BluetoothGatt.GATT_SUCCESS) {

                List<BluetoothGattService> services = gatt.getServices();
                for (BluetoothGattService service : services) {

                    if (service.getUuid().equals(SERVICE_UUID)) {

                        sharedPref.edit().putString(getString(R.string.paired_adapter_address),gatt.getDevice().getAddress()).apply();
                        mBluetoothGatt = gatt;
                        deviceState = DEVICE_STATE.CONNECTING;
                        mWriteCharacteristic = service.getCharacteristics().get(0);
                        mReadCharacteristic = service.getCharacteristics().get(1);

                        setCharacteristicNotification(mReadCharacteristic);
                        scanning = false;

                        Intent intent = new Intent("state");
                        intent.putExtra("message", MainActivity.MESSAGE_CONNECTING);
                        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
                        return;
                    }
                }
            }
            gatt.disconnect();
        }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, true);

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_UUID);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
Ali Feili
  • 61
  • 1
  • 9

1 Answers1

0

On my CARISTA, the first characteristic is the WRITE one, the 2nd is the READ one.

In general: You must probe the characteristics' properties instead of hardcoding them. There is no guarantee that the READ characteristic is always the first one and the WRITE characteristic is always the second one. In fact, it's not always necessary that the adapter exports the serial port over two distinct characteristics – sometimes it's just one.

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67