react-native-bluetooth-classic icon indicating copy to clipboard operation
react-native-bluetooth-classic copied to clipboard

how to make a active conversation between 2 device

Open xudo49 opened this issue 11 months ago • 14 comments

env react native v.0.73 android 7 and android 11

now I can sand text from device 1 to device 2 but device 2 can't send back to device 1

reproduce open device2 and listen accept use device 1 connect to device 2 and send some message it worked very well

in device 2 can't send message back to device 1

please provide some solution

xudo49 avatar Mar 07 '24 08:03 xudo49

Are there errors? Anything in the log? Delimiters setup?

You'll need to do some debugging, but I can't see why this shouldn't work. I just don't have time time or devices to try this out.

kenjdavidson avatar Mar 07 '24 12:03 kenjdavidson

Are there errors? Anything in the log? Delimiters setup?

You'll need to do some debugging, but I can't see why this shouldn't work. I just don't have time time or devices to try this out.

this is not error but I using your example code ( convert to functional ) and try to send message from host ( device 1 ) can be send all of time, opposite in device 2 can't sent a message is disable send button( just hacked by remove disable flag ) is same issue

image

xudo49 avatar Mar 07 '24 16:03 xudo49

I appreciate that you may be using the sample app, but this was never tested with android to android communication. I did the testing with two very distinct bluetooth devices (they are listed on the readme). If you're having issues, based on what you've said:

  1. Try to debug and see why the disable was set to true?
  2. If bypassing the disabled flag, and its still not working, try debugging to see why its not working? I would assume it was disabled for a reason.
  3. Is the Second device not sending? Or is the first device not reading? Both are possible.

Anyhow, as mentioned, you'll need to do some debugging and if there is an issue I can walk you through fixing and accept a PR for it. But I won't be able to setup and debug this for you.

kenjdavidson avatar Mar 07 '24 16:03 kenjdavidson

let me try :d

xudo49 avatar Mar 07 '24 16:03 xudo49

@kenjdavidson today I'm just try to make chat using example app I need to know how is work phoneOne -> call Accept() <- phoneTwo send some message to phoneOne via writeToDevice() [ send success ] phoneOne callBack by sendBack via writeToDevice() <- not working.

question if I want to send message back to that devices, how to send or what method do please provide step to procedure like [ disconnect() -> connect() -> writeToDevice() ] for sender and receiver open accept () thanks

xudo49 avatar Mar 12 '24 13:03 xudo49

The library just wraps the Bluetooth functionality into basic socket programming. There are two phases that are used:

  1. The connection phase. This is generally used on the device with connectToDevice() in order to grab the Socket to a peripheral. When working with phone to phone, the accept() function was added. So in the grand scheme of things:
Phone 1 - `accept()`
Phone 2 - `connectToDevice()`
Phone 1 - `open socket`
Phone 2 - `open socket`
  1. Once the sockets are opened, it works just like you'd expect:
Phone 1 - `write()` will write data to the socket
Phone 2 - `read()` will read data from the socket
Phone 2 - `write()
Phone 1 - `read()`

If you want to throw in the listener instead of manual reads:

Phone 1 - `onDeviceRead()`
Phone 2 - `onDeviceRead()`
Phone 1 - `write()` will call the Phone 2 `onDeviceRead()` callback
Phone 2 - `write()` will call the Phone 1 `onDeviceRead()` callback

There is no need to disconnect and reconnect in order to write. Once you've accept and connectToDevice the sockets are open, and should communicate without any issues.

kenjdavidson avatar Mar 12 '24 14:03 kenjdavidson

Phone 1 - `onDeviceRead()`
Phone 2 - `onDeviceRead()`
Phone 1 - `write()` will call the Phone 2 `onDeviceRead()` callback
Phone 2 - `write()` will call the Phone 1 `onDeviceRead()` callback

I think just try but listener not working in hooks

image

xudo49 avatar Mar 12 '24 15:03 xudo49

Well that doesn't look right, you're only calling listening() in the return from useEffect(), which if you read the documentation is the unloadComponent function, so it will only start listening when the unload happens?

https://legacy.reactjs.org/docs/hooks-effect.html#example-using-hooks-1

kenjdavidson avatar Mar 12 '24 15:03 kenjdavidson

Well that doesn't look right, you're only calling listening() in the return from useEffect(), which if you read the documentation is the unloadComponent function, so it will only start listening when the unload happens?

https://legacy.reactjs.org/docs/hooks-effect.html#example-using-hooks-1

sorry, wrong pictures :x

xudo49 avatar Mar 12 '24 15:03 xudo49

Sadly I haven't gotten around to re-writing the sample app using functions instead of classes. But I don't see anything majorly wrong with that code.

As mentioned before, you'll have to debug the native java code:

  • Confirm that the message is being read and stored in the buffer
  • Confirm that the delimiter is finding the message and splitting it
  • Confirm that it's calling the sendEvent method

if all those things work, then maybe something is up with useEffect() which would suck, and I'm sure is unlikely since if so there would have been far more open issues on here.

kenjdavidson avatar Mar 12 '24 15:03 kenjdavidson

Sadly I haven't gotten around to re-writing the sample app using functions instead of classes. But I don't see anything majorly wrong with that code.

As mentioned before, you'll have to debug the native java code:

  • Confirm that the message is being read and stored in the buffer
  • Confirm that the delimiter is finding the message and splitting it
  • Confirm that it's calling the sendEvent method

if all those things work, then maybe something is up with useEffect() which would suck, and I'm sure is unlikely since if so there would have been far more open issues on here.

I tried Class I worked but, it can't Interact between two phone can you help me double check that code because I'm poor in class base

xudo49 avatar Mar 12 '24 15:03 xudo49

The code looks fine, there's not much I can do without debugging it, so I suggest you spend some time actually debugging it. If you want to link your repository, I'll take a peak when I can, but I can't promise anything.

kenjdavidson avatar Mar 12 '24 15:03 kenjdavidson

The code looks fine, there's not much I can do without debugging it, so I suggest you spend some time actually debugging it. If you want to link your repository, I'll take a peak when I can, but I can't promise anything.

after pull current code. it can't run, [ my env is different ] I need to upgrade react core and something for run these project and Im not sure these lib work fine for a new react and react native version

it Can't Start metro java 17 node 18.17.1 [ node 8 and 14 too ]

xudo49 avatar Mar 12 '24 15:03 xudo49

The library uses the legacy native module code, it has not been updated to use turbo modules, so it's entirely possible that React has stopped allowing legacy native modules. I would suggest NOT updating react native versions, and using only the versions which have been documented as working.

kenjdavidson avatar Mar 12 '24 15:03 kenjdavidson