Connections from one Android device to Another #50

Open
insominx opened this Issue Feb 13, 2014 · 39 comments

Projects

None yet
@insominx

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
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
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
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
ctreton commented Jul 7, 2014

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

@don
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
Owner
don commented Oct 13, 2014

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

@ragcsalo
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
Owner
don commented Dec 10, 2014
@tanzeelrana

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

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
ragcsalo commented Feb 19, 2015 edited

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

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

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

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

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

Thanks 👍

@tanzeelrana

@ragcsalo did you upload the project ?

@ragcsalo
ragcsalo commented Feb 22, 2015 edited

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 :)
@Slim2k
Slim2k 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

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

@don don added the enhancement label Dec 3, 2015
@BurakDev
BurakDev commented Dec 8, 2015

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

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

@ateufel
ateufel commented Dec 8, 2015

@ragcsalo the link to the zip file does not work anymore

@don
Owner
don commented Dec 9, 2015

@BurakDev There's a fork that should support Android to Android #50 (comment)

The original code for Android was based on the Android chat example that @ateufel mentioned. I think people enabled the RFCOMM listening to make this work.

Maybe someone can issue a pull request based on @tomvanenckevort or @ragcsalo's changes?

For iOS to iOS, you'll need one of the iOS devices to be running a Bluetooth Low Energy service that looks like one of the supported UART services. (Nordic/Adafruit, RedBearLab, BlueGiga, Laird)

For Android to iOS or iOS to Android, you'd need Bluetooth Low Energy, since iOS doesn't (easily) support Bluetooth Classic. You can use another one of my plugins https://github.com/don/cordova-plugin-ble-central to connect to Bluetooth low energy services. You'd need to create the service with native code for now. (I have cordova-plugin-ble-peripheral planned, but not implemented).

@ragcsalo
ragcsalo commented Dec 9, 2015

Uhh... I accidentaly deleted the ZIP file... I'll upload it again soon!!!

@cjtoribio

@ragcsalo I am new with this library and saw all the comments on connecting two android devices as i tried and failed. I see the ZIP files are not up. If you can send a pull request as @don said.

@ateufel
ateufel commented Jan 11, 2016

@cjtoribio just take a look at the official bluetooth chat example, it´s a pretty good tutorial with all the code your need:

@gcdpinho

I am having a problem to send and receive messages between two devices android. I place the connection and the function write returns me sucess , but I can't receive the message on another device.

Srry abount my english, i'm brazilian.

@ateufel
ateufel commented Apr 11, 2016

that´s a bit too broad, i´m afraid. all we know is "it doesn't work", which is nothing we can work with. take a look at the links in my comment above, it should be quite easy to figure out what´s wrong with the original code from google. if you really can´t figure it out, better post the relevant code on stackoverflow with a detailed description.

@paredesivan

@ragcsalo could you upload the file again?

This was referenced Sep 1, 2016
@sando431
sando431 commented Oct 4, 2016 edited

@ragcsalo

http://download.webtanar.hu/BluetoothSerial/bluetooth.zip

I am seeing 404 error on this page .

Also i tried your changes and in both android devices, it is just showing Connecting and immediately disconnected.

Can you please help me regarding this.

@ragcsalo
ragcsalo commented Oct 5, 2016

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

@ragcsalo
ragcsalo commented Oct 5, 2016

Instructions

1) Create your Cordova project as usual:
========================================
cordova create project_name com.yourdomain.project_name project_name
cd project_name
cordova platform add android
cordova build android

2) Add the necessary plugins to your project (select the ones you need):
=============================================
cordova plugin add cordova-plugin-inappbrowser
cordova plugin add cordova-plugin-device
cordova plugin add cordova-plugin-dialogs


3) Add the BluetoothSerial plugin:
==================================
cordova plugin add https://github.com/jackTheRipper/BluetoothSerial


4) Copy the working .js and .java files from this .zip file (to your project, overwriting the old files):
===========================================================
platforms/android/assets/www/plugins/com.jacktheripper.cordova.bluetoothserial/www/bluetoothSerial.js
platforms/android/src/com/megster/cordova/BluetoothSerial.java
platforms/android/src/com/megster/cordova/BluetoothSerialService.java


5) In your main file (index.html) make the BT connection:
=========================================================
See the HTML and JavaScript code in platforms/android/assets/www/index.html
The necessary js/jquery-1.9.1.min.js is also included in this .zip file.


6) Build the project, and install the .apk on both Android devices


7) Turn on Bluetooth and pair your devices (on the Bluetooth settings page of your devices)


8) Run the app, and click on 'Connect BT remote' on BOTH devices


9) When the connection is OK, send a message to the other device
   Type a message in the text field, and press 'Send message' button.

@sando431
sando431 commented Oct 6, 2016

Thanks a lot ragcsalo .It will really help me.I just installed your apk and its working as expected.Will go through the code now and ask you if any help needed in understanding..

Thanks again.... :)

@htahir6200 htahir6200 referenced this issue in muaz-khan/RTCMultiConnection Oct 23, 2016
Open

Signalling Server Issue #268

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