0

I am building a React Native Expo App with EAS for visualising OBD2 Data. For this purpose im using an ELM327 OBDII Adapter with Buetooth Low Energy and the react-native-ble-plx library to connect to it. I am able to connect to the Adapter, but i have problems writing Commands and reading the responses.

The following code shows how i am connecting and discovering the services and characteristics of the adapter:

  const connectToDevice = async (device: Device) => {
    try {
      const connectedDevice = await bleManager.connectToDevice(device.id);
      setConnectedDevice(connectedDevice);
      await bleManager.discoverAllServicesAndCharacteristicsForDevice(
        device.id
      );
      const services = await bleManager.servicesForDevice(device.id);
      services.forEach(async (service) => {
        const characteristics = await device.characteristicsForService(
          service.uuid
        );
        // characteristics.forEach(console.log);
      });
      bleManager.stopDeviceScan();
      startStreamingData(device);
      console.log("Connection Successful!");
    } catch (e) {
      console.log("Connection Error", e);
    }
  };

The function startstreamingdata should be the spot, where the magic happens. Here i first try to send a OBDII PID ("010C" for Engine Speed) and read the answer afterwards with the following methods:

 const service = "0000fff0-0000-1000-8000-00805f9b34fb";
  const readAndNotifyChar = "0000fff1-0000-1000-8000-00805f9b34fb";
  const writeChar = "0000fff2-0000-1000-8000-00805f9b34fb";

  const startStreamingData = async (device: Device) => {
    if (device)
      try {
        await device.writeCharacteristicWithoutResponseForService(
          service,
          writeChar,
          "010C\r"
        );
        const currentRPM = await device.readCharacteristicForService(
          service,
          readAndNotifyChar
        );

        if (currentRPM.value) {
          console.log("Response: " + currentRPM.value);
        } else {
          console.log("No Value!");
        }
      } catch (error: any) {
        throw new Error(error);
      }
  };

Sadly my output when running the app and connecting to the adapter is the following: Console Output

It seems like currentRPM.value stays null. What am i doing wrong?

0 Answers0