Giter VIP home page Giter VIP logo

Comments (35)

kenjdavidson avatar kenjdavidson commented on June 27, 2024 1

First off, let me just confirm that we're talking about the same thing:

Bonded Devices (paired)

  • are devices which have gone through the pairing process in system
  • or are paired through the app (not really tested much)

Connected Devices

  • are devices that have an open socket connection

This means, in order to have a connected device, you must first perform:

  • device.connect()
  • RNBluetooth.connectToDevice()

In my companies application I have a very specific device to which we connect, so the flow in that particular app is:

  • App starts
  • Paired devices are retreived
  • If paired devices contains the specific device CLASS i'm looking for
  • device.connect()
  • When the app stops, disconnect

the getConnectedDevice() or getConnectedDevices() are mainly there for flavour, incase, for some reason, you choose not to maintain the connected device in the React Native state (which I guess you could do, but makes no sense). It's also possible if the connection stays open across Pause and Resume lifecycles, but I've found this to be annoying to attempt. So it's just easier to disconnect and connect to the same device.

Once I get around to having the Connection work in the background, this will be a little more useful.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024 1

If you can provide more details on the IOException, it would help. You'll need to debug the java code in the library during the socket.connect() logic and try to grab the actual error. Things that have stopped it are:

  • The peripheral is already connected somewhere
  • Something on the peripheral code drops/blocks the connection
  • Just because a device is paired, doesn't mean it's on or nearby (this seems dumb, but it's been an issue in the past)

Any other number of things.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024 1

Hey! You are an amazing man. You just resolved my so long stretched mistake! You're amazing man truly amazing. Thank you very much! Now that I put my device in accept mode and it is getting connected! I can't thank you enough! You're great 👏

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024 1

haha, I don't know about that. But I'll take it.
Good luck going forward.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

The BluetoothClassicExample is really used for development, it's just named poorly I guess since I've split it out. To get it working you'll also need to clone the actual library react-native-bluetooth-classic in order to get it working.

With regards to that specific error, it looks like you have React Native versioning issues - I'm unsure of what version or how you have it installed, maybe globally?

facebook/react-native#21093

Has a quick fix that might work and get you up and running. I'm unable to replicate this on my machine, so I'm going to trust the fixes from that issue.

With regards to starting your own app, it should be fairly straight forward:

  • Just install the library into your application like you normally would npm install react-native-bluetooth-classic and start going just like you would with any other library.
  • Unless you're using IOS or a customized DeviceConnection you can implement using the API as you see fit.

I'm not sure what other "flow" you're looking for.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

I started the project from scratch!
Anyways, I am able to retrieve bluetooth status and paired devices too!
But I am not able to retrieve Connected Devices. Can you please give me hints on how should be the flow stepwise. I simply want to connect bluetooth to HC05 bluetooth module and send data which is only 1 character long. For this much work do I have to include all the methods described in docs or only few will do work?
Work flow in mind is as simple as scanning for devices -> connecting to module -> sending the data as long as module is connected.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

When I use connect() method, it needs device from props to be connected. Can it be done as from paired devices list, when I click on one of the device then it should get connected? How can I achieve this and what things I need to pass in connect() method?

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

When I use connect() method, it needs device from props to be connected. Can it be done as from paired devices list, when I click on one of the device then it should get connected? How can I achieve this and what things I need to pass in connect() method?

You 100% want to do a getBondedDevices() first and then use those devices to perform the connection.

The connect parameters all depend on your logic and devices:

  • Delimiter
  • Charset
  • Timeout

The list of parameters should all be documented.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

I could do everything smoothly uptil getBondedDevice(). How am I going to specify the device I want to connect to?

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

This block of code doesn't tell which device I need to connect to. If I need to pass let say id of device, then how am I supposed to pass it in connect() method?

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

getBondedDevices() returns an array of BluetoothDevice objects.

You choose which one of those objects you want to connect to:

  • Either by the user clicking a button
  • Doing a bondedDevices.find(device -> device == some criteria)

Now you have two options:

  • You do a device.connect() which is just a helper method on the BluetoothDevice that automatically passes the ID
  • You do a RNbluetoothClassic.connectToDevice(device.id) which is essentially what gets called anyhow.

You can choose whatever method you like, I've left it completely open for developers to decide what works best for them.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

If you maintain the application state:

pairedDevices = [
  { id: 12354, ..., connect() },
  { id: 12356, ..., connect() },
  { id: 12358, ..., connect() },
];

Then when you attempt the connection you maintain that:

connectedDevice =   pairedDevices[0].connect();
// connectedDevice = { id: 12354, ..., connect() },

at an extremely high level

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Okay. That's understandable. I'll try implementing it.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

when I tried to connect the paired device as you mentioned, I am getting WARN Possible Unhandled Promise Rejection (id: 0): Error: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 this warning followed by an error. How am I supposed to resolve that?
My code looks like:

            this.state.pairedDevices.map((item, index) => {
              return (
                  <View key={index} style={styles.pairedDeviceListStyle}>
                    <Text>{item['name']}</Text>
                    <Text>{item['id']}</Text><Button title='Connect' onPress = {()=>this.connectToDevice(item)}/>
                  </View>
              )
            })

and connectToDevice() looks like

  connectToDevice(device){
    device.connect();
  }

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

First of all you should be wrapping the connect in a try/catch to handle the error a little more gracefully.

Second of all, that's just a socket IO error, which is going to be related to your device and/or peripheral. I'm sure you're aware that socket development can be super fun with regards to these types of things.

There isn't much other than that I can help with, it looks like it's a communication issue:

  • Accept mode enabled?
  • Connection type?
  • Secure/Insecure?
  • Etc.

A whole bunch of things can mess up socket connections.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

I will enable Accept mode first. How am I supposed to fx the issue?

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

As I mentioned before, this is where you need to start looking into your peripheral. The connect method that you've provided will use the defaults:

  • Secure RFCOMM
  • Delimiter \n
  • Charset ASCII

You need to ensure that these are all correct on the HC05. You also need to ensure that the HC05 is setup to accept a bluetooth connection, sometimes the peripherals need to be pushed into accept mode so that they can be connected to. This is where I said, I don't own an HC05 and have not had a chance to test with one.

But you're getting an IO exception, which means the Socket attempting the connection and something failed. I cannot help any further than this without actually debugging your phone or HC05. Now you're just down to good old fashioned Socket development.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Fine! You already helped a lot! This looks like kind of discussion than issue 😆 Thank you very much!

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Not able to get connected devices using method getConnectedDevices() even if a device is connected. What am I doing wrong here?
Also, onDeviceDisconnected method works properly but onDeviceConnected doesn't work. By any way, not able to get connected device nor connect to a device.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

This is where you need to do some debugging.

  • do you get an error or just empty list?
  • how did you end up getting the socket to open? What was the issue?
  • can you show me the variables content in the module while debugging that method?

I need more than "it doesn't work".

"By any way, not able to get connected device nor connect to a device."

If you can't connect to a device, why do you think there are connected devices? That doesn't make any sense.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024
  • I am getting an empty list.
  • I didn't debug the issue. More specifically I don't know how to debug it from source library.
    I manually connected device and checked if my application shows if its connected or not. It still returns false when I check if the device is connected.
    I tried to get connected device first but it returns empty list.
    Then I manually connected it and checked if it returns true on checking if device is connected, it still returns false.
    I connected manually that's why I thought it should show if the specific device is connected. The socket error I couldn't debug and do not even know how to debug it.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

I have no idea what you mean by manually connected. Paired is Not connected. Having an open socket is connected. If you can't call device.connext() successfully you're not connected.

You debug it just like any app. Open up Android studio, install the debug version and set some break points. The source is installed with the library in react native. Just look for the library inside the project manager

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

By manually connecting I mean, by connecting to other device from mobile settings and coming back to my application and checking connection.
To write data on device I first of all need to pair then connect to the device. Am I right? I've achieved pairing but not connection due to which I'm still unable to send data.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

So you're pairing with the device. And get bonded devices works? Yes this is the first step.

You then need to open a serial socket. Which is done, as we discussed, with device.connect(). Which you are having issues with. You need to sort that connection, before Get Connected Devices will return anything.

And for that it's a little more in depth on your part. I can't debug your devices, so not much I can help with until I see debug logs.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Okay fine. I'll find some way to debug it. Thank you very much!

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

You should be able to open your

project/android

folder inside Android Studio. From there, you'll see the

react-native-bluetooth-classic

in the library list in the project management section. You can open it up and find the methods you want to debug.

From there, you have to setup the ADB bridge, which you probably already have done in order to test locally. Then just start the application from Android Studio, INSTEAD of from React Native CLI.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Okay fine! Will try that :)

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

I opened the android folder from my project in android studio. But there is no library list nor react-native-bluetooth-classic.
ss

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

There should be a pregen class in there called Build Config or something. It's where the extra modules get loaded too. You can use that file to follow to the project path.

Try looking at it in another view. Not Android but package (in the top left corner)

I have stuff to do this weekend so I won't be in front of my computer.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Hey! Didn't disturb you as it was weekend! I started project in android studio from scratch and followed some docs and tutorials. The error is still same i.e. java.io.IOException: read failed, socket might closed or timeout, read ret: -1, but this time I know where it is occuring!

public void run(){
            try{
                socket.connect();
            }catch (IOException e){
                Log.e("Connect->Run:",e.toString());  // <---------------------------------
                try{
                    socket.close();
                }catch (IOException er){
                    Log.e("Connect->Close:",er.toString());
                }
                connectionFailed();
                return;
            }

            synchronized ( ChatUtils.this){
                connectThread = null;
            }

            connected(device);
        }

Here the error is occuring!
my socket is initialized by device.createRfcommSocketToServiceRecord(APP_UUID); and
my APP_UUID is 00001101-0000-1000-8000-00805F9B34FB
I believe that socket is not able to make connection.
I stackoverflow ed the issue, but they suggested to create an insecure rfcomm socket.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

So ya, it looks like socket.connect(); is causing the problem. This is a low level connection issue between the devices, this has nothing (per say) to do with the library. If you think that creating an insecure socket will work, then you can attempt to do so by supplying that parameter:

device.connect({ secure: false })

I believe is the connection parameter name, it should be in the documentation.

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Okay! I will try that. 👍

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

Hey! Sorry for bugging you again and again. But I am still stuck on the same problem. Whenever you have time can you please help me?
It seems problem is exactly at socket.connect();. And in there the exception is thrown by `readAll(); method. I am totally clueless why might this be happening. It seems that in

    private int readAll(InputStream is, byte[] b) throws IOException {
        int left = b.length;
        while (left > 0) {
            int ret = is.read(b, b.length - left, left);
            if (ret <= 0) {
                throw new IOException("read failed, socket might closed or timeout, read ret: "
                        + ret);
            }
            left -= ret;
            if (left != 0) {
                Log.w(TAG, "readAll() looping, read partial size: " + (b.length - left)
                        + ", expect size: " + b.length);
            }
        }
        return b.length;
    }

this method, in my case the ret variable somehow becomes -1 and it throws IOException error. I tried few solutions on stackoverflow where they have said to create a fallback method, but it didn't work for me.
Your help is needed if possible.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

I don't mind the questions, but you're asking me about code that is deep in the android library. There isn't much I can do as this is very much a device specific issue. If the socket is not opening or is being forced shut then something is up on your phone or your hc05 device.

I do wish I could help more, but I don't have your phone. And I don't have an hc05 to replicate your code.

https://android.googlesource.com/platform/frameworks/base/+/9a7debe5857ffc7c71cfc4082b1b6d72a5cf81cd/core/java/android/bluetooth/BluetoothSocket.java

Readall
Readint
Connect

Is how that's called, and it's while it's attempting to pick up a channel/port. So it looks like the socket and stream are opened but it's unable to read the port number accordingly.

Did you say whether you can connect with the Bluetooth Terminal app natively?

from react-native-bluetooth-classic-apps.

viren-vii avatar viren-vii commented on June 27, 2024

I am currently trying to connect to an android phone only assuming that it will work on hc05 as well. I tried reading and understanding code inside library and it seems I can't do anything about it.
When initiated connect() method, it connects for 2-3 seconds and get disconnected again.

from react-native-bluetooth-classic-apps.

kenjdavidson avatar kenjdavidson commented on June 27, 2024

Oooohhhhhhhhhhh.

How are you doing that? Do you have this library on both devices?

If so you have to enable accept mode on one device and then connect. The accepting phone needs to have a BluetoothServerSocket open and listening before you'll be able to do that.

That's why you're not able to connect, you don't have a server socket listening for the connection.

This library supports this. You just have to do it properly. If you're using the Bluetooth sample app on both devices it will work.

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.