个人热点的多人连接 Swift

Multipeer Connectivity With Personal Hotspot Swift

我正在使用 Multipeer 连接。当所有设备都连接到同一个 wifi 时,它工作正常。

我有 1 个主设备和 5 个从设备。我想连接个人热点,但每次连接都会被拒绝。 我想使用个人热点将所有从设备与主设备连接起来,以便在它们之间共享数据。

这是我的代码:

class SessionManager: NSObject, MCSessionDelegate, MCBrowserViewControllerDelegate, MCNearbyServiceAdvertiserDelegate, MCNearbyServiceBrowserDelegate {

    func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String: String] ? ) {
        browser.invitePeer(peerID, to: mcSession, withContext: nil, timeout: 10)
    }

    func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
        print("Lost Peer")
    }


    static
    let shared = SessionManager()

    var peerID = MCPeerID(displayName: UIDevice.current.name)
    var mcSession: MCSession!
        var mcAdvertiserAssistant: MCAdvertiserAssistant!
            var delegate: SessionDelegate!
                var serviceBrowser: MCNearbyServiceBrowser
    var serviceAdvertiser: MCNearbyServiceAdvertiser

    override init() {
        serviceBrowser = MCNearbyServiceBrowser(peer: peerID, serviceType: "demo")
        serviceAdvertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: "demo")

        super.init()

        peerID = MCPeerID(displayName: UIDevice.current.name)
        mcSession = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .required)
        mcSession.delegate = self

        serviceBrowser.delegate = self
        serviceBrowser.startBrowsingForPeers()
        serviceAdvertiser.delegate = self
        serviceAdvertiser.startAdvertisingPeer()

    }


    //MARK: Start Hosting
    func startHosting() {
        mcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "demo", discoveryInfo: nil, session: mcSession)
        mcAdvertiserAssistant.start()
    }

    //MARK: Join Session
    func joinSession() {
        let mcBrowser = MCBrowserViewController(serviceType: "demo", session: mcSession)
        mcBrowser.delegate = self
        mcBrowser.navigationController ? .navigationBar.backgroundColor = .black
        UINavigationBar.appearance().tintColor = .black
        UIApplication.visibleViewController.present(mcBrowser, animated: true)
    }

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

    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 browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
        UIApplication.visibleViewController.dismiss(animated: true, completion: nil)
    }

    func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
        UIApplication.visibleViewController.dismiss(animated: true, completion: nil)

    }

    func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
        switch state {
            case MCSessionState.connected:
                print("Connected:SessionManager \(peerID.displayName)")

            case MCSessionState.connecting:
                print("Connecting:SessionManager \(peerID.displayName)")

            case MCSessionState.notConnected:
                print("Not Connected:SessionManager \(peerID.displayName)")
        }
    }

    func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
        let sessionData = SessionModel.init(session, peerID, data)
        delegate.didRecieveSessionData(sessionData)
    }

    func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data ? , invitationHandler : @escaping(Bool, MCSession ? ) - > Void) {
        print("Receive Invitation")
        invitationHandler(true, self.mcSession)
    }
    func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) {
        print("Ddn't Start")
    }


}

有什么办法可以做到这一点

如有任何帮助,我们将不胜感激。

之前已经问过(无果):Multipeer Connectivity with personal Hotspot not working

据我所知,MPC 只是不同网络方式之上的抽象层。 Apple 使用 M-DNS (Bonjour) 和包括蓝牙在内的其他技术使 macOS / iOS 设备之间的网络尽可能简单。

多点连接是一种点对点解决方案,而您所描述的更像是一种客户端-服务器解决方案。这并不意味着您无法完成它,但最好记住这一点!这意味着您希望将 MPC 作为网络的基本级别,并在其之上实现客户端-服务器结构。我看不出为什么在使用其中一个设备作为热点时不可能这样做,但我认为甚至不需要这样做:Airdrop 也使用 MCP,并且当设备不在同一网络中时它也可以工作。

这方面有一些宝贵的资源: