java - 使用颤振获取我的手机连接的wifi路由器的mac地址
问题描述
我一直在尝试获取我的设备连接到的 wifi 路由器的 mac 地址。我正在使用颤振。
我遇到过几个插件,比如get_ip、wifi_info_plugin和flutter_ip。但要么显示未知,不支持android,不支持ios,要么什么都不显示。
我要做的是让我的应用程序仅在连接到一个特定的 wifi 路由器时运行。所以基本上,当连接到我以外的其他 wifi 路由器时,应用程序的某些功能将被禁用。
请建议任何其他插件或任何解决方法。
解决方案
您可以使用您SystemConfiguration.CaptiveNetwork
的框架从支持的接口复制您当前的网络信息。请注意,您需要允许访问您设备的位置并在您的应用功能中启用热点配置和访问 WiFi 信息:
import UIKit
import CoreLocation
import SystemConfiguration.CaptiveNetwork
import NetworkExtension
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var ssid = ""
var bssid = ""
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if #available(iOS 14.0, *) {
fetchNetworkInfo()
} else {
fetchBSSIDInfo()
}
locationManager.stopUpdatingLocation()
}
@available(iOS, introduced: 4.1.0, deprecated: 14.0)
func fetchBSSIDInfo() {
if let interfaces = CNCopySupportedInterfaces() as? [CFString] {
for interface in interfaces {
if let currentNetworkInfo = CNCopyCurrentNetworkInfo(interface) as? [CFString: Any] {
ssid = currentNetworkInfo[kCNNetworkInfoKeySSID] as? String ?? ""
print("ssid:", ssid)
bssid = currentNetworkInfo[kCNNetworkInfoKeyBSSID] as? String ?? ""
print("bssid:", bssid)
break
}
}
}
}
@available(iOS 14.0, *)
func fetchNetworkInfo() {
NEHotspotNetwork.fetchCurrent { network in
guard let network = network else { return }
print("The SSID for the Wi-Fi network.")
print("ssid:", network.ssid, "\n")
self.ssid = network.ssid
print("The BSSID for the Wi-Fi network.")
print("bssid:", network.bssid, "\n")
self.bssid = network.bssid
print("The recent signal strength for the Wi-Fi network.")
print("signalStrength:", network.signalStrength, "\n")
print("Indicates whether the network is secure")
print("isSecure:", network.isSecure, "\n")
print("Indicates whether the network was joined automatically or was joined explicitly by the user.")
print("didAutoJoin:", network.didAutoJoin, "\n")
print("Indicates whether the network was just joined.")
print("didJustJoin:", network.didJustJoin, "\n")
print("Indicates whether the calling Hotspot Helper is the chosen helper for this network.")
print("isChosenHelper:", network.isChosenHelper, "\n")
}
}
@available(iOS, introduced: 13.2.0, deprecated: 14.0)
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
didChangeAuthorization(status: status)
}
@available(iOS 14.0, *)
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
didChangeAuthorization(status: manager.authorizationStatus)
}
func didChangeAuthorization(status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
print("The user has not chosen whether the app can use location services.\n")
case .restricted:
print("The app is not authorized to use location services.\n")
case .denied:
print("The user denied the use of location services for the app or they are disabled globally in Settings.\n")
case .authorizedAlways:
print("The user authorized the app to start location services at any time.\n")
case .authorizedWhenInUse:
print("The user authorized the app to start location services while it is in use.\n")
@unknown default: break
}
switch status {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted, .denied:
let alert = UIAlertController(title: "Allow Location Access",
message: "Please turn on Location Services",
preferredStyle: .alert)
alert.addAction(.init(title: "Settings",
style: .default) { _ in
let url = URL(string: UIApplication.openSettingsURLString)!
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url) { success in
print("Settings opened: \(success)")
}
}
})
alert.addAction(.init(title: "Ok", style: .default))
DispatchQueue.main.async {
self.present(alert, animated: true)
}
case .authorizedWhenInUse, .authorizedAlways:
locationManager.startUpdatingLocation()
@unknown default: break
}
}
}
推荐阅读
- android - Firebase 远程配置:只调用一次
- java - 未使用 jpa 映射的表名
- android - viewModelProvider 所有者的建议?
- python - 打印出在 python3 执行期间调用的函数的简单、pythonic 方式
- firebase - 将 HttpsCallable 与服务凭据一起使用
- java - REST API 中可以强制查询参数吗
- validation - 当我们根据训练步骤进行训练时,何时进行验证?
- r - 如何使用 dplyr group_by 并获取每个不同分组的索引?
- python-3.x - 如何在 py3 代码中将列表转换为 scapy.plist.PacketList?
- recursion - 不能接受 2 的 CAR(Common Lisp)