Smarter ideas worth writing about.

Android-iPhone Peer-to-Peer Communication

 

Recently, we were faced with an interesting and challenging requirement while developing a mobile application. We will not go into project details here, but the solution required multiple mobile devices to communicate with one another in a peer-to-peer fashion. Also, these devices would always be in the same physical location. Picture four people sitting at a table, each interacting with the application on his/her own mobile device, with data being communicated between application instances.

As most people know, iOS does not support near field communication (NFC), so getting Androids and iPhones to talk to one another is not trivial. Maybe its time they get to know each other a little better by getting them both to open up and have a conversation…

Our goal for this project was to make cross -platform communication easy without the overhead of a client-server model. Devices should be able to easily recognize each other and send and receive messages lightning-fast. In the absence of NFC and the overhead of client-server polling, we went through an intermediary – in this case, a WIFI access point.

To pull this off, we had to utilize some lower-level networking APIs that are supported by both Android and iOS. The User Datagram Protocol (UDP) is a core protocol of Internet communications, much like HTTP, TCP, FTP, etc. The User Datagram Protocol (UDP) allows devices to send and receive messages without having to setup complicated communication channels.

Achieving cross platform communication between iOS and Android devices over a peer-to-peer (P2P) network has its own challenges.  These challenges range in difficulty from data types to communication barriers. 

 

iOS

To utilize UDP, the iPhone requires the use of the base UNIX/Darwin OS that underpins iOS. We developed the communication endpoint using the C-based language, which allows for broadcasting UDP messages to users of the application. The base networking functionality uses socket connections that allow devices to listen on the network and subnet. By manually developing the networking data protocols, we were able to ensure only the application’s data is sent and received between devices, allowing it to filter out any unwanted data.

 

Android

In Android, broadcast messaging is filtered out by default. As a result, a low-level API must be utilized to expose the network data to the application. Android’s WifiManager object must request a multicast lock on the WiFi interface in order to receive multicast UDP packets. In order to keep the UI responsive, sending and receiving network data is handled on a separate thread running in the background as an Android Service.

 

Our main challenge was designing a data protocol that the application can understand independent of the mobile operating system.  Once the development challenges were tackled, the application could send and receive datagram messages broadcasted on a WIFI access point. This allows data to be passed to and from different platforms with easy of use and provides quick transfer of data.

Few if any applications allow for cross platform compatibility, let alone the same application interacting without a server. Getting two devices from different platforms talking with one another is no easy task. However, making this work opens the door up to many new opportunities for mobile phones.

Share:

About The Author

Steven is a developer in Cardinal's Cincinnati Mobile Practice. He has experience with iOS, Android, as well as the Microsoft platform.