Hello All, I am facing one problem in the Nearby Interaction library, I want to connect multiple devices at the same time,
Right now NISession is disconnect itself when a new device comes in range and connect with a new device
My question is how can I connect with multiple devices at the same time and access the distance of all the connected devices.
This how i am using right now
final class NearbyInteractionManager: NSObject {
static let instance = NearbyInteractionManager()
var sessionNI: NISession?
weak var delegate: NearbyInteractionManagerDelegate?
func start() {
sessionNI = NISession()
sessionNI?.delegate = self
MultipeerConnectivityManager.instance.delegate = self
MultipeerConnectivityManager.instance.startBrowsingForPeers()
}
private var discoveryTokenData: Data {
guard let token = sessionNI?.discoveryToken,
let data = try? NSKeyedArchiver.archivedData(withRootObject: token, requiringSecureCoding: true) else {
fatalError("can't convert token to data")
}
return data
}
}
extension NearbyInteractionManager: MultipeerConnectivityManagerDelegate {
func connectedDevicesChanged(devices: [String]) {
print("connected devices changed \(devices)")
}
func connectedToDevice() {
print("connected to device")
MultipeerConnectivityManager.instance.shareDiscoveryToken(data: discoveryTokenData)
}
func receivedDiscoveryToken(data: Data) {
print("data received")
guard let token = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NIDiscoveryToken.self, from: data) else {
fatalError("Unexpectedly failed to encode discovery token.")
}
let configuration = NINearbyPeerConfiguration(peerToken: token)
sessionNI?.run(configuration)
}
}
// MARK: - NISessionDelegate
extension NearbyInteractionManager: NISessionDelegate {
func session(_ session: NISession, didUpdate nearbyObjects: [NINearbyObject]) {
delegate?.didUpdateNearbyObjects(objects: nearbyObjects, token: nearbyObjects[0].discoveryToken)
}
func session(_ session: NISession, didRemove nearbyObjects: [NINearbyObject], reason: NINearbyObject.RemovalReason) {}
func sessionWasSuspended(_ session: NISession) {}
func sessionSuspensionEnded(_ session: NISession) {}
func session(_ session: NISession, didInvalidateWith error: Error) {}
}
protocol NearbyInteractionManagerDelegate: class {
func didUpdateNearbyObjects(objects: [NINearbyObject], token: NIDiscoveryToken)
}
Any update will be very helpful full
Thank you