Zishan Neno
Zishan Neno

Reputation: 2817

Android - communicating between two devices

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?

Upvotes: 30

Views: 63432

Answers (8)

SciVegan KCPM
SciVegan KCPM

Reputation: 1

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

Upvotes: 0

InnisBrendan
InnisBrendan

Reputation: 2249

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:

  1. Use Network Service Discovery to find devices running your app
  2. Connect to other instances of your app using a socket

For a complete tutorial you can check this out

Upvotes: 0

luckyhandler
luckyhandler

Reputation: 11329

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.

Upvotes: 0

Zvika
Zvika

Reputation: 1280

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.

Upvotes: 24

Niraj Adhikari
Niraj Adhikari

Reputation: 1728

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

Upvotes: -1

shohreh
shohreh

Reputation: 526

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?)

Upvotes: 3

Derek Harrington
Derek Harrington

Reputation: 467

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

Upvotes: 18

Igor
Igor

Reputation: 33963

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.

Upvotes: 2

Related Questions