Android – communicating between two devices-ThrowExceptions

Exception or error:

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?

How to solve:

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

###

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

###

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:

  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

Leave a Reply

Your email address will not be published. Required fields are marked *