← Back to list

How to Build a Mesh-Like Network on iOS with Swift, Bluetooth & MultipeerConnectivity

Mesh networking allows devices to communicate in a decentralized and resilient way — without requiring a central server or even an…

Enes Eken · 2025-05-12 14:23 · 1 claps · 3.7 min read
#mesh-networks #mesh-networking #bluetooth-low-energy #multipeerconnectivity #ios-development
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

How to Build a Mesh-Like Network on iOS with Swift, Bluetooth & MultipeerConnectivity?

Mesh networking allows devices to communicate in a decentralized and resilient way — without requiring a central server or even an internet connection. Although iOS doesn’t natively support full mesh networking like Bluetooth Mesh Profile, you can still build mesh-like behaviors using:

  • CoreBluetooth (BLE)
  • MultipeerConnectivity
  • 3rd-party frameworks (like Bridgefy and Berty)

Let’s explore how to simulate mesh networking on iOS.

What Is a Mesh Network?

A mesh network is a structure where every node (device) connects with other nearby nodes, forwarding data as needed. It enables:

  • Offline communication
  • Resilience (no single point of failure)
  • Range extension via message hopping

1. BLE Mesh-like Networking Using CoreBluetooth

Though Apple doesn’t support Bluetooth Mesh, you can build basic one-hop communication with CoreBluetooth.

import CoreBluetooth

class BLEMeshNode: NSObject, CBCentralManagerDelegate, CBPeripheralManagerDelegate {

    private var centralManager: CBCentralManager?
    private var peripheralManager: CBPeripheralManager?

    private let nodeName = "MeshNode"

    override init() {
        super.init()
        centralManager = CBCentralManager(delegate: self, queue: nil)
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
    }


    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        guard peripheral.state == .poweredOn else {
            print("Peripheral is not powered on")
            return
        }

        let advertisementData: [String: Any] = [
            CBAdvertisementDataLocalNameKey: nodeName
        ]

        peripheralManager?.startAdvertising(advertisementData)
        print("Started advertising as \(nodeName)")
    }



    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        guard central.state == .poweredOn else {
            print("Central is not powered on")
            return
        }

        centralManager?.scanForPeripherals(withServices: nil, options: nil)
        print("Started scanning for peripherals")
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
        guard let name = peripheral.name else { return }
        print("Discovered peripheral: \(name) with RSSI: \(RSSI)")

    }
}

This code works for basic Bluetooth proximity messaging and peer discovery. However, it does not implement full mesh networking or message relay. To simulate a real multi-hop mesh network, you will need to manually build logic for message propagation, device state management, and data routing between nodes.

2. True Peer-to-Peer: MultipeerConnectivity Framework

Apple’s MultipeerConnectivity framework provides seamless peer discovery and communication over Bluetooth, Wi-Fi, and peer-to-peer Wi-Fi.

Key Features:

  • Automatically discovers nearby devices
  • Handles session management
  • Transfers data, streams, and resources
  • Works offline
import MultipeerConnectivity
import UIKit

class MeshSession: NSObject, MCSessionDelegate, MCNearbyServiceAdvertiserDelegate, MCNearbyServiceBrowserDelegate {

    private let peerID = MCPeerID(displayName: UIDevice.current.name)
    private var session: MCSession?
    private var advertiser: MCNearbyServiceAdvertiser?
    private var browser: MCNearbyServiceBrowser?

    override init() {
        super.init()

        session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .required)
        session?.delegate = self

        advertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: "meshapp")
        advertiser?.delegate = self
        advertiser?.startAdvertisingPeer()

        browser = MCNearbyServiceBrowser(peer: peerID, serviceType: "meshapp")
        browser?.delegate = self
        browser?.startBrowsingForPeers()
    }

    func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
        print("Peer \(peerID.displayName) changed state: \(state.rawValue)")
    }

    func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
        print("Received data from \(peerID.displayName)")
    }

    func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {}

    func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {}

    func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {}


    func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID,
                    withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
        print("Received invitation from \(peerID.displayName)")
        invitationHandler(true, session)
    }


    func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
        print("Found peer: \(peerID.displayName)")
        browser.invitePeer(peerID, to: session!, withContext: nil, timeout: 10)
    }

    func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
        print("Lost peer: \(peerID.displayName)")
    }
}

MultipeerConnectivity doesn’t support multi-hop natively, but with clever routing logic, you can simulate forwarding messages to peers.

3. Third-Party Libraries for Mesh Networking

For more advanced, multi-hop communication without writing everything from scratch, use one of these libraries:

a. Bridgefy

  • SDK for offline Bluetooth-based mesh messaging
  • Automatically relays messages through nearby devices
  • Popular in disaster recovery apps and public events
  • Well-documented and widely adopted in commercial apps

b. Berty

  • SDK for fully encrypted offline messaging
  • Relays messages using Bluetooth, mDNS, and other protocols
  • Designed for privacy and censorship resistance
  • Open-source, based on peer-to-peer cryptographic principles

Use Cases for Mesh-Like Networking on iOS

  • Offline messaging apps
  • Multiplayer games without internet
  • Smart home coordination
  • Emergency communication systems
  • Education apps for classrooms without Wi-Fi

Limitations and Considerations

  • Apple limits BLE background usage
  • Multipeer supports ~8 peers simultaneously
  • Multi-hop is not native in Apple APIs — must be implemented
  • Battery usage and privacy must be handled carefully

Conclusion

Although iOS doesn’t natively support Bluetooth Mesh, you can achieve mesh-like behavior with CoreBluetooth, MultipeerConnectivity, or robust 3rd-party libraries like Bridgefy or Berty. Depending on your needs — proximity chat, offline relay, or smart IoT networks — you can choose the right approach.

Offline communication is possible. Mesh it up.

In today’s article, we explored how to build a basic mesh network using Bluetooth and MultipeerConnectivity in Swift.

Thank you for reading


메타데이터
post_id
8e01ebfe41bf
slug
how-to-build-a-mesh-like-network-on-ios-with-swift-bluetooth-multipeerconnectivity-8e01ebfe41bf
url
https://medium.com/@eneseken85/how-to-build-a-mesh-like-network-on-ios-with-swift-bluetooth-multipeerconnectivity-8e01ebfe41bf
canonical_url
https://medium.com/@eneseken85/how-to-build-a-mesh-like-network-on-ios-with-swift-bluetooth-multipeerconnectivity-8e01ebfe41bf
author_url
https://medium.com/@eneseken85
status
ok
fetched_at
2026-09-05 13:46:36