问题
I am working with Multipeer connectivity. It works fine when all devices are connected to same wifi.
I have 1 master and 5 slaves devices . I want to do connectivity with personal hotspot but everytime the connection will declined.
This is my code :
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")
}
}
来源:https://stackoverflow.com/questions/51593275/multipeer-connectivity-with-personal-hotspot-not-working