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!
In peer-to-peer (P2P) networking, a group of computers are linked together with equal permissions and responsibilities for processing data. Unlike traditional client-server networking, no devices in a P2P network are designated solely to serve or to receive data.
Wi-Fi Direct (also known as peer-to-peer or P2P) allows your application to quickly find and interact with nearby devices, at a range beyond the capabilities of Bluetooth. The Wi-Fi Direct (P2P) APIs allow applications to connect to nearby devices without needing to connect to a network or hotspot.
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);
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With