Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no read() data is received #343

Open
Code-Divine opened this issue Dec 15, 2024 · 0 comments
Open

no read() data is received #343

Code-Divine opened this issue Dec 15, 2024 · 0 comments

Comments

@Code-Divine
Copy link

Code-Divine commented Dec 15, 2024

"react-native": "0.76.5",
"react-native-bluetooth-classic": "^1.73.0-rc.12",

Device Arduino bluetooth HC-06

The command is successfully sent and executed but afterwards .available() returns zero and .read() returns nothing. It blocks my device because the device itself keeps the output in the buffer and doesn't accept new commands until is cleared. So I have to use another 3rd party program that reads the output buffer and then I can send commands with this plugin again. I tried both with .read() and .onDataReceived()

Here is a sample of my code

Reading method:

const performRead = async (device) => {
    try {
      console.log("Polling for available messages");
      let available = await device.available();
      console.log('There is data available [${available}], attempting read');

      if (available > 0) {
        for (let i = 0; i < available; i++) {
          console.log(`reading ${i}th time`);
          let data = await device.read();
          console.log(`Read data ${data}`);
          console.log(data);
        }
      } else {
        console.log("No read data available");
      }
    } catch (err) {
      console.log(err);
    }
  };

Main method:

const connectAndSendData = async () => {
    try {
      console.log("connecting");
      let device = await RNBluetoothClassic.connectToDevice(
        selectedDevice.address
      );
      if (await device.isConnected()) {
        console.log("device", device);
        const readInterval = setInterval(() => performRead(device), 5000);
        const write = await device.write(
          createCommand(1)
        );
        await new Promise((resolve) => setTimeout(resolve, 20000));
        console.log("disconecting", disconnect);
        clearInterval(readInterval);
        const disconnect = await device.disconnect();
        console.log("disconnected", disconnect);
      }
    } catch (err) {
      if (await device.isConnected()) {
        const disconnect = await device.disconnect();
        console.log("disconnected in catch", disconnect);
      }
      console.log("error", err.message);
      setError(err.message);
    }
  };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant