Skip to content

Commit 2cfa98d

Browse files
authored
Migrate from CNCopySupportedInternet to NEHotspotNetwork (#3438)
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
1 parent 4dd754a commit 2cfa98d

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

Sources/Shared/Environment/ConnectivityWrapper.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Foundation
22
#if os(iOS)
33
import CoreTelephony
44
import Reachability
5-
import SystemConfiguration.CaptiveNetwork
65
#endif
76
import Communicator
7+
import NetworkExtension
88

99
/// Wrapper around CoreTelephony, Reachability
1010
public class ConnectivityWrapper {
@@ -59,31 +59,18 @@ public class ConnectivityWrapper {
5959
#if targetEnvironment(simulator)
6060
return "Simulator"
6161
#endif
62-
63-
guard let interfaces = CNCopySupportedInterfaces() as? [String] else { return nil }
64-
for interface in interfaces {
65-
guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as CFString) as NSDictionary? else {
66-
continue
67-
}
68-
return interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
69-
}
7062
return nil
7163
}
7264
self.currentWiFiBSSID = {
73-
guard let interfaces = CNCopySupportedInterfaces() as? [String] else { return nil }
74-
for interface in interfaces {
75-
guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as CFString) as NSDictionary? else {
76-
continue
77-
}
78-
return interfaceInfo[kCNNetworkInfoKeyBSSID as String] as? String
79-
}
80-
return nil
65+
nil
8166
}
8267
self.connectivityDidChangeNotification = { .reachabilityChanged }
8368
self.simpleNetworkType = { reachability?.getSimpleNetworkType() ?? .unknown }
8469
self.cellularNetworkType = { reachability?.getNetworkType() ?? .unknown }
8570
self.currentNetworkHardwareAddress = { nil }
8671
self.networkAttributes = { [:] }
72+
73+
syncNetworkInformation()
8774
}
8875
#else
8976
init() {
@@ -99,6 +86,8 @@ public class ConnectivityWrapper {
9986
self.cellularNetworkType = { .unknown }
10087
self.currentNetworkHardwareAddress = { nil }
10188
self.networkAttributes = { [:] }
89+
90+
syncNetworkInformation()
10291
}
10392
#endif
10493

@@ -111,4 +100,20 @@ public class ConnectivityWrapper {
111100
CTTelephonyNetworkInfo().serviceCurrentRadioAccessTechnology
112101
}
113102
#endif
103+
104+
private func syncNetworkInformation() {
105+
NEHotspotNetwork.fetchCurrent { hotspotNetwork in
106+
Current.Log
107+
.verbose(
108+
"Current SSID: \(String(describing: hotspotNetwork?.ssid)), current BSSID: \(String(describing: hotspotNetwork?.bssid))"
109+
)
110+
self.currentWiFiSSID = {
111+
#if targetEnvironment(simulator)
112+
return "Simulator"
113+
#endif
114+
return hotspotNetwork?.ssid
115+
}
116+
self.currentWiFiBSSID = { hotspotNetwork?.bssid }
117+
}
118+
}
114119
}

0 commit comments

Comments
 (0)