Shreya B
Shreya B

Reputation: 326

react-native bluetooth Connection failed: java.io.IOException read failed, socket might closed or timeout, read ret: -1

I have used this library to in my application(https://github.com/kenjdavidson/react-native-bluetooth-classic-apps/tree/main/BluetoothClassicExample) to make a connection to bluetooth devices.

I am getting this issue - Connection failed: java.io.IOException read failed, socket might closed or timeout, read ret: -1 while connecting to a device.

Here is the code code i used for connect() function. (Referred this code from https://kenjdavidson.com/react-native-bluetooth-classic/react-native/rn-bluetooth-device/)

async connect() {
    try {
      let connection = await this.props.device.isConnected();
      console.log("connection is......", connection);
      if (!connection) {

        console.log(this.state.connectionOptions);
        connection = await this.props.device.connect({
           CONNECTOR_TYPE: "rfcomm",
           DELIMITER: "\n",
           DEVICE_CHARSET: Platform.OS === "ios" ? 1536 : "utf-8",
        });

      } else {
        console.log("Connected to ", this.props.device.name)
      }

      this.setState({ connection });
      this.initializeRead();
    } catch (error) {
      console.log(error.message)
    }
  }

Can someone suggest me the solution

Upvotes: 1

Views: 1375

Answers (1)

kendavidson
kendavidson

Reputation: 1460

As discussed on the Github issue, in order to connect to another device, you need to ensure that that device is:

  • Paired
  • Has a BluetoothServerSocket open

When using this library you need to:

Device 1

Set the library/device to accepting mode:

let device = await RNBluetoothClassic.accept();

Device 2

Connect to Device 1, which you were doing correctly:

    connection = await this.props.device.connect({
           CONNECTOR_TYPE: "rfcomm",
           DELIMITER: "\n",
           DEVICE_CHARSET: Platform.OS === "ios" ? 1536 : "utf-8",
        });

But also as we discussed, Headphones and other peripherals that take advantage of the BluetoothProfile(s) are not really supported by this library.

Edit 11-Jun-2021

As we've also discussed the library itself only provides the very basic communication options (RFCOMM) which are provided by:

  • RfcommDeviceAcceptorImpl
  • RfcommDeviceConnectorImpl
  • DelimitedStringDeviceConnectionImpl

but the library was changed to allow uses to override/add any number of their own developed Acceptor, Connector or DeviceConnection implementations.

The limiting factor here is that React Native does not allow you to send Byte arrays back and forth, the best method of sending data is using Strings, which in this case are Base64 encoded (for the most part).

So at a very high level, you would need to write your own verions of:

  • LcappDeviceAcceptorImpl
  • LcappDeviceConnectorImpl

and get those configured in the Package/Module. You'd then have to write your own ObexDeviceConnectionImpl and get that configured in the module. Looking at the specifications for Obex though, it definitely looks like you're also going to run into issues with transferring as Strings to the React Native side.

It looks like Obex (of which I have zero experience) is just a message format used for transferring data. Which means this can be implemented using ANY type of communication channel, it just happens to say Lcapp is the primary, but looking at the WIKI it does say that RFCOMM can be used for "legacy applications".

It looks like an interesting project, and it may be possible with a lot of custom work. But for something so specialized (if it's private) you may want to look at writing your own internal module or just using native java.

Upvotes: 1

Related Questions