0
votes

iOS Multipeer Connectivity question...

My app uses MCNearbyServiceBrowser and MCNearbyServiceAdvertiser (but not simultaneously on a given device).

My MCNearbyServiceAdvertiser always uses the same PeerId ... I store it in NSUserDefaults, per the 2014 WWDC session's advice on this.

When another device is browsing for services, the browsing device gets a foundPeer browser delegate callback, as expected.

However, if on the browsing device I switch away from my app (e.g., via a Home button tap) and then switch back to my app, I get another call to foundPeer for the advertising device, but this time the PeerId is different!

This seems odd, because my advertiser always uses the same PeerId.

Any ideas why this might be happening? Unexpected?

(I was planning to see if a newly-found advertising device with a given PeerId is already in my table view of advertisers, but the above issue kind of messes up that plan.)

Thank you.

-Allan

3
How do you check that the peerID is different? Is the peerID.displayName also different? - Peter Fennema
In browser:foundPeer:withDiscoveryInfo: I get the peerID of the found advertiser. I keep an array of NSDictionary...each dictionary includes the peerID of a found advertiser. If browser:foundPeer:withDiscoveryInfo: gets called with a peerID that is already in my table, I ignore it. But if I tap the home button on my browsing device, then switch back to my app, browser:foundPeer:withDiscoveryInfo: gets re-called for the same advertising device, but the peerID parameter has a different value than before. The peer name is the same, but two devices might have the same peer name. - lagouyn
How are you creating PeerID displayName values? Does the mystery PeerID match your format? MPC has a habit of discovering devices that are long-gone, especially on the simulator. - ChrisH

3 Answers

0
votes

From the apple docs: “ The Multipeer Connectivity framework is responsible for creating peer objects that represent other devices.”

After pressing the home button and switching back to the app the framework has created a new PeerID object to represent the advertising device. This is another object than the previous one, even though it represents the same advertising device. So you can not rely on PeerID object equality.

0
votes

To identify peer correctly I suggest you should create an NSUUID string and archive it on disk and reuse it. When you initialise MCPeerID object the display name you should be passing would be displayName+UUID. Use display name for UI elements and UUID for identifying peer.

I hope it helps.

0
votes

You will not get the same MCPeerID when you create two from the same display name. This ensures uniqueness when you have a name collision. It's common to use the device name as the display name. Not everyone personalizes theirs.

If you want to recognize and be recognized by previously connected peers, then you must save and retrieve the actual MCPeerID.

To see what I mean, paste the following code into a playground and run it.

import MultipeerConnectivity

let hostName = "TestPlaygroundHostName"

let firstPeerID = MCPeerID(displayName: hostName)
let secondPeerID = MCPeerID(displayName: hostName)

firstPeerID.hashValue == secondPeerID.hashValue