Blog: Building P2P networks with the help of Bluetooth on iOS and Android devices

In this article we will talk about the challenges and solutions around building a peer-to-peer network for iOS and Android mobile devices using Bluetooth. This experience is based on one of the recent projects of our mobile team.

Main idea

A peer-to-peer or P2P network is not a new concept. It has been beautifully implemented in some of the very famous products like Napster, Kazaa, Skype to name a few… In this article we will present some technical aspects and challenges of what it takes to build a mobile p2p network over modern mobile devices using iOS and Android.

A P2P network is a decentralized computer network. It is designed around the concept of equality of its nodes (called “peers”). Each node in a P2P network acts both as a client and a server. Often this type of network does not have central servers, and each “peer” acts both as a client and a server. As opposed to the client-server centralized architecture, P2P system allows to maintain the network performance and operational efficiency while using any number and combination of the available nodes.


Advantages of a P2P network

Building a P2P network on mobile devices over Bluetooth can be particularly useful in several cases:

  • when you need to create a network outside Wi-Fi or mobile network coverage zones (subway, away from populated areas, or in other cases when other wireless communication services are not available)
  • when creating games for mobile devices (P2P allows playing with more than 2 partners at the same time)

One of the obstacles we faced when working on the project was the difficulty to connect iOS and Android devices over Bluetooth. Certain iOS restrictions cause this problem. Simply put, the application can’t work with Bluetooth on the low level. At the same time the high level Game Kit Framework available to developers did not support multiple connections.
Our goal for this specific project was to create a p2p network that would support various data exchange technologies. As for Bluetooth, we’ve decided to connect iOS and Android devices separately.
The standard Bluetooth pairing tools (Bluetooth API – Android, Game Kit Framework – iOS) allowed us to easily pair 2 devices for data exchange purposes.

However, if we want to connect devices 2 and 3, we’ll have to first break the connection between devices 1-2 and 3-4 as shown below.


So we had to develop our own protocol which allows P2P network creation using Bluetooth. This protocol should also support the data exchange without breaking connection. And it should allow for connecting 6 devices to the network simultaneously.

In such network  each device can exchange data with other devices in real time. When the user adds a new device to the network, the connection doesn’t break.


Discovering devices within a network

The first step in building such a network is discovery. All devices connected to the network should be able to quickly discover devices in his close vicinity which are ready to connect. Then we use these discovered devices to form a p2p network and to accept data transfer requests.  Making a fast discovery process turns out to be a non-trivial task at all, because the standard methods of discovering and connecting devices require manual involvement from the user:

  • discover devices in the device Settings;
  • select 1 device to connect to;
  • approve connection.

Another key requirement for a new protocol was the ability to discover and transfer data not to a single device but to all the devices available within Bluetooth range (20-30 meters).


In order to solve the task we used Bonjour technology (for iOS):


Bonjour enables automatic discovery of devices and services on a local network using industry standard IP protocols. It is also known as zero-configuration networking. Bonjour makes it easy to discover, publish, and resolve network services with a sophisticated, yet easy-to-use, programming interface. This interface is accessible from Cocoa, Ruby, Python, and other languages.

Apple Developers Resources

4Using Bonjour allowed us to minimize user’s participation in the work process of the app – now the user simply needed to enable Bluetooth in the Settings.

So, it became critical to obtain and collect device information for all devices and keep them available all the time. To achieve this we used the data exchange mechanism via Bonjour service. This mechanism starts on every device and reports its device info. At the end of this process we obtained a list of all  detected devices. So, we could tell which of them had our app running.


Maintaining and updating the list of discovered devices

After device discovery step is complete, the next key task is to maintain a correct list of devices. Wireless devices can come and go out of reach as they go to a sleep mode (for energy saving purposes) or leave the range of  coverage. So, the simplest solution would be to frequently ping all previously discovered devices to make sure they are alive and ready to receive data. However, too frequent pings will slow down devices. Moreover, the search for all nearby devices could take up to 15-30 sec in the network.

So we had to come up with a device-pinging algorithm which allowed us to get information on the devices availability while preventing them from hibernating. We used a system of multiple pings in this algorithm. It helps validate if the device is truly went out of network or not. If the device doesn’t respond after several pings, you should remove it from the “discovered list”.

P.S. In the next article we’ll talk more about data exchange оver Bluetooth connection which was established using a method described in this article.