can anybody confirm what are the currently allowed methods for peer-to-peer communications within the Android framework? I need to transfer json strings and I’m currently using SMS which works ok but the problem is that the data also ends up as lots of text messages. I’ve read Reto Meier’s first edition of Professional Android Application Development where he says that the data transfer options were not implemented due to security concerns.
Has this changed at all and how would you do peer-to-peer transfer of data?
Have you looked at Qualcomm’s AllJoyn library? It is designed to work over Bluetooth or wifi, so may fit, though if you’re connecting over 3G or wider range networks it won’t work.
Given the variation and reliability of networks between two remote devices not on the same network, I’d question whether peer-to-peer is the best solution, and would venture to suggest considering using an application server in between, so you could then use Cloud to Device Messaging [deprecated] (perhaps in tandem with Google App Engine). i.e. to send a message the sender passes it to the server, and the server then passes it on to the recipient.
In theory all devices on the net have a unique IP address and can talk to each other, but it’s rarely that simple as routers/firewalls are configured differently so you’d need to pay great attention to the ports you use, especially considering many inbound ports are blocked by default for security reasons.
You can simply use UDP/TCP sockets. In a separate thread you set up the server-side listener socket and that’s it. Of course your application has to be started first (or should run in the background all the time). Here’s an example:
If you also need peer discovery that will make the thing more difficult.
You should also take a look at peerdroid, an open source project available here. I’ve been looking in to peer communication options from the point of view of having a collection of federated devices (pre-paired, if you like, similar to Bluetooth pairing); this library looks like it may give you the foundation for what you are trying to do.
If you are on your own network (for example a home or office WiFi) then you should be able to query for other connected devices. If the network you are on is not under your control (the mobile network or a public wifi) then the network will have been configured to isolate each device from everything else. In this case you will have no choice but to put a server up to act as the man in the middle. That brings its own architectural compromises – every device has to regularly poll the server or keep a connection open – unless you use Google App Engine which supports push notifications over Google’s own infrastructure.
Thanks for your answer ldx but I would need peer discovery as you indicated. Some further research appears to indicate XMPP as a suitable technology and there are now some services on offer, although these appear to be aimed at ‘server’ to client notifications. There is a good discussion here on XMPP and some more here although it would appear that there are still some issues to deal with such as polling v push, long-running open http connections and battery life. Xtify looks promising, especially their web service. I hope this provides suitable information to others looking at the topic of peer-to-peer data communication.
Try PushSharp. A server-side library for sending Push Notifications to iOS (iPhone/iPad APNS), Android (C2DM and GCM – Google Cloud Message), Windows Phone, and Windows 8 devices.