Giter VIP home page Giter VIP logo

Comments (10)

kenjdavidson avatar kenjdavidson commented on June 27, 2024

Hey, I have (obviously) no problem helping out, but this IOException is a pretty common problem, but sadly it generally has nothing to do with the library and just to do with:

  • Issues or configuration with the Peripheral
  • The Device not being paired or on
  • The Device requiring Insecure/Secure, the default being secure

You'll need to provide way more information on:

  • The phone you're using
  • The device you're connecting to
  • Just more information in general

You say you're using the exactly the same code as my sample app, but are you using the sample app? Or did you copy and paste it to your own app?

You're troubleshooting and issue opening needs some work. Feel free to check back with more.

from react-native-bluetooth-classic-apps.

software3daerospace avatar software3daerospace commented on June 27, 2024

Hello Ken,

First of all I thank you for the quick reply. I also apologize for giving half information in the question.

Here is the detailed information

  • Source phone - Galaxy A50
  • Destination phone - Galaxy A50 (Another phone)
  • Both devices gets connected via bluetooth via normal android connection (Settings -> Connections -> Bluetooth )
  • Lastly I have all the devices listed when I trigger RNBluetoothClassic.requestBluetoothEnabled() in the application for which I am attaching image below.
    WhatsApp Image 2021-06-09 at 14 56 22 (1)

Error Image
WhatsApp Image 2021-06-09 at 14 56 22

I have copied the code from this sample app and integrated into my application as a component. Will it be any issue in my connectionOption object?

        {
           CONNECTOR_TYPE: "rfcomm",
           DELIMITER: "\n",
           DEVICE_CHARSET: Platform.OS === "ios" ? 1536 : "utf-8",
        }

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

Ok, so you're connecting from PHONE to PHONE. You need to ensure that:

  • The ACCEPTING phone is placed in accept mode:
let connection = RNBluetoothModule.accept();
  • And then CONNECT from the other phone, which you look like you're doing.

Since at no point in this do you mention that you're putting the other phone into accept mode, and that's one of the biggest issues posted when attempting to connect between devices, that it's your issue.

Socket programming 101:

  • 1 connect
  • 1 accept

All the time.

from react-native-bluetooth-classic-apps.

software3daerospace avatar software3daerospace commented on June 27, 2024

Hello Ken,

I also have accept connection code (it was commented as i thought it wasnt important. Thank you for telling its importance). But among so many list of devices how does it get to know which device it should target . As we arent giving any specific device details as parameter inside RNBluetoothModule.accept()?

I have a button Accept connection now, and when i click it below function - acceptConnections() function gets triggered but does nothing.

acceptConnections = async () => {
    this.setState({ accepting: true });
    try {
      let device = await RNBluetoothClassic.accept({ delimiter: "\r" });
      if (device) {
        this.props.selectDevice(device);
      }
    } catch (error) {
      // If we're not in an accepting state, then chances are we actually
      // requested the cancellation.  This could be managed on the native
      // side but for now this gives more options.
      if (!this.state.accepting) {
        Toast.show({
          text: "Attempt to accept connection failed.",
          duration: 5000,
        });
      }
    } finally {
      this.setState({ accepting: false });
    }
  };

WhatsApp Image 2021-06-09 at 15 51 23

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

The process you want to follow is:

  1. On the first device click Accept Connections this will start this device listening BluetoothServerSocket

  2. On the second device you click the first device, this will do the device.connect() and attempt to connect to the first device.

  3. The first device will ACCEPT the connection and pass the DeviceConnection to the first device app

  4. The second device will successfully connect and pass the DeviceConnection to the second device app

At this point, you're two devices have connected to each other and should be able to communicate. This is just like all other socket programming:

  • FtpClient connects to FtpServer
  • HttpClient connects to HttpServer
  • Etc.

from react-native-bluetooth-classic-apps.

software3daerospace avatar software3daerospace commented on June 27, 2024

Hey Ken,

This is getting quite interesting now. The steps you gave above absolutely works fine.

But I need the same functionality the other way round.

  1. On first device - select a device you want to pair
  2. On second device - will get a prompt to accept request from first device
  3. Second device will accept the connection
  4. Thus both device gets connected.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

There is no concept of "Prompting" in the sample application, you'll need to do that all on your own:

  • Either within the APP on React side
  • Or you'll need to write custom ACCEPTOR/CONNECTOR classes with your logic

But the functionality is reversed:

  • Accept mode on device 2 - BluetoothServerSocket must always be opened first
  • Connect on device 1

At this point, both devices will receive their respective DeviceConnection(s) and at that point you can determine what rules you want to use to figure out your own rules.

from react-native-bluetooth-classic-apps.

software3daerospace avatar software3daerospace commented on June 27, 2024

Hey Ken,

Thank you very much for this information, it was helpful to try out new method.
As you told Accept mode on device 2 - BluetoothServerSocket must always be opened first, i thought, bluetooth headphones usually have the BluetoothServerSocket always open. I tried 2 scenarios with this headphones.

Scenario 1 using android - (device 1) Settings -> Bluetooth -> On . It now shows the headphone option. When this device is clicked, It first shows a prompt to pair with device . I click 'pair' . Thus it gets automatically 'paired' and gets 'connected' immediately.

Scenario 2 using my app - (device 1) Bluetooth icon inside app (triggers the react-native-bluetooth-classic library) -> allow to switch on bluetooth -> I get list of discovered devices. When i click on the device 2 (i.e headphones) , it raises the same prompt to pair with device. I click 'pair'. Gets paired but doesn't connect. I have Connection Failed: java.io.IOException: read failed, socket might closed or timeout with -1 error.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

You are correct, Bluetooth peripherals when connected will always have a listening socket open waiting for connection. Sometimes they:

  • Don't allow others to connect when one is established
  • Kick off the original when a new connection comes in

It's really up to the peripheral developer to determine what and how they want to listen for incoming connections. Headphones are also a little different in that they use Profiles (I think to manage RFCOMM connections) but I'm not entirely sure, I haven't played around with headphones that much.

This library doesn't support any of the Profiles that you're probably looking for, you can read through the Issues and discussions on the library page and see why that is.

So if your endgame is using headphones, this may not be the right library and you may need to write your own.

from react-native-bluetooth-classic-apps.

software3daerospace avatar software3daerospace commented on June 27, 2024

Hello Ken,

Thank you very much for all the help. I will try this from my end.

from react-native-bluetooth-classic-apps.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.