28

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?

John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • Bluetooth? no? http://developer.android.com/guide/topics/wireless/bluetooth.html – Aidanc Feb 18 '11 at 12:47
  • 2
    No, assume they are distant from one another. – John J Smith Feb 18 '11 at 20:03
  • @ChrisF - this is not a duplicate of that question, as the other question is specific t "ad-hoc" wifi networking, while this question has no such restriction, and has answers which address other network modes such as 3g. – Chris Stratton Mar 06 '14 at 21:19
  • Google Games API claims to use peer-to-peer connections for realtime games. I would like to how it works inside too. – Dzmitry Lazerka Sep 14 '14 at 04:11

4 Answers4

22

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.

M4N
  • 94,805
  • 45
  • 217
  • 260
Ollie C
  • 28,313
  • 34
  • 134
  • 217
6

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:

http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/

If you also need peer discovery that will make the thing more difficult.

ldx
  • 3,984
  • 23
  • 28
4

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.

Phil Haigh
  • 4,522
  • 1
  • 25
  • 29
  • how about without server itself @Gopal? Would it be possible from android ! to Android B using their own WIFI access. Not the server but instead direct Wifi. – gumuruh Apr 24 '14 at 07:56
  • @gumuruh yes this is certainly possible on your own network, I've written a prototype app that uses UDP to discover other Android devices running the same app and go through a 'pairing' process; once paired the app allows one device to use hardware on the other - for example an app on a tablet sending text messages using a phone it has been paired with. As long as UDP is permitted on your subnet you can do this with no problems. Although it is complex to set up. – Phil Haigh Apr 24 '14 at 12:39
  • waw! That's great! Where could i read the short tutorial for it, @Phil ? I'm curious if the checking of Android A could detect the Android B whether they're running the same App or not. It's interesting. – gumuruh Apr 25 '14 at 07:10
  • @gumuruh there's no 'short tutorial' because it is, actually, quite complex. The brief explanation is that to 'pair' you have device A send a broadcast and other devices listen for and acknowledge it. In this way you can detect all devices on your network that are running this app and are currently in "listening mode". This is the first of several (non-trivial) stages of building peer to peer functionality. – Phil Haigh Apr 25 '14 at 09:56
  • alright then. I got the main idea, once the device is broadcasting its availability. Then the other device could capture the status right? I see, i see... where do you get the 'quite long tutorial' for it, @Phil? – gumuruh Apr 28 '14 at 02:12
  • @gumuruh There isn't one... I spent a lot of time working it out the hard way. – Phil Haigh Apr 29 '14 at 09:33
  • so what we have here ... if your devices are in different network and want to communicate. you have 2 options . one is use a server and another is use google push . right ? there is no another way for them to communicate directly ? – Sagar Nayak Mar 09 '16 at 06:14
  • @SagarNayak If you have total control of the network path between the two devices, then they could communicate directly provided the network(s) have been configured to permit it. If you don't then by and large it isn't going to work and that's why you need a server in the middle with a predictable address (domain name) that they can both talk to. – Phil Haigh Jun 08 '18 at 09:13
1

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.

Community
  • 1
  • 1
John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • Since asking this question and researching some of the answers provided, I have decided to implement Google's C2DM. I hope this is the right decision as it is currently still in 'Labs' and could potentially be withdrawn. – John J Smith Jul 30 '11 at 11:24