How would one detect multiple devices with Nearby Interaction?

The sample code provided only allows a connection of up to one other device. I was wondering how would one create multiple sessions in Swift, how to handle different devices and their inputs, and how many sessions is the U1 chip capable of handling. Thank you!

Accepted Reply

All NISessions are peer-to-peer and as a result, creating multiple NISession objects is necessary to have multiple concurrent sessions.

One approach is to create a dictionary between an identifier for the peer (i.e. a user identifier provided by your app MyAppUserID) and the NISession objects while also keeping track of the NIDiscoveryToken for each peer identifier:

Code Block swift
var sessions = [MyAppUserID: NISession]()
var peerTokensMapping = [NIDiscoveryToken: MyAppUserID]()


The number of concurrent sessions is dynamic and may be limited by available system resources. Check if session(_:didInvalidateWith:) on the NISessionDelegate is called with NIError.Code.activeSessionsLimitExceeded and handle when the maximum number of sessions is exceed.




Replies

All NISessions are peer-to-peer and as a result, creating multiple NISession objects is necessary to have multiple concurrent sessions.

One approach is to create a dictionary between an identifier for the peer (i.e. a user identifier provided by your app MyAppUserID) and the NISession objects while also keeping track of the NIDiscoveryToken for each peer identifier:

Code Block swift
var sessions = [MyAppUserID: NISession]()
var peerTokensMapping = [NIDiscoveryToken: MyAppUserID]()


The number of concurrent sessions is dynamic and may be limited by available system resources. Check if session(_:didInvalidateWith:) on the NISessionDelegate is called with NIError.Code.activeSessionsLimitExceeded and handle when the maximum number of sessions is exceed.