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, designed around the concept of equality of its nodes (called “peers”), where each node 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.

108760,xcitefun-bluetooth-logo-ss-250-crop380w
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. This problem was caused by certain iOS restrictions: it was impossible to 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.
1
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.

2_fix

So we had to develop our own protocol which allows P2P network creation using Bluetooth, supports the data exchange without breaking connection, and allows 6 devices to be connected to the network simultaneously.

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

3
Discovering devices within a network

The first step in building such a network is discovery.  Each devices connected to the network should be able to quickly discover devices in his close vicinity which are ready to connect. These discovered devices are then used 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):

0004

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

Apple Developers Resources

4
Using 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’ve used the data exchange mechanism (via Bonjour service) which starts on every device  and reports it device info. At the end of this process we obtained a list of all  detected devices, and could tell which of them had our app running.

 

Maintaining and updating the list of discovered devices

5_fix
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  and 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. In this algorithm we used a system of multiple pings to validate if the device is truly went out of network or not. If after several pings it doesn’t responds, it is removed 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.