question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Help] Cannot read from device, but can write to it

See original GitHub issue

Mobile Device Environment Provide a list of operating systems on which this issue is relevant.

  • Device: Android
  • OS: Android 11

Application Environment Provide information about your development environment:

  • React Native version: v0.63.4
  • RN Bluetooth Classic version: 1.60.0-rc.16

Describe the bug I have a Raspberry Pi 3, on which a bluetooth server (Bluedot btcomm) is running. I can send and receive data perfectly from the Android Serial Bluetooth Terminal application. But with my React Native app, I can only send data. I cannot read data from the device. My code is shown below:

  let status = await RNBluetoothClassic.requestBluetoothEnabled();
  if (status) {
    try {
      let unpaired = await RNBluetoothClassic.startDiscovery();
      let rpi = unpaired.find((o) => o.name === "raspberrypi");
      if (rpi) {
        let conn = await RNBluetoothClassic.getBondedDevices();
        let connStatus = conn.find((o) => o.name === "raspberrypi");
        if (!connStatus) {
          console.log("Pairing....");
          await RNBluetoothClassic.pairDevice(rpi.id);
        }
        try {
          device =  await RNBluetoothClassic.connectToDevice(rpi.id, {});
          await device.connect({});
          await device.write("Foo", "ascii");  // 'Foo' is received in raspberry pi
          console.log(await device.available());  // Logs '0'
          await device.onDataReceived((res) => console.log(res));  // Does not log anything
          let readData = await device.read();
          console.log(readData);  // Logs 'null'
        } catch (e) {
          console.log(e);
        }
      } else {
        console.log("Bluetooth error!");
      }
    } catch (e) {
      console.log(e);
    }
  }

Please let me know if I am doing anything incorrectly.

Thank you.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
deepalisrivastava25commented, Feb 9, 2022

@hemanth-kumarv @kenjdavidson I have implemented this library in my app to establish bluetooth connection with another device.(which is an android phone in my case) However I am unable to connect to another device. In the above code snippet shared by @hemanth-kumarv , device = await RNBluetoothClassic.connectToDevice(rpi.id, {}); await device.connect({});

connectToDevice accepts 2 params which are address and options.

This is the definition of this method in the react-native-bluetooth-classic lib.

connectToDevice(address, options) {
    return __awaiter(this, void 0, void 0, function* () {
        // Comming from the Java world this is nuts - not being able to assign anything to
        // options because it's a <T extends StandardOptions> even with something that matches
        // the StandardOptions interface
        let connected = yield this._nativeModule.connectToDevice(address, options || {});
        return new BluetoothDevice(connected, this);
    });
}

What do we mean by the comment section above.? What should I pass as options in connectToDevice method.

Can you share a proper example of how can we connect to a device?

0reactions
kenjdavidsoncommented, Feb 10, 2022

Not sure what to tell ya man, the error is right there

Error: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

The socket on the other side (ServerSocket) is:

  • closed
  • timeout
  • etc

as I mentioned before, in order to make a connection, you need something waiting on the other side to accept this connection.

  • How are you doing that?
  • You say that accept doesn’t work, but how doesn’t it work? you have provided no errors.

You also need to have an understanding of the language used:

  • Paired = Paired
  • Connected = RFCOMM socket open

You’re not connected (ie. the error above) so you’d never get the device back from isDeviceConnected(). I can tell you that the accept method works just fine, it’s being used in the sample/development application and I’ve used it to communicate between two Android devices.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to write to serial device but able to read from it
I am using select to wait for the IO resource to become available ... But even waiting 20 seconds, the device is still...
Read more >
I can do the WRITE but i can´t do the READ from a SERIAL port
I need to read/write from a pressure control (from GDS instruments) that has a SERIAL port (RS-232). I'm using LABVIEW 7.1, and Windows...
Read more >
How To Fix 'can't read from the source file or disk' Errors - Alphr
It is an easy one to fix though. Right-click the file you are trying to copy and select Properties.
Read more >
Unable to access (read and write) USB flash drive in Windows
Unable to access (read and write) USB flash drive in Windows 10. I have tried multiple solutions to troubleshoot or even just to...
Read more >
How to Fix SD Card Won't Read /SD Card Cannot Read error
It can be a USB port issue. Some laptops have more than one USB port – front and back. Try to access the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found