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

Connections from one Android device to Another #50

Open
insominx opened this issue Feb 13, 2014 · 75 comments
Open

Connections from one Android device to Another #50

insominx opened this issue Feb 13, 2014 · 75 comments

Comments

@insominx
Copy link

I know this was intended to connect with Arduino but it seems like connecting between a pair of android or ios devices could work too. Is this already the case? I fiddled with the examples for a while (using a nexus 7 and galaxy s4) but could not establish a connection.

The examples don't appear to have code to receive a connection in the javascript. Is that only in the arduino logic?

@don
Copy link
Owner

don commented Feb 18, 2014

The code to receive a connection on Android not implemented in the plugin.

This Android code in the plugin is based on sample in the Android SDK. They have a good example of Android to Android chat. See if you can get something working and send pull request.

@fppt
Copy link

fppt commented Feb 21, 2014

Hello,

Is communication between android devices planning on being implemented ? I ask because I need that functionality and don't want to delve into hacking this great plugin unless it's absolutely necessary of course.

@don
Copy link
Owner

don commented Feb 22, 2014

@Fido0101 Android to Android would be nice to have, but I don't think I'll have it implemented soon. If you get something working submit a patch or at least a link to your code.

@ctreton
Copy link

ctreton commented Jul 7, 2014

@don Hi, is there some news for this? it may be very nice :)

@don
Copy link
Owner

don commented Jul 7, 2014

@ctreton No news on this. I'm concentrating on BLE lately. I plan to allow multiple connections in the BLE plugin I'm working on.

@don
Copy link
Owner

don commented Oct 13, 2014

More info on how this could be implemented #78 (comment)

@ragcsalo
Copy link

ragcsalo commented Nov 2, 2014

I've managed to connect Android devices using Phonegap 2.7, with some modifications to the Java source code. I think there was an error in the code, some lines were turned into a comment, I had to "uncomment" them. Now I'm experimenting with Phonegap 4.0 but couldn't install the plugin yet (using cordova plugin add).

@don
Copy link
Owner

don commented Dec 10, 2014

@tomvanenckevort has a forked version that might work https://github.com/tomvanenckevort/BluetoothSerial

@tanzeelrana
Copy link

This version does not work for me.

  • I downloaded the project
  • copied the Chat example in a separate place
  • cordova plugin add com.megster.cordova.bluetoothserial
  • cordova platform add android
  • cordova build - > and run

but again I see the same issue. when I try to connect my nexus 4 to my nexus 5 or vice versa. it instantly says disconnected. Was anyone successful in connecting 2 android devices with https://github.com/tomvanenckevort/BluetoothSerial ?

@ragcsalo
Copy link

I have updated the Java and JavaScript files, these are working just fine.
:-)
Please feel free to use them!
Regards, Bence

2015-02-19 18:49 GMT+01:00 tanzeelrana notifications@github.com:

This version does not work for me.

  • I downloaded the project
  • copied the Chat example in a separate place
  • cordova plugin add com.megster.cordova.bluetoothserial
  • cordova platform add android
  • cordova build - > and run

but again I see the same issue. when I try to connect my nexus 4 to my
nexus 5 or vice versa. it instantly says disconnected. Was anyone
successful in connecting 2 android devices with
https://github.com/tomvanenckevort/BluetoothSerial ?


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

@ragcsalo
Copy link

ragcsalo commented Feb 19, 2015

Download updated files from here (instructions and final .APK of my test app included):
http://benkesmith.com/android/bt_serial/Bluetooth_Serial.zip

Download the full Android Studio project from here:
http://benkesmith.com/android/bt_serial/btserial_project.zip

@tanzeelrana
Copy link

thanks !! @ragcsalo I am going to try this out .....

https://github.com/tomvanenckevort/BluetoothSerial

have you tried this app? my last comment was regarding this link above. I tried this and it was not connecting 2 android devices

@ragcsalo
Copy link

ragcsalo commented Feb 19, 2015 via email

@tanzeelrana
Copy link

@don
Under limitations it says

Limitations

The phone must initiate the Bluetooth connection
iOS Bluetooth Low Energy requires iPhone 4S, iPhone5, iPod 5, or iPad3+
Will not connect Android to Android*
Will not connect iOS to iOS*

does that need to be changed ?

@ragcsalo
Copy link

ragcsalo commented Feb 19, 2015 via email

@tanzeelrana
Copy link

@ragcsalo with your updated files the app is forever on discovering devices
or it just normally takes long time to discover devices ?

Are you sure those are the only files that need to be updated ?

@ragcsalo
Copy link

ragcsalo commented Feb 19, 2015 via email

@tanzeelrana
Copy link

If you can zip the project that works for you and share I will try to find the differences in code and figure out what is causing this problem.

@ragcsalo
Copy link

ragcsalo commented Feb 19, 2015 via email

@tanzeelrana
Copy link

Thanks 👍

@tanzeelrana
Copy link

@ragcsalo did you upload the project ?

@ragcsalo
Copy link

ragcsalo commented Feb 22, 2015

Sorry for the delay. I have updated the source files from Hungarian to
English... :-)

Download the full Android Studio project from here:
http://benkesmith.com/android/bt_serial/btserial_project.zip

  1. pair the 2 Android devices over Bluetooth first
  2. install BluetoothSerial.apk on both devices
  3. after starting the app, select other device from dropdown list (on both
    devices)
  4. click on "Connect BT remote" on both devices
  5. after connected, you can send messages over Bluetooth :)

@JonD84
Copy link

JonD84 commented Jul 6, 2015

Hi,

I know this is 5 months old since last activity, but I am trying to send data between 2 Android devices (Nexus 7 and Moto X 2nd Gen) I Can get them to connect and stay connected using the .connect() and isConnected() functions.

But when I use the Write() function, I get a success callback that the string was sent, but neither read() or Subscribe is receiving the data. Really not sure what else to try, code is very messy as I have been trying all different things, but I have pasted it below for any advise. I just want to send a string from one device to another the same as you would a chat app, although this isn't a chat app.

//Hosting the game
$scope.isHost = null;
//Watching the game
$scope.isGuest = null;

var Delimiter = '\n';
var DeviceAddress;

var isTurnedOn
var isEnabled;
var isConnected;
var sender;
var reciver;

//Check is Bluetooth is turned on
$scope.btEnabled = function () {
    $cordovaBluetoothSerial.isEnabled().then(
            function () {
                isEnabled = "Bluetooth is enabled";
            },
            function () {
                btEnabled = "Bluetooth is NOT enabled";
            }
    )
    return isTurnedOn;
};

//Check if device is connected
var s = ""
$scope.connectionStatus = function () {
    setTimeout(function () {
        $cordovaBluetoothSerial.isConnected().then(
                function (info) {
                    console.log("BT Connected");
                    s = "Bluetooth is connected";
                    isConnected = true;
                },
        function () {
            console.log("BT *not* connected");
            s = "Bluetooth is not connected";
            isConnected = false;
        });
    }, 5000);

    return "is "+s;
};

//List of BT devices
$scope.btList = {}
$scope.btDiscover = function () {
    $cordovaBluetoothSerial.list().then(
        function (devices) {
            $scope.btList = devices;
            $scope.showBTlist = true;
        },
        function (err) {
            console.log(err);
            $scope.showBTlist = false;
        }
    )
};

function subScribe() {
    $cordovaBluetoothSerial.subscribe('\n',
        function (data) {
            console.log(data);
            $scope.subscribData = "Sub Data = " + data;
    });
}


$scope.btConnect = function (item) {

    //Determin devce
    if (item.address) {
        //Adress Android and WP
        DeviceAddress = item.address;
    }
    else if (item.uuid) {
        // UUID used by iOS
        DeviceAddress = item.uuid;
    }

    if (isConnected != true) {
        sender = true;
        $cordovaBluetoothSerial.connect(DeviceAddress).then(
            function (success) {
                console.log('Connected');
                $scope.connectedTo = item.name;
                // subScribe();
                //stayConnected();
            },
            function (err) {
                console.log('Connection failed');
                $scope.connectedTo = item.name + err;
            }
        )
    }
    else {
        console.log("Subscribe");
        reciver = true;
        subScribe();
    }
};


$scope.sendMessage = function (message) {
    //$cordovaBluetoothSerial.write('\n' + message);
    $cordovaBluetoothSerial.write('\n' + message).then(
        function (success) {
            console.log("Message Sent")
        },
        function (failure) {
            console.log("Message Sent" + failure)
        }
    )
}

@mitosistech
Copy link

Can you please help me ?I have tried this bluetooth serial plugin.But i can't able to connect the two android device.Please suggest ,what I need to change in code.I am new to cordova

@BurakDev
Copy link

BurakDev commented Dec 8, 2015

@don actually support android-to-android ? ios-to-ios ? android-to-ios ?

@ateufel
Copy link

ateufel commented Dec 8, 2015

@BurakDev there is no support for that, but you can just take a look at the bluetooth chat example in the official android docs for android to android communication.

@ragcsalo
Copy link

ragcsalo commented Dec 8, 2015

I already uploaded my code, I made Android to Android connection with a
modified version of Don's bluetooth serial plugin.
2015. dec. 9. 0:11 ezt írta ("Andreas Teufel" notifications@github.com):

@BurakDev https://github.com/BurakDev there is no support for that, but
you can just take a look at the bluetooth chat example in the official
android docs for android to android communication.


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

@ragcsalo
Copy link

Great! :-) I was also experimenting with it, changing a code a little, and now it works well most of the time. :-)

@flockf
Copy link

flockf commented Aug 17, 2017

@ragcsalo Thanks man, you did an awesome job. And thanks for commenting

@ragcsalo
Copy link

My pleasure to help others :-)

@wang3g
Copy link

wang3g commented Aug 17, 2017

@flockf, is it possible for you to share what you have done to fix the connection problem? Thanks a lot.

@flockf
Copy link

flockf commented Aug 18, 2017

@wang3g Well i just tried again, carefully following the instructions. I think i had missed adding the plugins which the example of @ragcsalo uses.
Have you tested the demo apk provided by @ragcsalo ?

@wang3g
Copy link

wang3g commented Aug 22, 2017

@flockf Yes, the demo apk provided by @ragcsalo works. I also tried again and followed the instructions from step 1 to 9, but still no luck

@flockf
Copy link

flockf commented Aug 22, 2017

@wang3g If you are simply trying to build the chat demo provided, what does your project's folder structure look like? There must be something you are missing. Did you add the three other plugins?
What problem do you have exactly?

@wang3g
Copy link

wang3g commented Aug 23, 2017

screen shot 2017-08-23 at 12 42 08 pm

@flockf Above is my project's folder structure.
I have added all 3 plugins:
cordova plugin add cordova-plugin-inappbrowser
cordova plugin add cordova-plugin-device
cordova plugin add cordova-plugin-dialogs

The problem I m seeing here is:
When i click connect on the 2nd device, it disconnects from the 1st device. And when i click connect on the 1st device, it disconnects from the 2nd device.

@flockf
Copy link

flockf commented Sep 2, 2017

@wang3g if you have not figured it out yet, feel free to upload your project and i will have a look !

@flockf
Copy link

flockf commented Nov 11, 2017

@ragcsalo Man, is it possible to handle multiple, simultaneous connections with yours?

@ragcsalo
Copy link

@flockf I don't think so... :-/

@flockf
Copy link

flockf commented Nov 11, 2017

@ragcsalo ohhh. would the option mentioned in this thread work?
#58

@der1typ
Copy link

der1typ commented Nov 13, 2017

@ragcsalo i just tried to follow ur instruction, but "cordova plugin add https://github.com/jackTheRipper/BluetoothSerial" fails, while "cordova plugin add https://github.com/don/BluetoothSerial" works fine. does this affect the code you've changed?

because i get the same problem like flockf.
when i try to connect the second android device, it disconnects the first...
i'm pretty new to js and can't figure that out :'D

@I-Connect
Copy link

Hi, I have the same issue as der1typ. Seems package.json is missing which prevents the jackTheRipper/BluetoothSerial from installing. Any work around known for this?

thx!

@turkalpkucur
Copy link

@ragcsalo
Hello,
I deeply searched web, tried to accomplish but am at the place,where I began, no progress. your solution

"
I have uploaded the files, instructions and the whole project again! :-)

Download updated files from here (instructions and final .APK of my test app included):
http://benkesmith.com/android/bt_serial/Bluetooth_Serial.zip

Download the full Android Studio project from here:
http://benkesmith.com/android/bt_serial/btserial_project.zip
"
which is, what I exactly try to accomplish in ionic 2 , that you done in pure android.
I think that you deserve to be put on ionic page something like

Repo: https://github.com/ragscalo/BluetoothSerial

Anyway, My aim is , using ionic2 bluetooth-serial or ionic2 bluetooth- ble , I want to send hello world message from one android phone to another android phone.

for this purpose, I prepared an ionic 3 app...

http://blog.turkalpkucur.com/debug.rar

http://blog.turkalpkucur.com/error.jpg

@Alexander055
Copy link

Alexander055 commented Apr 4, 2018

@ragcsalo

Thank for your instruction, it works for me,

Just a little bug I found, once the two devices are connected, if I try to press disconnect on the device I start the connection, and so press disconnect on the other device, I am not able to reconnect them again by pressing connecting on the first device.

  1. on Device_02 I press connect BT remote
  2. on Device_01 I press connect BT remote
  3. now the two devices are connected each other
  4. Press disconnect on Device_01
  5. Press disconnect on Device_02

Now Device_01 is unabled to reconnect to Device_02 by pressing Connect BT remote

@Paras-avantsoft
Copy link

@ragcsalo Thanks man for your solution. I downloaded from below link and the apk works great with my android devices. I am able to transfer the msg from one device to other.

But when I tried to do this with latest version 0.4.7 this is not working. Its always showing me that 'Unable to connect the device'. I also check the documentation of @dom, and its saying that android to android connection will not work.

Is there any solution you have for this?

@ragcsalo
Copy link

ragcsalo commented Apr 5, 2018

I always copy the .java and .js files from one of my earlier projects after I install the plugin.
I should've made a fork or a complete copy of the plugin when everything was working...
Lately I've been experiencing with BT LE plugins, and I could easily make connection between 2 Android (and also iOS) devices! One is the master (central), one is the slave (peripherial), and they both can send/receive data. :-) Check out this plugin: https://github.com/randdusing/cordova-plugin-bluetoothle

@Paras-avantsoft
Copy link

@ragcsalo

Thanks for the quick reply.
I will try by replacing the .java and .js files and will also try your suggested plugin and let you know if its working for me.

@Paras-avantsoft
Copy link

@ragcsalo

Replacing the .java and .js file worked. Thanks man for the help.

@turkalpkucur
Copy link

hey fellas,
thanks ragscalo, but I have to mention that, platform independent solution, native solution (independent from being ios or andro) must be served. thus, the solution of don is a crap! it is not working! I hope, in ionic 4, these issues will be solved. so , there is no solution.

@w4ugit
Copy link

w4ugit commented Apr 25, 2018

Hello. I try to make the application using the instruction and I do not go out, the devices do not connect. can you help me? I really need

@ragcsalo
Copy link

Hello guys,
I have updated the Instructions (see above, and also in the Bluetooth_Serial.zip file) and also the ZIP files. :-) If you've failed to connect 2 Android devices, try again with the new files:

Download updated files from here (updated instructions and final .APK of my test app included):
http://benkesmith.com/android/bt_serial/Bluetooth_Serial.zip

Download the full Android Studio v3.0 project from here:
http://benkesmith.com/android/bt_serial/btserial_project.zip

Good luck! :-)

@longas
Copy link

longas commented Mar 19, 2019

@ragcsalo thanks for your work.

I've used your solution in a Ionic (v4) project and this is what I had to do to make it work.

  • First add the plugin to your project:
    ionic cordova plugin add cordova-plugin-bluetooth-serial
    npm install @ionic-native/bluetooth-serial

  • Download @ragcsalo solution

  • Go to platforms\android\app\src\main\assets\www\plugins\cordova-plugin-bluetooth-serial\www , open bluetoothSerial.js and remove the first line:
    cordova.define("cordova-plugin-bluetooth-serial.bluetoothSerial", function(require, exports, module) {});

  • Then copy that file to your project's plugins\cordova-plugin-bluetooth-serial\www

  • Copy platforms\android\app\src\main\java\com from ragcsalo's package to your project's plugins\cordova-plugin-bluetooth-serial\src\android

  • I recommend removing your android build located in platforms\android and then running:
    ionic cordova build android

@sammy00747
Copy link

@ragcsalo thanks for your work.

I've used your solution in a Ionic (v4) project and this is what I had to do to make it work.

  • First add the plugin to your project:
    ionic cordova plugin add cordova-plugin-bluetooth-serial
    npm install @ionic-native/bluetooth-serial
  • Download @ragcsalo solution
  • Go to platforms\android\app\src\main\assets\www\plugins\cordova-plugin-bluetooth-serial\www , open bluetoothSerial.js and remove the first line:
    cordova.define("cordova-plugin-bluetooth-serial.bluetoothSerial", function(require, exports, module) {});
  • Then copy that file to your project's plugins\cordova-plugin-bluetooth-serial\www
  • Copy platforms\android\app\src\main\java\com from ragcsalo's package to your project's plugins\cordova-plugin-bluetooth-serial\src\android
  • I recommend removing your android build located in platforms\android and then running:
    ionic cordova build android

@longas it says "plugin not installed" as a warning

@akashkhairnar01
Copy link

@ragcsalo i follow all the instruction but it is not working for me

@CalaxDev
Copy link

CalaxDev commented Aug 3, 2020

I am asking here because I have no other way of finding out about my Problem.

Does anyone else of you using this plugin have the problem of sending (writing) data to a remote device (SPP Connection, NOT Android to Android) where messages get lost/replies are somtimes not received?

@chtenb
Copy link

chtenb commented Jul 25, 2022

Is this still the most up-to-date solution and does it work for capacitor?

I've used your solution in a Ionic (v4) project and this is what I had to do to make it work.

  • First add the plugin to your project:
    ionic cordova plugin add cordova-plugin-bluetooth-serial
    npm install @ionic-native/bluetooth-serial
  • Download @ragcsalo solution
  • Go to platforms\android\app\src\main\assets\www\plugins\cordova-plugin-bluetooth-serial\www , open bluetoothSerial.js and remove the first line:
    cordova.define("cordova-plugin-bluetooth-serial.bluetoothSerial", function(require, exports, module) {});
  • Then copy that file to your project's plugins\cordova-plugin-bluetooth-serial\www
  • Copy platforms\android\app\src\main\java\com from ragcsalo's package to your project's plugins\cordova-plugin-bluetooth-serial\src\android
  • I recommend removing your android build located in platforms\android and then running:
    ionic cordova build android

@bevinGithub
Copy link

bevinGithub commented Nov 10, 2022

Hi guys is this solution also applicable on Ionic 6 targeting SDK 31 Android 1, l keep getting error below

"Need android.permission.BLUETOOTH_SCAN permission for AttributionSource { uid = 10467, packageName = com.fire.service.app, attributionTag = null, token = android.os.BinderProxy@a208f16, next = null }: Starting discovery."

Your assistance will be much appreciated.

Below is my environment
Ionic:
Ionic CLI : 6.20.1
Ionic Framework : @ionic/angular 6.2.2
@angular-devkit/build-angular : 13.0.4
@angular-devkit/schematics : 13.0.4
@angular/cli : 13.0.4
@ionic/angular-toolkit : 5.0.3

Cordova:
Cordova CLI : 11.0.0
Cordova Platforms : android 10.1.2
Cordova Plugins : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 5.0.0, (and 14 other plugins)

Utility:
cordova-res : 0.15.4
native-run (update available: 1.7.1) : 1.5.0

System:
NodeJS : v14.15.4 (C:\Program Files\nodejs\node.exe)
npm : 6.14.10
OS : Windows 10

@ash737
Copy link

ash737 commented Dec 10, 2022

@ragcsalo thanks for your work.
I've used your solution in a Ionic (v4) project and this is what I had to do to make it work.

  • First add the plugin to your project:
    ionic cordova plugin add cordova-plugin-bluetooth-serial
    npm install @ionic-native/bluetooth-serial
  • Download @ragcsalo solution
  • Go to platforms\android\app\src\main\assets\www\plugins\cordova-plugin-bluetooth-serial\www , open bluetoothSerial.js and remove the first line:
    cordova.define("cordova-plugin-bluetooth-serial.bluetoothSerial", function(require, exports, module) {});
  • Then copy that file to your project's plugins\cordova-plugin-bluetooth-serial\www
  • Copy platforms\android\app\src\main\java\com from ragcsalo's package to your project's plugins\cordova-plugin-bluetooth-serial\src\android
  • I recommend removing your android build located in platforms\android and then running:
    ionic cordova build android

@longas it says "plugin not installed" as a warning

Hi @sammy00747,
I have same issue plugin not installed
Did you got a solution?

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