How I used 5 iDevices to play piano (MultipeerConnection framework)

Table of Contents:

  • How iOS Multipeer Connection works?
  • How to pass information between devices?
  • How to implement Multipeer Connection?
  • How to create a Piano with MPConnection?

How iOS Multipeer Connection works?

The Multipeer Connectivity framework uses a device wireless capabilities to detect nearby devices running the same framework, making communication between gadgets possible. So, it uses the Wifi and Bluetooth to exchange information between iPads and iPhones to create multiplayer apps or simply impress someone.

How to pass information between devices?

MultipeerConnection exemplification (Reference: WWDC 13)
  1. Alex is searching for a nearby person to talk.
  2. When Alex hears Marco calling, he replies to Marco with a connection request.
  3. Marco can accept or reject Alex's request. If Marco accepts the request, a pear-to-pear session between them is created, and now, they can both talk.

How to implement Multipeer Connection?

To implement a multipeer connection we need to use three delegates: MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate and MCSessionDelegate. If you read the last session you can guess what each delegate can do. So, create a class using Xcode and import MultipeerConnectivity framework. To manage our sessions and peers we need to instantiate some variables, like a session, a peerId (device identification), a service to keep the advertiser and a service to keep the browser.


With this delegate you can see MultiConnection services search in others devices. When the framework find other device, it will automatically invite the peer to connect. I suggest you create an extension of your class to keep things organized:


With MCNearbyServiceAdvertiserDelegate is possible to accept invitations from a browser. To keep things organized I created an array of pears, which each new pear is appended to. The most important line of the following code is invitationHandler(true,self.session), with this line you can create a logic to choose if the advertiser is going to accept or deny the invitation from the browser. I chose to accept all invitations from all browsers.


This final delegate is the most important part, because with MCSessionDelegate you can receive and send data from session. The best way to receive session data in your Controller is by implementing a protocol.

Controller Initialization

To finish our implementation we need to write the init, deinit and send methods. In the init method we initialize and start our services, like the advertiser, and the browser. With deinit we make the opposite, we stop our service to avoid problems with memory and unnecessary processing. And with send(string: String) we send our strings to other pears.

How to create a Piano with MPConnection?

Until now we have created our class to manage the pear-to-pear connection. So, we need to find a way to use this, right? I found the best solution for you, a piano :D.


Leonardo A. de Geus |




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ETL- Data Processing using Talend

How you can’t use Docker’s Go client

Ruby on Rails — Bounded contexts via interface objects

10 Cool Tips for Writing Robust Bash Scripts

OxidemusiC and the somewhat modern tech stack it uses.

Get Started with Apache Cassandra Database in the Cloud

How to Choose: Benefits of Using Goldilocks vs. Fairwinds Insights

Fix my bike: Use Daml to model a real life example

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Leonardo Alexandre de Geus

Leonardo Alexandre de Geus

More from Medium

SwiftUI and UIKit Compatibility Research at LinkAja!

Every iOS developer should know these Xcode tricks

Charts — SwiftUI framework for building charts in iOS 16

Build an iOS app with NotificationCenter