Android - Communicating Between Two Devices

Android - communicating between two devices

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

Is it possible to communicate between two android phones?

Yes, two applications can talk over the internet (or bluetooth or sms/mms for that matter, but less practical). Just like any two computers can communicate over the internet. There's a wide variety of protocols. A whole world really. Do you need references to some tutorials on networking or am I totally missing your question?

EDIT: You can try The TCP/IP Guide for an intro

EDIT: And for android you probably want to use their socket interface

UPDATE: About how to communicate between clients - you probably don't want to rely on knowing a particular peer's IP address. Usually what you do is work with some known host as a server, which coordinates between the peers, sometimes relaying the messages between them, or alternatively just notifying the clients about each other's addresses.

android communicating two apps in separate devices

Connecting over networks that accept incoming socket connections

The usual way to do this between Android devices (or between any peer devices) is to use sockets.

You set up one or both devices to 'listen' for connections on a socket and then accept a connection from the other when they want to communicate (or you can have a dedicated client and server and the client always initiates the connections).

Once the connection is established you can send messages back and forth.

There are many examples of Android client server socket applications, but one I found useful was:

  • Android Server/Client example - client side using Socket (and its companion server side blog article - link included in the client blog)

Note that you may have to add your own 'protocol' on top of this - for example if you are sending a file of unknown length without any special 'end' character, you may want to add a byte (or several byte to represent an int, long etc) at the start to indicate the length of the transmission so the receiving side knows when it has received everything (or that it has not received everything in case of an error).

Connecting over networks which do not allow incoming connections (e.g. most 3G/4G)

In these scenarios, while there is nothing theoretically stopping sockets working, in practice many mobile operators will not allow incoming socket connections. In addition you would need to find the public IP address of the Mobile, which is possible but is extra complexity. If your solution will only ever run on a single operators network you can experiment and see if it works, but if not you may find it better and easier to use a server in the 'middle':

  • Device A connectes to server
  • Device B connectes to server
  • Device A asks server for addresses of connected devices and 'discovers' device B
  • Device A send a message for device B. It actually sends the messages to the server with an indication that it is to be sent to device B
  • The server notifies device B that a message is available for it (using some sort of message notification like Google Cloud Messaging for example, or simply by the devices polling regularly to see if they have any messages).
  • Device B retrieves the messages from the server

The above will work on pretty much any network that allows connectivity to the internet. It does have the disadvantage of requiring a server but it is likely a necessary approach over most mobile networks.

Communicating directly between two mobile devices

If you want to use general wireless module, like GSM, UMTS, LTE you need to provide standalone hardware with implementation of desired protocols. For GSM you could look into OpenBTS http://openbts.org/ which is free software.

As other communication possibilities, you can use Wi-Fi Peer-to-Peer http://developer.android.com/guide/topics/connectivity/wifip2p.html for establishing wifi connectivity.

Communication abilities are based on a chip's specifications, not all wi-fi support this kind of connection. On the other hand you can also establish Bluetooth connectivity.

How to communicate between two android device connected to same network but without having internet access

Yes it is possible you can achieve this by two way .

1) NSD (Network Service Discovery) :- NSD allow users to identify other devices on the local network that support the services your app requests. Using nsd service you can discover the peer on register on same network. for nsd you can follow below link
enter link description here

2) Wifi direct :- The Wi-Fi peer-to-peer (P2P) APIs allow applications to connect to nearby devices without needing to connect to a network or hotspot .
Some feature given below:-

Wi-Fi Direct supports WPA2 encryption. (Some ad-hoc networks support only WEP encryption.)

Devices can broadcast the services that they provide, which helps other devices discover suitable peers more easily.

When determining which device should be the group owner for the network, Wi-Fi Direct examines each device's power management, UI, and service capabilities and uses this information to choose the device that can handle server responsibilities most effectively.

link below for wifi direct enter link description here

So you can use both framework for network discovery and after that for sending file use java socket .

Make Socket server and other client socket connect them using ip found by nsd or wifi direct and after that you can broadcast.



Related Topics



Leave a reply



Submit