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