Following the official programing guide,I want to make a multi-device ARkit app using multipeer connectivity.
However,after advitiser's and browser's function,the session cannot transmiss data to the other peer(I used an Iphone and an Ipad in the same wifi.)
The simplified code are followed.
import UIKit
import ARKit
import SceneKit
import MultipeerConnectivity
var serviceadvtiser:MCNearbyServiceAdvertiser!
var servicebrowser:MCNearbyServiceBrowser!
var mypeerid:MCPeerID!
var oppionentID:MCPeerID!
var sessionclient : MCSession!
var worldmap:ARWorldMap!
class SetAnchor:UIViewController,ARSCNViewDelegate,ARSessionDelegateMCSessionDelegate,MCNearbyServiceAdvertiserDelegate,MCNearbyServiceBrowserDelegate{
override func viewDidLoad() {
super.viewDidLoad()
.....
arview.session.run(configuration)
}
func session(_ session: ARSession, didUpdate frame: ARFrame) {
if currentMode == .shuangrenshijing_host && !ishostset{
ishostset=true
oppionentID = nil
mypeerid=MCPeerID(displayName: UIDevice.current.name)
sessionclient=MCSession(peer: mypeerid, securityIdentity: nil , encryptionPreference: .optional)
sessionclient.delegate=self
serviceadvtiser=MCNearbyServiceAdvertiser(peer:mypeerid,discoveryInfo: ["ip":getIFAddresses()[0]],serviceType:servicename)
serviceadvtiser.delegate=self
serviceadvtiser.startAdvertisingPeer()
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(300)){
while self.arview.session.currentFrame!.worldMappingStatus == .limited||self.arview.session.currentFrame!.worldMappingStatus == .notAvailable{}
self.arview.session.getCurrentWorldMap {worldMap,error in
worldmap=worldMap}}}
else if currentMode == .shuangrenshijing_client && !isclientset{
isclientset=true
oppionentID = nil
mypeerid=MCPeerID(displayName: UIDevice.current.name)
sessionclient=MCSession(peer: mypeerid, securityIdentity: nil , encryptionPreference: .optional)
sessionclient.delegate=self
servicebrowser=MCNearbyServiceBrowser(peer: mypeerid, serviceType: servicename)
servicebrowser.delegate=self
servicebrowser.startBrowsingForPeers()
}
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
servicebrowser.invitepeer(peerID)
oppionentID=peerID
}
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
invitationHandler(true,sessionclient)
oppionentID=peerID
while map == nil{}
guard let map=worldmap
else{return}
print(1001)
guard let data = try? NSKeyedArchiver.archivedData(withRootObject: map, requiringSecureCoding: true)
else{return}
print(1002)
try! sessionclient.send(data, toPeers: [oppionentID], with: .reliable)
isclientjoin=true
print(1003) }}//get Eroor when execute "try! sessionclient.send(data, toPeers: [oppionentID], with: .reliable)"peers not connected
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {?
if let worldMap = try! NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: data) {
print("WorldMap") //never be executed
}
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 session(_ session: MCSession, didReceiveCertificate certificate: [Any]?, fromPeer peerID: MCPeerID, certificateHandler: @escaping (Bool) -> Void) {
}
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {}}
Xcode give following warning:
ICEStopConnectivityCheck() found no ICE check with call id
ProcessEvent:1199 Send BINDING_REQUEST failed