66//
77
88import MultipeerConnectivity
9+ import Foundation
910#if canImport(RealityKit)
1011import RealityKit
1112#endif
@@ -22,6 +23,10 @@ public class MultipeerHelper: NSObject {
2223 case both = 3
2324 }
2425
26+ public static let compTokenKey = " MPH_CompToken "
27+ public static let osVersionKey = " MPH_OSVersion "
28+ public static let platformKey = " MPH_Platform "
29+
2530 /// Detemines whether your service is advertising, browsing, or both.
2631 public let sessionType : SessionType
2732 public let serviceName : String
@@ -86,7 +91,10 @@ public class MultipeerHelper: NSObject {
8691 encryptionPreference: encryptionPreference
8792 )
8893 session. delegate = self
94+ self . setupSession ( )
95+ }
8996
97+ private func setupSession( ) {
9098 if ( self . sessionType. rawValue & SessionType . host. rawValue) != 0 {
9199 var discoveryInfo = self . delegate? . setDiscoveryInfo ? ( )
92100 ?? [ String: String] ( )
@@ -96,10 +104,21 @@ public class MultipeerHelper: NSObject {
96104 let networkLoc = NetworkCompatibilityToken . local
97105 let jsonData = try ? JSONEncoder ( ) . encode ( networkLoc)
98106 if let encodedToken = String ( data: jsonData!, encoding: . utf8) {
99- discoveryInfo [ " compatibility_token " ] = encodedToken
107+ discoveryInfo [ MultipeerHelper . compTokenKey ] = encodedToken
100108 }
101109 }
102110 #endif
111+ #if os(iOS) || os(tvOS)
112+ discoveryInfo [ MultipeerHelper . osVersionKey] = UIDevice . current. systemVersion
113+ #if os(iOS)
114+ discoveryInfo [ MultipeerHelper . platformKey] = " iOS "
115+ #else
116+ discoveryInfo [ MultipeerHelper . platformKey] = " tvOS "
117+ #endif
118+ #elseif os(macOS)
119+ discoveryInfo [ MultipeerHelper . osVersionKey] = ProcessInfo . processInfo. operatingSystemVersionString
120+ discoveryInfo [ MultipeerHelper . platformKey] = " macOS "
121+ #endif
103122 serviceAdvertiser = MCNearbyServiceAdvertiser (
104123 peer: myPeerID,
105124 discoveryInfo: discoveryInfo,
@@ -176,12 +195,12 @@ public class MultipeerHelper: NSObject {
176195
177196 /// Method used for disconnecting all services. Once completed,
178197 /// create a new MultipeerHelper if you want to connect to sessions again.
179- func disconnectAll( ) {
198+ public func disconnectAll( ) {
180199 self . serviceAdvertiser? . stopAdvertisingPeer ( )
181200 self . serviceBrowser? . stopBrowsingForPeers ( )
182201 self . serviceAdvertiser = nil
183202 self . serviceBrowser = nil
184- self . session. disconnect ( )
203+ self . session? . disconnect ( )
185204 }
186205}
187206
0 commit comments