'Can we send data from an android device to another android device directly (p2p) without server in the middle?

I need to send data from one android device to another android device directly(p2p) after they find each other through a server. I read some stuff that this can't be done but I don't understand why a device that can access the internet (it has an IP right?) can not create a peer to peer connection? If this is not possible why? and if it is how?

P.S : I know without internet we cannot achieve that,the question presumes that both targets have internet connection, either gprs/3g/4g or wifi internet connection!

Note : I want this to be possible both on wifi and non-wifi internet!



Solution 1:[1]

I have had success in the past using PubNub for this case. I used similar techniques when building Mote.io.

I have always thought it was dumb that phones are not capable of easy peer to peer connection, but you can emulate this sort of communication with a third party in-between. The advantage of using a hosted service like PubNub is that it works on Wifi and cell networks and is capable of dealing with poor or dropped connections.


Java SDK

You can use the PubNub Android SDK for android to subscribe and publish to channels on the PubNub network. All the networking is taken care of, your only concerns are channels and messages.

The following example is taken from the Android SDK tutorial found here.

SDK Screenshot


Subscribe

This is where the fun begins! If we want to receive messages from a channel we need to subscribe to a channel. To do this all we need to do is use the subscribe() method and provide a channel name. We’ll use hello_world for channel name.

We also need to define a callback object which will be passsed as parameter to the subscribe() method. Methods from callback object will be called on events like message receipt, error, connect, disconnect, reconnect:

try {
  pubnub.subscribe("hello_world", new Callback() {

      @Override
      public void connectCallback(String channel, Object message) {
          Log.d("PUBNUB","SUBSCRIBE : CONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void disconnectCallback(String channel, Object message) {
          Log.d("PUBNUB","SUBSCRIBE : DISCONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      public void reconnectCallback(String channel, Object message) {
          Log.d("PUBNUB","SUBSCRIBE : RECONNECT on channel:" + channel
                     + " : " + message.getClass() + " : "
                     + message.toString());
      }

      @Override
      public void successCallback(String channel, Object message) {
          Log.d("PUBNUB","SUBSCRIBE : " + channel + " : "
                     + message.getClass() + " : " + message.toString());
      }

      @Override
      public void errorCallback(String channel, PubnubError error) {
          Log.d("PUBNUB","SUBSCRIBE : ERROR on channel " + channel
                     + " : " + error.toString());
      }
    }
  );
} catch (PubnubException e) {
  Log.d("PUBNUB",e.toString());
}

Publish

Now its time to find our voice. Its time to send a message to everyone around the world subscribed to channel demo. First we’ll subscribe as we did in Step 3 only this time we’ll specify channel as demo. Once we are subscribed to the channel all we need to do is use the publish() method. We’ll specify the channel name, the message we’d like to send. We’ll also include a callback object.

Callback callback = new Callback() {
  public void successCallback(String channel, Object response) {
    Log.d("PUBNUB",response.toString());
  }
  public void errorCallback(String channel, PubnubError error) {
  Log.d("PUBNUB",error.toString());
  }
};
pubnub.publish("demo", "Hello World !!" , callback);

Solution 2:[2]

u dont need to have internet u can do it on local area network ,t transfer data between two android devices the devices must be connected to same local area network and then on one device u have to create a server socket and on other device u have to create a client socket.in this way two devices can communicate with each other u just need to have a wifi router to create a local area network .

Solution 3:[3]

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 and android-communicating-between-two-devices

Solution 4:[4]

you have to use push notification methodology. no without internet you can not achieve it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ian Jennings
Solution 2 Mohammad Shayan
Solution 3
Solution 4 java seeker