react-native-bluetooth-classic icon indicating copy to clipboard operation
react-native-bluetooth-classic copied to clipboard

Issue reading from OBD II device

Open amireds opened this issue 2 years ago • 8 comments

  • Device: Android

Application Environment

  • React Native version: 0.68.2
  • RN Bluetooth Classic version: ^1.60.0-rc.23

Bug Description Hi! Awesome work done on this package. My goal for using this package is to actually allow my app talk to an OBD II device that uses classic Bluetooth. I was able to connect to the device seamlessly - your tutorials were really helpful.

However, in trying to read data from the device, I keep getting null, I've tried several directions, both reading using onDataReceived and using .read() None seems to be working.

When I tried writing to it, I get a true response. but read keeps giving me null. Please help.

Below is my function snippet

To Reproduce

async function readConnected() {
    setReading(true);
    const connected = await device.isConnected();

    if (connected) {
      try {
        let writeMessage = await device.write("AT SP 0");
        console.log("Write message: ", writeMessage);
        if (writeMessage) {
          //let message = await device.read();
          //console.log("Read message: ", message);
          let subscription = await device.onDataReceived((data) =>
            onReceivedData(data)
          );
          //console.log("Received data: ", message);
          //console.log("Subscription: ", await subscription.remove());
        }
      } catch (error) {
        // Handle error accordingly
        console.log("Error reading message: ", error);
      }
    }
    setReading(false);
  }

Screenshots I added a screenshot just to see what has been done Screenshot

amireds avatar Aug 04 '22 09:08 amireds

Can you post the connection code, specifically with connection options? Generally the issue with reading data using the DelimitedStringDeviceConnectionImpl (which is the standard/default) is that you're using the wrong delimiter and therefore it doesn't think there are any messages to send.

Also link the documentation for the ODB II device if you can.

kenjdavidson avatar Aug 04 '22 14:08 kenjdavidson

Hi, @kenjdavidson Thanks for the response. Below is the code for the connection

async function connectToDevice() {
    setIsConnected(false);
    setConnecting(true);
    try {
      if (device._nativeDevice.bonded === false) {
        console.log("Bonding to device");
        const paired = await RNBluetoothClassic.pairDevice(device.address);
        setDevice(paired);
      }
      console.log("Connecting to device");
      const checkConnected = await device.isConnected();
      if (!checkConnected) {
        const connected = await device.connect({
          CONNECTOR_TYPE: "rfcomm",
          DELIMITER: "\n",
          //DEVICE_CHARSET: "ascii",
          DEVICE_TIMEOUT: 10000,
          SECURE_SOCKET: false,
        });
        console.log("Connected to device", connected);
      }
      setIsConnected(true);
    } catch (e) {
      console.log("Error in connection: ", e);
    }
    setConnecting(false);
  }

Also for the documentation... Would these suffice?

https://www.scantool.net/scantool/downloads/234/stn1100-frpm-preliminary.pdf http://www.obdtester.com/elm-usb-commands

amireds avatar Aug 04 '22 14:08 amireds

A quick look shows that the default is a newline:

By default, responses from the STN11xx are
terminated with a carriage return (0x0D). ATL1
command can be used to have the STN11xx append
line feeds (0x0A) to the carriage returns.

But it does mention a line feed can be added?
Sadly, due to the nature of these things, you're going to have to do the debugging for me with the device. I can guide you, but as I don't have the ability to replicate it, there isn't much more I can do.

Maybe try a quick \n\r and see if something comes back. Otherwise you'll need to debug the DelimitedStringDeviceConnectionImpl method for incoming data.

kenjdavidson avatar Aug 04 '22 14:08 kenjdavidson

Is there a possibility of a possible virtual meet, If you don't mind, we could use google meet?

amireds avatar Aug 04 '22 14:08 amireds

Sure, anything is possible!!

But this week is out, so maybe one evening (EST) next week I could have some time. I just got back from Vacation and I'm going away again this weekend.

kenjdavidson avatar Aug 04 '22 14:08 kenjdavidson

Oh Great! Thanks a lot. Pending that time, I'll appreciate a guide through the debugging. What do you need me to do, let me try it here.

amireds avatar Aug 04 '22 14:08 amireds

React Native will pull all the released source code into the Libraries folder of Android Studio (or the Pods folder of XCode). So there isn't much needed, you just need to open your application in Android Studio (you do this by selecting the /android folder of the project, then open the dependency file within that same session and set a breakpoint.

So I guess the only thing is Android Studio that you'll need.

kenjdavidson avatar Aug 04 '22 14:08 kenjdavidson

Hey sorry, work and fun have been busy this last little while. Have you made any headway with this? I might be able to spend some time in the coming week. I'd prefer if you've been able to get it debuggin in android studio before we worry about a screen share.

Let me know.

kenjdavidson avatar Aug 17 '22 01:08 kenjdavidson

Is this still an issue?

kenjdavidson avatar Sep 08 '22 12:09 kenjdavidson