rusel1989 / react-native-bluetooth-serial

Port of https://github.com/don/BluetoothSerial for react native
476 stars 291 forks source link

Is polling required to receive data? #2

Open esutton opened 8 years ago

esutton commented 8 years ago

I want to connect to a Bluetooth Classic SPP device ( not an Arduino device ).

Use Case1:

  1. Send a command to device from Android.
  2. Process response from device when it is received.

Use Case2:

  1. Connect to an external Bluetooth GPS.
  2. GPS constantly streams NMEA text data
  3. Receive and process NMEA text data as it arrives and update location information.

Is there an API event or something that can be used to asynchronously handle data reception from the Bluetooth device?

Thanks for sharing your work. Any advice is appreciated.

rusel1989 commented 8 years ago

Hey, Both use cases will need some fixes/updates ... I just came back from vacation so i will start working on it ... but you know Pull Requests are welcome

jcteague commented 8 years ago

I'm needing this functionality too. I'm checking to see if the product owners want me to contribute to this project to make it work. If they do, do you have general idea of the work. Feel free to dm me if that's easier. P.S. I'm currently targeting Android.

rusel1989 commented 8 years ago

@jcteague how does it look ? I currently have some free time so if you are not going to do it, i will start working on it

jcteague commented 8 years ago

I'm on hold right now. I will know more next week.

On Sep 9, 2016 2:18 PM, "Jakub" notifications@github.com wrote:

@jcteague https://github.com/jcteague how does it look ? I currently have some free time so if you are not going to do it, i will start working on it

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rusel1989/react-native-bluetooth-serial/issues/2#issuecomment-246012233, or mute the thread https://github.com/notifications/unsubscribe-auth/AAElJoGJ8OsVVD0kwJb4n6L_yYSqUkUDks5qobEJgaJpZM4Jdxkq .

rusel1989 commented 8 years ago

@jcteague okay so i will start working on it, i just checked it and it seems that there shoudn't be much work on making it work

jcteague commented 8 years ago

rgr. I'll check in with you next week and see if there is anything I can do to help.

FYI, my use case. I'm connecting to a bluetooth device connected a vehicle. I can send an $AT command that will respond with info about the vehicle (gps, rpms, etc..)

On Fri, Sep 9, 2016 at 3:05 PM, Jakub notifications@github.com wrote:

@jcteague https://github.com/jcteague okay so i will start working on it, i just checked it and it seems that there shoudn't be much work on making it work

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rusel1989/react-native-bluetooth-serial/issues/2#issuecomment-246024667, or mute the thread https://github.com/notifications/unsubscribe-auth/AAElJm6Gterx9QAXz7Qm5ssY65S_8EIFks5qobwggaJpZM4Jdxkq .

jcrjaci commented 8 years ago

@jcteague i need to do that as well. Did you use this library or developed something your own?

jcteague commented 8 years ago

we've punted on this for now, focusing on other requirements. We will need to address it at some point.

On Fri, Sep 16, 2016 at 11:14 AM, jcrjaci notifications@github.com wrote:

@jcteague https://github.com/jcteague i need to do that as well. Did you use this library or developed something your own?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rusel1989/react-native-bluetooth-serial/issues/2#issuecomment-247642138, or mute the thread https://github.com/notifications/unsubscribe-auth/AAElJuyVMGseGSATuexzvXh9YJkVAEvOks5qqsBPgaJpZM4Jdxkq .

arvindpdmn commented 8 years ago

The BTSerial API is documented here: https://github.com/derektu/react-native-bluetooth-serial This may help in reading data on app when received from device. I think setDataAvailableCallback() may help.

timscott commented 7 years ago

I'm taking over for @jcteague. Any progress on this?

By the way, I can't find a way to receive data from the connected device through polling or any other means. When I write, the resolved response is true which I'm guessing means the write succeeded; it's not a response from the device.

I guessing I will need to extend this package to implement at least the read function and perhaps subscribe. Is that correct?

timscott commented 7 years ago

After some digging I have discovered that read and subscribe (and many other functions) are already implemented. They are just not documented nor used in the sample app.

In regards to the original question, it would appear that subscribe does the job. However, I have not been able to test that yet.

rusel1989 commented 7 years ago

@timscott yep you're right, i didn't test these, so i don't know if they're working correctly ... I don't have much time lately ...

jlucier commented 7 years ago

@timscott @rusel1989 I'm trying to do read/write operations similar to what has been discussed here. Any update on whether or not read and subscribe are functional?

timscott commented 7 years ago

@jlucier - I've got read working. Realize that read will read what's written which might not be everything you want. If you need a request-response type implementation, as I do, you will need to read into a buffer (I just append to a string) in a tight synchronous loop until you reach a delimiter.

subscribe does not work for me at all. It's only ever called in response to a successful connect, returning true.

jlucier commented 7 years ago

@timscott Sounds good. I was expecting to have to buffer the messages like that. Is read working on the most recent release (the version installed via npm) or something else?

Searingfang commented 7 years ago

Subscribe does appear to work. You have to call Bluetooth.subscribe(delimiter).then(whateverfunction). That promise will always just return true but to get the data you need to make a listener with Bluetooth.on('data', dataHandler).

timscott commented 7 years ago

@Searingfang - Ah ha! That makes sense. Great work! I'll try that ASAP.

storerjeremy commented 7 years ago

It seems I am also working on something similar @timscott Did you manage to get subscribe working? It would be awesome if someone could update the example app to show how to use subscribe?

jlucier commented 7 years ago

@storerjeremy It does work. I did what @Searingfang said to do and it worked without issue.

storerjeremy commented 7 years ago

@jlucier Awesome, thanks for the confirmation it works. I'll give it a shot. Cheers

storerjeremy commented 7 years ago

@jlucier @Searingfang I tried using BluetoothSerial.subscribe('\n').then(whateverfunction) and BluetoothSerial.on('data', dataHandler) in the example app, however when the subscribe method is called it brings up an error "TypeError: _reactNativeBluetoothSerial2.default.subscribe is not a function". Any idea whats going on?

Searingfang commented 7 years ago

@storerjeremy Based on the package.json in the example app it uses the version of the bluetooth serial library on github. This is actually a different version than you would get from npm install. The version on github doesn't have the subscribe feature for some reason.

So probably all you have to do is change the package.json dependancy for react-native-bluetooth-serial from "file:../" to "^0.1.6". If that doesn't work make a small app from scratch. Cards on the table, I wasn't able to get the example working when I first tried.

jorge627 commented 7 years ago

Hi I want confirm that with subscribe it works, but I have a question, Do you know what is the limit time between data for receive it ?, I mean if my app is paired with a bluetooth device that is constantly sending data, what is the maximum frequency to send data from device to the app and obviously that the app can read each data properly ?, for example if the bluetooth device sends data each 10ms the app is in capacity to read each data properly ?

Thanks in advance

pewh commented 7 years ago

@Searingfang I'm trying to use subscribe, but I don't have any idea how to send command. What I'm trying is: after connect to device, send request 'ATI' like this. Thanks

Searingfang commented 7 years ago

@pewh sending a command uses the write command. What you receive is dependent on the device. If you are trying to use subscribe then you have to know the device is delimiting its messages with something. For instance mine uses a newline \r\n. So if you subscribe('\r\n') then whenever you get a newline from your device Bluetooth serial will dispatch a data event that you can handle with on('data', dataHandler)

babyinthesky commented 7 years ago

for the latest github version, I get the listener for receiving data working by the following way. Note that you have to replace the delimiter '\r\n' with the one upon your own case.

BluetoothSerial.withDelimiter('\r\n').then((res)=>{ console.log("delimiter setup",res); BluetoothSerial.on('read',(data)=>{ console.log('read',data); }) })

arrygoo commented 7 years ago

Any updates on doing this without setInterval would be sweet

hungtm84 commented 5 years ago

anyone have subscribe function example ? please share. tks!

jorge627 commented 5 years ago

@hungtm84 I created a small project with bluetooth using the subscribe function ( it also use redux ) you can check it here, it is still a work in progress but the bluetooth is working, may be it can help you.

https://github.com/jorge627/react-native-redux-bluetooth

ichbinvinh commented 5 years ago

for the latest github version, I get the listener for receiving data working by the following way. Note that you have to replace the delimiter '\r\n' with the one upon your own case.

BluetoothSerial.withDelimiter('\r\n').then((res)=>{ console.log("delimiter setup",res); BluetoothSerial.on('read',(data)=>{ console.log('read',data); }) })

Hi @babyinthesky ,

i placed your code in componentWillMount() but no events was catched when my scanner read barcode.

BluetoothSerial.withDelimiter('\r\n').then(() => { // BluetoothSerial.on('read', (data) => { // Toast.showShortBottom('DATA FROM BLUETOOTH 2:'+JSON.stringify(data)) // }); // })

I use this version : "react-native-bluetooth-serial": "^1.0.0-rc1"

Thanks in advance

WhaSukGO commented 4 years ago

Any update? BluetoothSerial.withDelmiter(... doesn't seem to work