Android - communicating between two devices - android

What is the best way for an Android app installed on two devices to communicate with each other? Can the devices connect directly without using text messaging?

You have several options, depending on your requirements and setup:
If your devices are very close to one another (up to about 10 meters), you can communicate using Bluetooth, as Derek suggested.
If your devices are somewhat further away, but within WiFi range of each other (up to about 100 meters), then they can communicate with each other using the Peer-to-Peer WiFi API, documented here (part of the Android Wireless API). This does not require a WiFi router to be present, and the devices will find each other and communicate directly. This does however require Android 4.1 or higher.
The Android Wireless API will also work if your devices are on the same local network (i.e., use the same WiFi router), even if they are not themselves within range of each other.
If none of these options are viable/guaranteed, then I agree with Derek that the easiest way would be to use ServerSocket and Socket to create a server/client interface through the Internet. Here is a sample application doing that. The main problem you might encounter is that if the server is sitting behind a NAT (such as a home internet router), you will have to configure the NAT to forward the incoming packets to your Android server.

You can connect them via bluetooth using BluetoothSockets. Android developer website has pretty good documentation on this.
http://developer.android.com/guide/topics/wireless/bluetooth.html
Or if you'd rather (and have internet on both devices), you can use regular Socket's.
http://developer.android.com/reference/java/net/ServerSocket.html for server side
http://developer.android.com/reference/java/net/Socket.html for client side
If you have a large amount of data to transfer, internet sockets have a greater data capacity and will be faster. The other advantage is that there is no such thing as "out of range". You can connect the two devices wherever internet is available, whereas with bluetooth they have to be within bluetooth range of each other

you can use PubNub. it handles all networking and you should only care about messages.
it has great API to work.
(Thanks to #Ian Jennings : Can we send data from an android device to another android device directly (p2p) without server in the middle?)

Depends on what you are doing. If you have a server, you may be able to send some message to it and have it pulled by the other device (assuming both clients have the app installed). I think this would be the most intuitive way (but it really depends on what you are communicating).
Text messaging and email might work, but you (or the user) needs to know the numbers/emails associated with a device to do that.

you should have a look at WifiDirect
Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later
devices with the appropriate hardware to connect directly to each
other via Wi-Fi without an intermediate access point.

As was already suggested, sockets are the easiest way to accomplish this if your devices are all connected to a network.
There are things to accomplish here:
Use Network Service Discovery to find devices running your app
Connect to other instances of your app using a socket
For a complete tutorial you can check this out

ShortAnswer: Yes
Data can be sent directly.
In order of range:
1 Bluetooth
2 wifidirect
3 maybe.. GSM hardware direct?
After that, options again in range order:
4 tether or network
5 Internet

The android NSD API is meant to do the exact same thing you are trying to achieve! The example bundled with SDK is self explanatory!
please check:
Android NSD API example

Related

What are good ways (existing) to transmit data between multiple mobile phones without internet?

Background
I have an idea for an app on vacation that needs to communicate to other phones with the same app. While on vacation those phones might not all have internet as roaming can be very expensive. The data is not a lot: like 500 kB max would suffice (in json).
Every phone has a bit of info that all the other phones would like to know, but if it helps the info can be stored on 1 phone (master phone from now on) and shared later to the other phones when back home over internet.
Phones
Android, iPhone and Windows Phone
We can't assume they have NFC, IR or zigbee. Just the hardware almost every phone has like bluetooth, camera, microphone etc.
My ideas
QR codes that changes, based on new info: If the first phone is scanned the second phones QR code has data from the 1st phone and itself and the 3rd phone has data from the 1st, 2nd and 3rd (itself) until it reaches that master phone that holds all data.
Data transmission trough sound that we can't hear (or we can). Con is that I don't know if something like this exists for mobile platforms and writing it is like a 3 year master thesis project.
http://nearbytes.com
https://applidium.com
https://developer.chirp.io/
Bluetooth. Can we connect like 8 devices? Would it work consistent (connecting even my headphones can be a hassle, what about 8 phones who try to connect simultaneously)
All of these ideas have big cons. Maybe I'm overlooking a better way.
I will add a bounty to the question for the best solution
An answer that explains it with a little bit of code reference (link is ok) is always better than just: "use bluetooth man"
TL;DR
The easiest (and most supported) way of getting multiple devices to connect to each other is using WiFi. Since your goal is to achieve data transfer with no internet, the most appealing solution would be to use a Peer-to-Peer network structure.
The two major smartphone OS's (Android and iOS) have API's and documentation on creating and transferring data over a Peer-to-Peer network.
Android WiFi P2P
Apple Multipeer Connectivity
These two also have a means to encrypt the data being transferred.
Windows doesn't seem to have an API to allow multiple peers connected, but their Proximity Class will work for one device at a time.
I can give a few outlines over the different options in each major OS:
Android
Android's WiFi P2P (peer-to-peer) API was created for transferring data without internet or another network.
From their documentation:
The Wi-Fi peer-to-peer (P2P) APIs allow applications to connect to nearby devices without needing to connect to a network or hotspot (Android's Wi-Fi P2P framework complies with the Wi-Fi Directâ„¢ certification program). Wi-Fi P2P allows your application to quickly find and interact with nearby devices, at a range beyond the capabilities of Bluetooth.
Google even has Documentation and training on this API.
iOS
Apple's Multipeer Connectivity.
Very similar to Android's P2P API, they claim:
The Multipeer Connectivity framework provides support for discovering services provided by nearby iOS devices using infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks and subsequently communicating with those services by sending message-based data, streaming data, and resources (such as files).
Here is a decent looking tutorial on using Multipeer Connectivity.
--EDIT--
Another iOS way of doing this, which is a bit of a mis-utilization(?) of the tool, is by using GameKit.
However, I think that to get it to work for your purposes might result in a bit of a hack, since the "players" have to be using Game Center.
Windows
The only way (apparently) to connect phones in Windows Phone, is by using Proximity, however, that only gives you the option of connecting no more than two phones together.
They state:
Proximity is a great way to create a shared app experience between two instances of your app running on two different devices.
Those are options in each of the major mobile device OS's.
App usage could be something like:
Decide which device was going to be the "master", so that other devices can connect to it. It isn't required to know this before deploying the app, but there should be a way for the user to decide whether he is going to be a client (receiving data) or the server (pushing data).
Once it was decided between the group of devices which was going to be pushing data, that device would have to be registered as the server (in the Android P2P API, you can establish a "group owner"), and then start looking for peers by initializing the service.
Then, once the devices are connected to the master device, you can start pushing data. An additional bonus is that when using Android WiFi P2P, all communication is encrypted with WPA2, and with iOS, you can enable encryption using MCEnableEncryption (however they state that is slows down data transfer rate).
Now you would just have to pick one method to go with, and make sure that all the phones ran that OS. Because these three methods of connectivity won't work together.
All of the three methods listed are done programmatically, so there should be no strange or odd things that your user will have to do. Searching for other devices, connecting, and transferring data can all be done within your app.
More help can be provided if the question is narrowed down to specific problems, but this should be enough data to get you started.
Don't try QR or sound. I think it would be very painful to transmit 500kb of data.
Bluetooth seems like a good solution but maybe, as you already said, hard to configure.
What do you think about wifi?
At least every Android and iPhone device can create a mobile wifi hotspot. By using this, you can easily setup a environment where 8 devices are in the same LAN (without using the internet by any of your devices).
Now your "master phone" runs a simple server to synchronize data (just like an internet server would do). Every of the seven clients could receive the ip adress of you master by scanning a simple QR code or sending a short message and afterwards configure itself accordingly.
Have you checked Alljoyn?
As quoted:
"Developers can write applications for interoperability regardless of transport layer, manufacturer, and without the need for Internet access"
You can create a Wifi connection between your devices. Than after connection it creates local network between your devices. Inside this network you, of course, can interact between your devices using TCP/IP connection. It works both on Android and iOS. Simply lauch your app as server on the one device
EDIT
Note, you have to connect your devices using any network. It is possible to connect the devices by initializing your device as WiFi-router. It can be both Android and iOS. If it is possible, you can connect your devices to any wifi connection.
Than, launch your app as Server-socket, the others as clients.
for Android (java) server use this link:
https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
for android device client:
try (
Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));
)
The same idea is for iOS (Objective-C):
server
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html#//apple_ref/doc/uid/CH73-SW8
and client:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html#//apple_ref/doc/uid/CH73-SW4
A better way could be use Ble.
It's easiers to connect the phones because you don't need user confirmation.
Seems like you can connect up to 20 devices Maximum number of peripherals on CoreBluetooth?.
To transfer 500KB should require few minutes (may be between 2 and 5).
You can track an Android device without Internet via GPS.
Connection without The Internet:
SMS
USSD
DTMF (very slow)
How to design a tracking device on USSD is mentioned at
M2M IoT Cookbook
How to develop a device based on Wireless Wide Area Network modules
You also can use the Android phone as a data logger and store under the Micro SD Card and read the card by:
Replacing the SD card to your PC
Streaming the data local by Bluetooth
Forwarding the data at home by Wi-Fi
Or Possibly:
Your app can use SMS API to transmit the DATA or other SOURCES.

Is Wi-Fi P2P ideal for a local chat-discovery app(Tinder similar)?

I wanted to know if WiFi P2P android API is ideal for use if we take lot of connections into consideration, tha app's users can be at time clients and servers they can share files and send messages and get them also as clients ? if no what is the best solution ( N.B : i'm opened to client/server architecture ).
I just wanted to know the limits of the API cited above and Thank you guys ...
Wifi direct as a technology is ideal for peer to peer communication.However talking about wifi direct for android , it works well only for a single connection that is only 1-1 use case .It is possible to create a one to many where every device connects to the group owner (Soft AP).But even if u build a one to one and try extend to multi use case you will have problems with certain devices that do no connect perfectly.
Checkout some of the problems listed under problems after downloading this application https://play.google.com/store/apps/details?id=com.budius.WiFiShoot&hl=en
If you want to go for multiple connection it is better to use hotspot .One device ends up being the hotspot and other clients connect to the hotspot .It is being used by plenty of application like xender, zapiya.

Android to iOS connect via WiFi or using Multipeer connectivity or using WiFi direct

So I am developing an APP and I need to connect multiple android and multiple Iphone to send text data without any internet connection or service provider data network.
So one of the phone will have to act as a server to relay information between them. But the app will have to decide which phone will be the server and if a phone that is the server leaves the conversation then another phone will pickup as the server this will all be done with some smart programming but before I get there.
I know Android WiFi direct can do a one to many connection setup which makes it easy to connect android phone and accomplish the task between android phone only. But the problems comes when I need to connect Iphone with the android phones. Since the Iphone must be able to act as a server as well.
I would like to know a few things:
Can I connect Android and Iphone via WiFi Direct?
Can I connect Android and Iphone using Multipeer connectivity feature on Iphone?
Is there anyway to create a soft access point using Iphone? I know android can do this via WiFi direct feature.
If non of these can work can you suggest something.
From the discussion here it doesn't look possible
I wonder though if both OS allow enough control over the WiFi transceiver if you couldn't just write an app that could what you are asking and just bypassing the built in software architecture all together. I would think Bluetooth would be too weak except in extremely dense device saturation environments, but just for proof of concept that could be another route to take. My guess though is that you just wouldn't get that level of control over any of the radios inside a phone through the current OS.

Convert application using wifi to application using bluetooth

I have an android app, it does the following:
Connects with a server to read and update a database at the same time as others.
I want to convert this to an app that does not need an internet connection. Therefore i would like to know if its possible to have an android device acting as the server with the database, whilst multiple phones connect to it via bluetooth getting and updating the information in the database?
Thanks
Yes. It possible.
However all of your devices will have to be located nearby, so they can connect to each other through bluetooth.
You can take a look at Android Bluetooth API.
However, my recommendation would be to use Wifi instead of Bluetooth. YOu will need additional WiFi router. However, you won't need to deal with Bluetooth API in such case.
You will only need to write a server on one of Android device and the rest of devices will work the same (as now)
There are a few options to doing so, that don't involve a server. Both of them require a slightly different approach than both devices connecting to a server.
Wi-fi Direct- Only available with Android 4.0+.
Bluetooth
Personally, I have been using the Bluetooth option, and not found it terribly difficult. Essentially, you have to do the following to make it work.
Have one of the devices listen for a connection. If it is unpaired, you will have to make the device discoverable.
The second device needs to initiate a connection. It can do this by looking at the known devices and trying to connect to one, or listening for a new device
After the two devices connect, they must initiate some kind of a communication protocol. The communication is essential a serial connection.
Blue-tooth requires that the devices be within about 10 m of each other. Wi-fi direct will allow somewhat further, but as mentioned, is less supported. It is possible to allow for both communication methods, but is somewhat challenging.

Android Wifi direct multiple connection ad-hoc

I try to create an ad-hoc network between many Android devices, but I can't find documentation to do that.
I thought Wifi direct could work for what I want, but is it possible to have multiple connection on one device ? I saw that we could do a device acting like an access point and communicate with many devices, but can we communicate from one client device to two access point devices ?
I have for example 3 devices: A -> B -> C
The "A" device need to communicate with "C" device but he's too far away, so I want to have ad-hoc wifi direct connection between A&B and ad-hoc wifi direct connection between B&C, so A can communicate with C. It should also work with more than 3 devices.
Thanks !
Here is the link to the Wifi Direct Demo http://developer.android.com/resources/samples/WiFiDirectDemo/index.html
I have been working on establishing ad hoc communication over Android phones for a month. I am trying to create an app that could work without having 3G or access point. Main idea is to let the clients to be able transfer voice or data in a close range.
There are some guys implemented a basic system regarding almost the same topic. It basically works with Wi-Fi access point. So you need an access point in order to establish the connection. Although this is not what I am looking for, it might be helpful for others. You might need to contact them for more details.
https://sites.google.com/site/androidwalkietalkie/home
Android does not have support for ad-hoc WiFi at this time -- sorry!
Update (2019): Android has supported WiFi Direct for a while, and this answer has not been updated.
Your scenario would work. A would be able to connect with C as long as B (the group owner) is within range of both A and C.
See Android's developer documentation for more info:
https://developer.android.com/guide/topics/connectivity/wifip2p
At this time only Galaxy S 2 support Wi-Fi Direct
The wifi direct supports concurrent hosting and client capabilities on single interface. Which means, the device will act as an AP, allowing other devices to connect to it as clients, And also connect to another AP as a client. And can share internet connection by driving the internet traffic between the interfaces.
However, current implementations of wifi direct on android are specific to the phone and not part of andorid sdk. So there is not much clarity if they support concurrency. This capability is not limited by hardware.
Ad hoc wifi is disabled in Android. You need to ask Google why they have chosen to do so. If you have access to a device that allow you to install custom Android images, you will be able to patch Android to support it. Or you can root your device and reenable ad hoc Wifi, there are plenty of solutions on the web on how to enable ad hoc on rooted Android devices. This is a software issue, the hardware on every Android device that I know of has support for ad hoc Wifi.
The Serval Project, based in part on WiFi Tether, is the only place I've found this functionality. It's still alpha phase, but seems to be the forerunner in the field. The wiki is much more useful than the main project page linked above, with installation instructions and a bit of documentation.
Serval may or may not be right for your needs, but I felt it should be pointed out that ad-hoc networking is not impossible on Android. It might be better to wait until Google gets around to implementing it as standard API, though (if you believe they ever will :) ).
Well You might be interested in looking at AllJoyn. It is qualcoms SDK for android OS which includes wi-fi peer-to-peer implementations by creating adhoc connections. This is same as Wi-Fi Direct.

Categories

Resources