Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to multiple devices at the same time #58

Open
j6strickland opened this issue Mar 12, 2014 · 32 comments
Open

Connect to multiple devices at the same time #58

j6strickland opened this issue Mar 12, 2014 · 32 comments

Comments

@j6strickland
Copy link

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.

@don
Copy link
Owner

don commented Mar 20, 2014

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

@darudude
Copy link

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

@benwnuk
Copy link

benwnuk commented Jul 2, 2014

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?

@don
Copy link
Owner

don commented Jul 7, 2014

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
Copy link
Owner

don commented Sep 23, 2014

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

@rosterloh
Copy link

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

@don
Copy link
Owner

don commented Sep 23, 2014

@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.

@rosterloh
Copy link

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

On Tue, Sep 23, 2014 at 8:29 AM, Don Coleman notifications@github.com
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
#58 (comment).

Kind ReKind Regards
Richard Osterloh

@stakutis
Copy link

stakutis commented Feb 9, 2015

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

@don
Copy link
Owner

don commented Feb 9, 2015

@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.

@stakutis
Copy link

stakutis commented Feb 9, 2015

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 notifications@github.com
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
#58 (comment).

@don
Copy link
Owner

don commented Feb 9, 2015

@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.

@stakutis
Copy link

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 notifications@github.com
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
#58 (comment).

@darudude
Copy link

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!

@stakutis
Copy link

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 notifications@github.com 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
#58 (comment).

@darudude
Copy link

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 notifications@github.com 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 notifications@github.com
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
<#58 (comment)
.


Reply to this email directly or view it on GitHub
#58 (comment).

@darudude
Copy link

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.

@stakutis
Copy link

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 notifications@github.com 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
#58 (comment).

@endoplasmic
Copy link

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.

@stakutis
Copy link

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 notifications@github.com
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
#58 (comment).

@stakutis
Copy link

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 chris.stakutis@gmail.com
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 notifications@github.com
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
#58 (comment)
.

@don
Copy link
Owner

don commented May 25, 2016

@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.

@stakutis
Copy link

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 notifications@github.com
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
#58 (comment)

@flockf
Copy link

flockf commented Mar 24, 2018

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.

@davidgaldon
Copy link

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!

@manaskant
Copy link

manaskant commented Jul 19, 2021

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

@Fomenko-developer
Copy link

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

@AbrilDAngelo
Copy link

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!

@Fomenko-developer
Copy link

Fomenko-developer commented Feb 24, 2023

@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: developer.fomenko.artjoms@gmail.com

@AbrilDAngelo
Copy link

@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: developer.fomenko.artjoms@gmail.com

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

@Fomenko-developer
Copy link

@AbrilDAngelo going to sleep now, if u will have any questions, will respond tomorrow

@Fomenko-developer
Copy link

@AbrilDAngelo did it help you? Sorry, to the author for spaming here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests