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!
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.
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.
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());
}
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);