kenjdavidson / react-native-bluetooth-classic

⚛ Bluetooth classic Android(Bluetooth)/IOS(ExternalAccessory) module for serial communication
https://kenjdavidson.github.io/react-native-bluetooth-classic
MIT License
250 stars 94 forks source link

onDeviceConnected is not triggered? #173

Closed ShaneZhengNZ closed 2 years ago

ShaneZhengNZ commented 2 years ago

Mobile Device Environment Provide a list of operating systems on which this issue is relevant.

Application Environment Provide information about your development environment:

Describe the bug It looks like the RNBluetoothClassic.onDeviceConnected event is not triggered when the Bluetooth device is connected.

To Reproduce Steps to reproduce the behavior:

  1. Create an Expo RN project with Bluetooth feature enabled.
  2. Add code to subscribe to RNBluetoothClassic.onDeviceConnected and RNBluetoothClassic.onDeviceDisconnected
  3. Try to connect and disconnect a paired Bluetooth device. I am using SONY wireless headphones (WF-1000XM3).
  4. When the headphone is connected, the onDeviceConnected callback is not called, however, when I switch the headphone off (device disconnected), the onDeviceDisconnected callback is called.
  5. I also tried to connect and disconnect programmatically with .connect() and .disconnect() methods. Exactly the same issue, onDeviceConnected does not work, but onDeviceDisconnected works.

Expected behavior RNBluetoothClassic.onDeviceConnected event should be triggered when paired device is connected to the phone.

kenjdavidson commented 2 years ago

It looks like I removed it from the acl receiver on purpose. Can't remember why but I'm guessing that when I converted all the requests to promises it made no sense to lisen for a connection as the result of the promise should give you want you need, or reject if failed.

Disconnect is needed so you can handle cleanup appropriately.

Is there any reason why you'd prefer the event over the promise resolve?

kenjdavidson commented 2 years ago

Can easily add it back into the AclReciever and Implement in the module. Feel free to give it a go and submit a pr if you confirm it working.

But again using. Connect should return a promise with he device. Also headphones are usually not rfcomm, they usually need the a2dp profile (from other issues ppl have asked about).

But if the acl event doesn't happen, then it won't really matter. I remmeber, now, reading the the acl connected wasn't a good indicator of socket connection, and that only disconnect was certain. I think that's why I removed it.

Sadly I didn't remove the js and documentation.

ShaneZhengNZ commented 2 years ago

I didn't try the promise, will try that first. It that works, then the onDeviceConnected event is not required. In which case, I totally agree that the js and documentation should be removed. Thank you for the prompt reply

kenjdavidson commented 2 years ago

Keep me Posted bud.

Get Outlook for Androidhttps://aka.ms/ghei36


From: Shane Zheng @.> Sent: Saturday, March 5, 2022 9:19:10 PM To: kenjdavidson/react-native-bluetooth-classic @.> Cc: Ken Davidson @.>; Comment @.> Subject: Re: [kenjdavidson/react-native-bluetooth-classic] onDeviceConnected is not triggered? (Issue #173)

I didn't try the promise, will try that first. It that works, then the onDeviceConnected event is not required. In which case, I totally agree that the js and documentation should be removed. Thank you for the prompt reply

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkenjdavidson%2Freact-native-bluetooth-classic%2Fissues%2F173%23issuecomment-1059880108&data=04%7C01%7C%7C44973744a86e4818df5908d9ff17b3b1%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821299528503676%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=WFVuGOZ6d1SMhkZxG0stKIPSXKI9fluNp2OJCUj0u1I%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABPMJL7WX7JVOYJO2B5K6ZLU6QIZ5ANCNFSM5QAI5Y3Q&data=04%7C01%7C%7C44973744a86e4818df5908d9ff17b3b1%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821299528503676%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=pSga6oJuizt6yHp%2FT7xgBKRkN%2FuGaXqiRx%2BMchyVhaA%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7C44973744a86e4818df5908d9ff17b3b1%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821299528503676%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=uJmebv5jeIigxZSl0avdoF5ZwQJs9V7RPxfYwrcItcM%3D&reserved=0 or Androidhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7C44973744a86e4818df5908d9ff17b3b1%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821299528503676%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=2KFdi7Fz4ceANFGZ2BcjUeEPxWpAa6zVOoMLiNrInmI%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

ShaneZhengNZ commented 2 years ago

I am sorry, in fact, I am using the promise.

Here is what I am doing - await the promise to resolve with an infinite loop to achieve auto connect

let connectionEstablished = false;
while (!connectionEstablished) {
  try {
    connectionEstablished = await deviceToConnect.connect();
    if (connectionEstablished) {
      // Run my device connected function
      break;
    }
  } catch (error) {
    // Do nothing when the connect fails. Just back to the loop and connect again.
  }
}

I am not 100% happy about the infinite loop, but I don't think the onDeviceConnected event handle can solve this either. It is an ok solution for now (at least from my perspective), not sure whether you have any better way to achieve the similar auto connect feature.

If you agree, I can close this issue. Maybe open a PR to delete the onDeviceConnected event handle and documentation.

kenjdavidson commented 2 years ago

You shouldn't need on the loop. I think you're running into an issue with a headset needing a2dp and not rfcomm. I don't think you can use this library for headsets. There are a few issue open and closed already about this.

Get Outlook for Androidhttps://aka.ms/ghei36


From: Shane Zheng @.> Sent: Saturday, March 5, 2022 9:59:42 PM To: kenjdavidson/react-native-bluetooth-classic @.> Cc: Ken Davidson @.>; Comment @.> Subject: Re: [kenjdavidson/react-native-bluetooth-classic] onDeviceConnected is not triggered? (Issue #173)

I am sorry, in fact, I am using the promise.

Here is what I am doing - await the promise to resolve with an infinite loop to achieve auto connect

let connectionEstablished = false; while (!connectionEstablished) { try { connectionEstablished = await deviceToConnect.connect(); if (connectionEstablished) { // Run my device connected function break; } } catch (error) { // Do nothing when the connect fails. Just back to the loop and connect again. } }

I am not 100% happy about the infinite loop, but I don't think the onDeviceConnected event handle can solve this either. It is an ok solution for now (at least from my perspective), not sure whether you have any better way to achieve the similar auto connect feature.

If you agree, I can close this issue. Maybe open a PR to delete the onDeviceConnected event handle and documentation.

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkenjdavidson%2Freact-native-bluetooth-classic%2Fissues%2F173%23issuecomment-1059884182&data=04%7C01%7C%7C0684678b0d0945f087e408d9ff1d5cbf%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821323835644996%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=zxEFtoQlknn0OTsKzzxJuozYjVTmoVP9rc7mDAB4m9o%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABPMJLZWYT6DEFVR2UUMMH3U6QNR5ANCNFSM5QAI5Y3Q&data=04%7C01%7C%7C0684678b0d0945f087e408d9ff1d5cbf%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821323835644996%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=eWDRaAQg7ig5tGExFANGvROzLQqvWYq3dD5XXA62TeE%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7C0684678b0d0945f087e408d9ff1d5cbf%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821323835644996%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=KP5MDhZVLSKW33NkoymLazTnqz4nWvy18ktdbXoeWnE%3D&reserved=0 or Androidhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7C0684678b0d0945f087e408d9ff1d5cbf%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821323835644996%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=Gh6o1wHDgzNgFqn955ih0qAxgPLwbXL4Y5uHPB659rc%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

ShaneZhengNZ commented 2 years ago

I don't know too much details about bluetooth, that's why I use a headset as an example, obviously it is not appropriate.

I am actually using the library to connect to Bluetooth GPS device (Garmin GLO™ 2). Not sure whether the loop is appropriate to achieve auto connect? If not, what is your suggestion?

kenjdavidson commented 2 years ago

I'd say no. Either the device connects or it doesn't. There is no point in a loop, as you have to handle the exception of fails.

Get Outlook for Androidhttps://aka.ms/ghei36


From: Shane Zheng @.> Sent: Saturday, March 5, 2022 10:23:34 PM To: kenjdavidson/react-native-bluetooth-classic @.> Cc: Ken Davidson @.>; Comment @.> Subject: Re: [kenjdavidson/react-native-bluetooth-classic] onDeviceConnected is not triggered? (Issue #173)

I don't know too much details about bluetooth, that's why I use a headset as an example, obviously it is not appropriate.

I am actually using the library to connect to Bluetooth GPS device (Garmin GLO™ 2). Not sure whether the loop is appropriate to achieve auto connect? If not, what is your suggestion?

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkenjdavidson%2Freact-native-bluetooth-classic%2Fissues%2F173%23issuecomment-1059886606&data=04%7C01%7C%7C75e580f5600244ebb4be08d9ff20b301%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821338169823384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=Ag9C84Kd%2FOqXJA2M80vyO1scRV3DXK7wzsVVg41Ijxg%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABPMJL2UMZXQKEGYV2RY67LU6QQLNANCNFSM5QAI5Y3Q&data=04%7C01%7C%7C75e580f5600244ebb4be08d9ff20b301%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821338169823384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=lFw2flP4NcRRy2nq3bKZvpru2SPteBNOYnfBGp%2B76OY%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7C75e580f5600244ebb4be08d9ff20b301%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821338169823384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=2BU7X49bCX8SPN2txQLtBCM%2F1o99Hin91ceJPYFxEWs%3D&reserved=0 or Androidhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7C75e580f5600244ebb4be08d9ff20b301%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821338169823384%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=a%2F1pcw0OV5ijWIRgB4n%2BUT6%2BTYOoQMYsYmV2ga14E%2B8%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

ShaneZhengNZ commented 2 years ago

True, however, that means I can't achieve auto connect. With the loop, I can have the loop running so that whenever the Bluetooth device is powered on, it connects.

Unless there are other ways to achieve that auto connect that I am not aware of.

kenjdavidson commented 2 years ago

That's not what connected is anyhow. Connected is the rfcomm socket. There is no way to know when the device comes online besides try/catch on connect like you're doing in a loop.

But it should be try catch, not try false.

Get Outlook for Androidhttps://aka.ms/ghei36


From: Shane Zheng @.> Sent: Saturday, March 5, 2022 10:48:57 PM To: kenjdavidson/react-native-bluetooth-classic @.> Cc: Ken Davidson @.>; Comment @.> Subject: Re: [kenjdavidson/react-native-bluetooth-classic] onDeviceConnected is not triggered? (Issue #173)

True, however, that means I can't achieve auto connect. With the loop, I can have the loop running so that whenever the Bluetooth device is powered on, it connects.

Unless there are other ways to achieve that auto connect that I am not aware of.

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fkenjdavidson%2Freact-native-bluetooth-classic%2Fissues%2F173%23issuecomment-1059889171&data=04%7C01%7C%7C87b67fef23f4409dce1408d9ff243e76%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821353392996906%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=OX67ZjJJOyEUDca%2FwgmXNsPEO1VKsazfNkHB7n9TkV8%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FABPMJL6367JCMU6RAGPZIMDU6QTKTANCNFSM5QAI5Y3Q&data=04%7C01%7C%7C87b67fef23f4409dce1408d9ff243e76%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821353392996906%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=Pq5tfVNjh7Vnlnj4660AQrc1%2BnF%2FFYaflp%2BYRlGxRcI%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7C%7C87b67fef23f4409dce1408d9ff243e76%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821353392996906%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=WmcFaGHCGX1eIw29LVpOlgaFEwvyRxSZjQMc8FTR%2Fvw%3D&reserved=0 or Androidhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7C%7C87b67fef23f4409dce1408d9ff243e76%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637821353392996906%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=qh9Bmv5ncsOt8r7p%2B3iXhlXTrzjKutdJdP9nPAidDxg%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

RahulMvr2824 commented 1 year ago

Hi kenjdavidson, I'm designing an App where device takes the sensor readings from hardware model and displays it on multiple screens which is the requirement for us. So in order to do that I had to perform two communication. Therefore, I need to check the Bluetooth connection status and pass the message from the mobile to desired hardware model. I was facing trouble in handling onDeviceConnected. Could please guide me how to resolve that.

kenjdavidson commented 1 year ago

On device connected was deprecated. It was an old method that posed no real purpose after i updated the library. You get a device back from the connection request now that should be used.

You'd have to provide explicit details to your use case, as there are other ways to handle it.

RahulMvr2824 commented 1 year ago

My use case is I'm building an app with multiple screens like home page, temperature measurement page etc., including Bluetooth connectivity page which means for connecting Bluetooth devices I'm using a separate page. So here I'm navigating from home page to Bluetooth page and connecting to the device required and coming back to home page where my two way Bluetooth communication starts. From home page itself I need to read data from the hardware module and write data to module. So here in need to check the device Bluetooth connectivity first.

kenjdavidson commented 1 year ago

Ya. That's not what on connected device was originally for. So even if it 2asnt deprecated it wouldn't work how you're expecting.

You just want to connect to a device and store that connection at the app level. Just remember that lifecycle changes will kill the connection.

RahulMvr2824 commented 1 year ago

Hi there, I working on a react native project for IoT device where the sensor readings from the hardware needs to be displayed on Mobile App through Bluetooth. Here I'm very well established a connection to the device and also able to communicate with the device. But the problem is I cannot able to read the data at the same instant the hardware device throws it into the channel. Which means I'm missing out some data in between two read operations. Where as while read through the for loop as mentioned in your repository code it's getting lagged between the data which comes into channel and data which is fetched.(for better understanding, the instant I fetch one data value there are tons of data getting logged into buffer).

So kindly please help me with this kind of issue on smooth & efficient data communication. Thanks in advance

kenjdavidson commented 1 year ago

But the problem is I cannot able to read the data at the same instant the hardware device throws it into the channel

Are you not using the onDeviceRead handler, which should be passing messages to your React side code as soon as a delimited message is found? If this is a separate issue, you should:

from what you're saying I can only imply that you are only doing timeout/polling type device.read() and you are missing messages? Which still shouldn't be the case, since device.read() should eventually give you all the messages.

kenjdavidson commented 1 year ago

You're going to have to deal with getting data on your own, you're two options are:

  1. onDeviceRead() which is event based
  2. read() which is polling based

both have their pros and cons.

You can even go so far as writing your own BluetoothConnection implementation and handle direct data that way. The library should be open enough for any number of options.

RahulMvr2824 commented 1 year ago

You're going to have to deal with getting data on your own, you're two options are:

  1. onDeviceRead() which is event based
  2. read() which is polling based

both have their pros and cons.

You can even go so far as writing your own BluetoothConnection implementation and handle direct data that way. The library should be open enough for any number of options.

Thank you for your help. Will try the same and post the outcome.