BluetoothSerial icon indicating copy to clipboard operation
BluetoothSerial copied to clipboard

Connect to multiple devices at the same time

Open j6strickland opened this issue 10 years ago • 32 comments

I am able to successfully connect and read from a single device at a time using the plugin. Does the plugin support being able to connect and read data from multiple Arduino devices at the same time ? I am using Arduino Uno board with serial communication to an cordova application on Android. When I try to connect to multiple devices after pairing them, I cannot connect to either device. If I only attempt connection to a single device at a time, the connection and data read works.

j6strickland avatar Mar 12 '14 21:03 j6strickland

Right now the plugin supports one connection at a time. Connecting to multiple devices is possible, but will require code changes.

don avatar Mar 20 '14 23:03 don

I'd be interested in this as well. Don - is there anyway we can add this as a feature request?

darudude avatar May 30 '14 19:05 darudude

This is also some functionality I could really use in my own project. I'm a long time web developer but I've never done any native mobile development. Can anyone elaborate on just what sort of a task adding multiple clients would be? Are we talking adding a couple new functions or a fairly involved rewrite/refactoring?

benwnuk avatar Jul 02 '14 04:07 benwnuk

It's doable to have this connect to multiple client connections but not trivial. You need to keep track of which callbacks go to which client. I'm working on a new BLE plugin and it should support multiple connections, but adding multiple connections to this plugin isn't high on my priority list right now.

don avatar Jul 07 '14 21:07 don

If you're doing Bluetooth Low Energy try Cordova BLE Plugin which supports simultaneous connections to multiple peripherals on iOS and Android.

don avatar Sep 23 '14 06:09 don

Hi @don is there an example anywhere of how one might connect to multiple peripherals?

rosterloh avatar Sep 23 '14 07:09 rosterloh

@rosterloh Basically you can just call connect() multiple times. When you use the BLE plugin you're responsible for managing the device ids, since they need to be passed into every call.

Here's a rough example on a branch. I hope to add a better example later.

don avatar Sep 23 '14 07:09 don

Awesome, I'll give it a test. Thanks!

On Tue, Sep 23, 2014 at 8:29 AM, Don Coleman [email protected] wrote:

@rosterloh https://github.com/rosterloh Basically you can just call connect() multiple times. When you use the BLE plugin https://github.com/don/cordova-plugin-ble-central you're responsible for managing the device ids, since they need to be passed into every call.

Here's a rough example https://github.com/don/cordova-plugin-ble-central/tree/multipleDevices/examples/multipleConnections on a branch. I hope to add a better example later.

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-56484527.

Kind ReKind Regards Richard Osterloh

rosterloh avatar Sep 23 '14 07:09 rosterloh

Any further update/hope/news on having this work with more-than-one connect at a time?

stakutis avatar Feb 09 '15 16:02 stakutis

@stakutis I'm mostly work with BLE lately. My BLE plugin will do multiple simultaneous connections. I don't expect it will get added to this plugin unless a client needs it.

don avatar Feb 09 '15 20:02 don

Thanx. I suppose the BLE Plugin will not work with devices are not BLE, right? The device I'm using is a medical PEF meter ("AM1+") and it has been working fantastic with your BT Serial plugin, but we want to support more devices simultaneously. The device itself doesn't even say what type of BT it is...is there any chance it supports both or is that dumb...?...as dumb as asking if my Android phones support both, right? (I know iOS is BLE-only). Uhg, makes things hard to a single application that works across platforms and supports a variety of devices. If you have any ideas, I'm all ears! Thanx. [?]

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Mon, Feb 9, 2015 at 3:30 PM, Don Coleman [email protected] wrote:

@stakutis https://github.com/stakutis I'm mostly work with BLE lately. My BLE plugin https://github.com/don/cordova-plugin-ble-central will do multiple simultaneous connections. I don't expect it will get added to this plugin unless a client needs it.

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-73584005.

stakutis avatar Feb 09 '15 20:02 stakutis

@stakutis it sounds like your device is bluetooth classic. If it works with this plugin and Android it's Serial Port Profile (SPP). Some devices support Classic and BLE, but most don't. Newer Android devices with 4.3+ can support BLE, but your PEF meter probably doesn't do BLE.

don avatar Feb 09 '15 21:02 don

What about double-instantiation...? (To allow more-than-one Connect at a time). What if I put: In my config file twice (same package name, but a unique feature name). I believe Cordova would instantiate the pluginclass twice and I'd have essentially two different sets of wrapper functions. Would that work? (It would also be so easy and less risky). As long as there are no class-static data...

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Mon, Feb 9, 2015 at 4:31 PM, Don Coleman [email protected] wrote:

@stakutis https://github.com/stakutis it sounds like your device is bluetooth classic. If it works with this plugin and Android it's Serial Port Profile (SPP). Some devices support Classic and BLE, but most don't. Newer Android devices with 4.3+ can support BLE, but your PEF meter probably doesn't do BLE.

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-73594327.

stakutis avatar Feb 10 '15 12:02 stakutis

Should have responded to this a few months back when I got it working. I essentially did what stakutis is suggesting but had to take it a step further and change variable names in the android src files.

The coee on the client side is ugly but the functionality was great!

darudude avatar Feb 10 '15 12:02 darudude

Thanx!

What variable names did you have to change? I have no problem with simply calling cordova.exec() directly and not using the wrapper javascript anyway. In fact, I did a project here recently where we dynamically re-load dex files from an off-machine library and then overload "cordova/exec" (but that wont be needed here if I can pre-load it multiple times).

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Tue, Feb 10, 2015 at 7:59 AM, darudude [email protected] wrote:

Should have responded to this a few months back when I got it working. I essentially did what stakutis is suggesting but had to take it a step further and change variable names in the android src files.

The coee on the client side is ugly but the functionality was great!

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-73694926.

stakutis avatar Feb 10 '15 13:02 stakutis

I'll have to check once I get to my computer. Haven't touched that code base in some time.

On Tue, Feb 10, 2015, 08:04 stakutis [email protected] wrote:

Thanx!

What variable names did you have to change? I have no problem with simply calling cordova.exec() directly and not using the wrapper javascript anyway. In fact, I did a project here recently where we dynamically re-load dex files from an off-machine library and then overload "cordova/exec" (but that wont be needed here if I can pre-load it multiple times).

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Tue, Feb 10, 2015 at 7:59 AM, darudude [email protected] wrote:

Should have responded to this a few months back when I got it working. I essentially did what stakutis is suggesting but had to take it a step further and change variable names in the android src files.

The coee on the client side is ugly but the functionality was great!

— Reply to this email directly or view it on GitHub <https://github.com/don/BluetoothSerial/issues/58#issuecomment-73694926 .

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-73695633.

darudude avatar Feb 10 '15 13:02 darudude

Alright - not sure of the exact steps I ran through to accomplish this (it was back in August and finally managed to get it work late into the night). However, here are the differences:

  1. I have two versions of the plugin in my plugin folder - com.megster.cordova.bluetoothserial & com.megster.cordova.bluetoothserial2.

  2. File name changes in com.megster.cordova.bluetoothserial2:

  • BluetoothSerial.java -> BluetoothSerial2.java
  • BluetoothSerialService.java -> BluetoothSerialService2.java
  • bluetoothSerial.js - > bluetoothSerial2.js
  1. Code changes in com.megster.cordova.bluetoothserial2:
  • bluetoothSerial2.js -> all references to BluetoothSerial modified to BluetoothSerial2
  • plugin.xml -> all references to BluetoothSerial modified to BluetoothSerial2, all references to BluetoothSerialService modified to BluetoothSerialService2,
  • fetch.json -> all references to BluetoothSerial modified to BluetoothSerial2

I believe that is it. There doesn't seem to be any changes to the java files as I originally thought.

darudude avatar Feb 10 '15 13:02 darudude

So what i did...and I think it works but I dont have a 2nd device...was I just put another "" tag, using the same package path, but gave it a different feature name. I put some debugs in...I can tell that the java class got instantiated twice...and i'm able to cordova.exec() to either one.

But I have only one device to play with...I was able to connect/read from one, disconnect it, then do the same from the other, and it worked.

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Tue, Feb 10, 2015 at 8:23 AM, darudude [email protected] wrote:

Alright - not sure of the exact steps I ran through to accomplish this (it was back in August and finally managed to get it work late into the night). However, here are the differences:

  1. I have two versions of the plugin in my plugin folder - com.megster.cordova.bluetoothserial & com.megster.cordova.bluetoothserial2.
  2. File name changes in com.megster.cordova.bluetoothserial2:
  • BluetoothSerial.java -> BluetoothSerial2.java
  • BluetoothSerialService.java -> BluetoothSerialService2.java
  • bluetoothSerial.js - > bluetoothSerial2.js 3) Code changes in com.megster.cordova.bluetoothserial2:
  • bluetoothSerial2.js -> all references to BluetoothSerial modified to BluetoothSerial2
  • plugin.xml -> all references to BluetoothSerial modified to BluetoothSerial2, all references to BluetoothSerialService modified to BluetoothSerialService2,
  • fetch.json -> all references to BluetoothSerial modified to BluetoothSerial2

I believe that is it. There doesn't seem to be any changes to the java files as I originally thought.

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-73698008.

stakutis avatar Feb 10 '15 15:02 stakutis

For those that are still looking for a work around and stumble upon this thread, this is what you want to do to sort yourself out.

In plugins/cordova-plugin-bluetooth-serial/plugin.xml You want to add more feature nodes like so:

<feature name="BluetoothSerial">
  <param name="android-package" value="com.megster.cordova.BluetoothSerial"/>
</feature>
<feature name="BluetoothSerial2">
  <param name="android-package" value="com.megster.cordova.BluetoothSerial"/>
</feature>
<feature name="BluetoothSerial3">
  <param name="android-package" value="com.megster.cordova.BluetoothSerial"/>
</feature>

In my case I had to run cordova platform remove android and then cordova platform add android to have the extra instances recognized.

Then in your JS code you can run cordova.exec() like this:

cordova.exec(
    function(devices) {
      console.log('Device List:', JSON.stringify(devices));
    },
    function(error) {
      console.log('ERROR', error);
    },
    'BluetoothSerial2',
    'list',
    []
);

First param is the success callback, then the error callback. Next 3 are the important ones. You specify the instance name of the class, then the method name, then an array of arguments to pass to that method. In my example the list method doesn't have any, but you can imagine write would have the string you want to send to the device.

endoplasmic avatar Jan 15 '16 06:01 endoplasmic

Not really sure what you're asking, but this article is really good at explaining cordova/phonegap/native stuff: http://devgirl.org/2013/07/17/tutorial-how-to-write-a-phonegap-plugin-for-android/

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Fri, Jan 15, 2016 at 1:05 AM, endoplasmic [email protected] wrote:

@stakutis https://github.com/stakutis, can you explain the process for calling cordova.exec()? I understand the modification regarding the config.xml file (although mine doesn't have an entry for any of my plugins other than whitelist)

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-171879745.

stakutis avatar Jan 15 '16 12:01 stakutis

Hey Don...will your bluetooth stuff work on windows10? can windows10 support classic mode?

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Fri, Jan 15, 2016 at 7:38 AM, Chris Stakutis [email protected] wrote:

Not really sure what you're asking, but this article is really good at explaining cordova/phonegap/native stuff:

http://devgirl.org/2013/07/17/tutorial-how-to-write-a-phonegap-plugin-for-android/

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Fri, Jan 15, 2016 at 1:05 AM, endoplasmic [email protected] wrote:

@stakutis https://github.com/stakutis, can you explain the process for calling cordova.exec()? I understand the modification regarding the config.xml file (although mine doesn't have an entry for any of my plugins other than whitelist)

— Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-171879745 .

stakutis avatar May 25 '16 17:05 stakutis

@stakutis I have a pull request from @Davide-sd that adds Windows 10 support. I need to get that merged. Window 10 will support Bluetooth Classic.

don avatar May 25 '16 19:05 don

Excellent! How soon? We have a big project and needs the support right away...help...

Chris Stakutis 978 764 3488 Twitter: @Stakutis

On Wed, May 25, 2016 at 3:54 PM, Don Coleman [email protected] wrote:

@stakutis https://github.com/stakutis I have a pull request from @Davide-sd https://github.com/Davide-sd that adds Windows 10 support. I need to get that merged. Window 10 will support Bluetooth Classic.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/don/BluetoothSerial/issues/58#issuecomment-221687797

stakutis avatar May 26 '16 10:05 stakutis

And this way we can connect simultaneously with a maximum of seven devices, right? If i understand correctly, that is the limit of connections for bluetooth. Does anyone know from experience?

Thanks.

flockf avatar Mar 24 '18 02:03 flockf

Hi, first of all, thanks so much @don for your plugin. I just started playing around with it on an Ionic app. I know you treated this topic already, but was anyone able to get multiple connections working? Is there any example? And another question: would this plugin allow streaming music to 2 pair of headphones from the same phone? Any other idea it case it can't? I would like to do it with an app instead of with a "Bluetooth splitter" device. Thanks so much for your time!

davidgaldon avatar Jul 27 '19 23:07 davidgaldon

You can use the teraterm serial terminal to view and connect multiple bluetooth devices and view the data they send at a time by opening multiple instances of teraterm and connecting the corresponding bt to serial port link

manaskant avatar Jul 19 '21 12:07 manaskant

I know many years passed, but today i faced neet to connect 2 phones ( this is possible also with pc if you have bluetooth ). If any of you seeks a solution how to communicate between 2 phones useng cordova in 2022, this works: https://github.com/bugnano/cordova-plugin-networking-bluetooth I had a struggle a bit but i got it working ( a little confusing documentation ), but if u need help woth it - you can ping me

Fomenko-developer avatar Nov 06 '22 18:11 Fomenko-developer

I know many years passed, but today i faced neet to connect 2 phones ( this is possible also with pc if you have bluetooth ). If any of you seeks a solution how to communicate between 2 phones useng cordova in 2022, this works: https://github.com/bugnano/cordova-plugin-networking-bluetooth I had a struggle a bit but i got it working ( a little confusing documentation ), but if u need help woth it - you can ping me

@Fomenko-developer I am currently trying to develop this feature for an app (android to android bluetooth connection) with no success so far. It would be very helpful for me if you could assist me or send any relevant documentation! Regards!

AbrilDAngelo avatar Feb 24 '23 18:02 AbrilDAngelo

@AbrilDAngelo Here is the repo with working sample: connect and send data to second device ( it will show the selected color ). https://github.com/Fomenko-developer/TestBluetooth If any questions here is my emails: [email protected]

Fomenko-developer avatar Feb 24 '23 19:02 Fomenko-developer

@AbrilDAngelo Here is the repo with working sample: connect and send data to second device ( it will show the selected color ). https://github.com/Fomenko-developer/TestBluetooth If any questions here is my emails: [email protected]

Thank you very much for your help and quick response! I'm going to check it out right now!

AbrilDAngelo avatar Feb 24 '23 22:02 AbrilDAngelo