Multipeer Connectivity (part 1)

Peter FennemaiOS5 Comments

This is the first part in a series about the iOS  Multipeer Connectivity Framework. This series addresses the following question: Is it possible to set up a session between peers and exchange data without any user interaction? This way you can start ad hoc peer connections: All nearby peers that run the same app on the foreground are instantaneously connected, and can start exchanging data immediately.

I will explore this ad hoc peer connection scenario, and describe the issues that I encountered during implementation and testing. The series goes accompanied by a Github repository, so you can reproduce, verify or improve my experiments and conclusions as you like. You can download the sources associated with this post here.

This is not a beginners tutorial. If you are new to the iOS Multipeer Connectivity Framework I suggest that you read some tutorials that explain the basic usage scenario’s for multipeer connectivity before you continue reading.

Many of these tutorials describe the interaction between a peer that is advertising and another peer that is browsing. The browsing peer discovers the advertisers that are nearby. The browser presents the nearby advertisers to the user, and the user selects one of them, indicating that he wants to connect. The selected advertiser receives an invitation to connect and asks its user to confirm the connection. After the user has accepted, a session is established and the peers are ready to exchange data. The ad hoc peer connection scenario does the same, but without any user involvement.

In this part of the Multipeer Connectivity series I will introduce the test app that I am using. In the next parts I will zoom in on the issues that I encountered while implementing the ad hoc peer connection scenario and how they can be solved.

Multipeer Connectivity Test Application

The test application consists of 3 main concepts:

  • The PFLoggingConsole: Logs NSLog statements to the screen of a device. This way we can read NSLogs not only on the simulator, but on any device. Very convenient if you are testing with multiple devices.
  • The PFPeerNameViewController: Presents a screen that allows the user to provide a peer name for the device. By default the current name of the device is chosen. It is shown at first-time startup of the app.
  • The PFPeerConnector: This is where the multipeer connectivity logic is implemented.

Let’s see it in action!

At first-time app startup the PFPeerNameController is presented, and you’ll see the following screen:

PFPeerNameViewController

You can give the device the name you like or accept the default name. After pressing Done the name is saved and the PFLoggingConsole screen is presented:

PFLoggingConsole

The text on the console is produced by ordinary NSLog statements in the code.

I have written a separate post about the PFLoggingConsole. Take a look if you are interested, otherwise feel free to skip it, you don’t need it to understand the remainder of this post.

Multipeer Connectivity classes

Now that we’ve dealt with the configuration of peer names and presentation of logs to the user, let’s start talking about the real topic of this series of posts, multipeer connectivity.

The class PFPeerConnector contains all logic to connect peers. Let’s take a look at the header PFPeerConnector.h first:

The method connect is called by the PFAppDelegate when the app becomes active and disconnect is called when the app resigns active.

The implementation code in PFPeerConnector.m is shown below.

Assuming that you already have some experience with the basics of the Multipeer Connectivity Framework, a lot of this code will look familiar to you. You will recognise the MCNearbyServiceAdvertiser, MCNearbyServiceBrowser, MCSession as well as their delegates.

In line 3 you see the import statement

As explained earlier this will cause all NSLog output to be written to the device screen.

The connect() method creates a session, and starts the browser as well as the advertiser. The disconnect() methods neatly stops them, and calls disconnect on the session.

Now remember what this tutorial was all about: Is it possible to set up a session between peers and exchange data without any user interaction?

The implementation of this feature is in 2 snippets of code:

The first snippet is in the MCNearbyServiceBrowserDelegate, line 132

This method is called when a browser finds a peer. In most tutorials the app stores each peer that it has found and presents the peer names in a table view. In the tableview a user can select a peer that he wants to connect with. In our ad hoc peer connection scenario we want to refrain from this user interaction. We do not even want to present the peer to the user. So as soon as a browser has found a peer it invites the peer to connect. That’s exactly what happens in the snippet above.

Below is the second snippet copied from the MCNearbyServiceAdvertiserDelegate, line 117

This method is called when an advertiser has received an invitation from a peer. In most tutorials this call is used to present a dialog to the user, offering the possibility to accept or decline the invitation. In our scenario we do not involve the user, therefore we blindly accept the invitation, by calling the invitationHandler with YES.

Create a session between 2 devices

Now we know all about the test app, let’s take a look what happens when we connect 2 peers. I will use the simulator and my iPhone. Below you can see the output of the apps.

multipeer startup log

 

  1. The simulator starts first. The connect method starts the simulator-browser as well as the simulator-advertiser. There are no other peers around, so the simulator stays quietly in the connecting state.
  2. The iPhone launches the app. The connect method starts the iPhone-browser as well as the iPhone-advertiser.This time there is another peer around: the simulator.
  3. The iPhone-browser discovers the simulator on the network. It invites the simulator to connect.
  4. The simulator-advertiser receives the invitation and accepts it. At that moment there are no peers yet in its session.
  5. Based on the acceptation by the simulator-advertiser the iPhone receives a call that a session has been established with the simulator.
  6. The simulator now also receives a call that a session was established with the iPhone. Both peers now have a shared session and the can start exchanging data. So far so good …..
  7. …… but wait…… The simulator is still browsing for peers, and it discovers the iPhone. And it does invite the iPhone. But it already has a session with the iPhone, so why invite again? I would expect that the multipeer framework would recognise this situation and deal with it by not sending out a new invitation. Is this a bug? Or is it a feature for a use case that I am unaware of?
  8. Anyway….. The iPhone friendly accepts the invitation, and the result is that it still has a session with the simulator. Steps 7 and 8 seem like overhead, because they do not change anything in the state of both peers.
  9. The simulator receives a call that the session with the iPhone was no longer connected. Why? There is obviously something wrong here.

Conclusion

We are not able to setup a session between 2 peers without user interaction. One of the peers is almost immediately disconnected. At point 6 everything seems to be fine, but at point 7 the simulator continues to invite a peer that it was already connected with. In the next part of this series I will show a way to prevent this mutual invitation problem.”

5 Comments on “Multipeer Connectivity (part 1)”

  1. Is there any way to find nearby devices with wifi that doesnt run the same app? That is to discover all devices nearby in a public place.

  2. Thanks so much for publishing this, I have been having trouble enabling this for ages, all the other blog posts I have read have not helped, until this one. Legend!!!

  3. Pingback: Flatiron School Presents – The Multipeer Connectivity Framework (Lattice) | Xida Zheng

  4. Pingback: Flatiron School Presents – The Multipeer Connectivity Framework (Lattice) | Xida Codes iOS

Leave a Reply

Your email address will not be published. Required fields are marked *