作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试获取我的设备所连接的 wifi 路由器的 MAC 地址。我正在使用 Flutter。
我遇到过一些插件,例如 get_ip , wifi_info_plugin还有flutter_ip 。但要么显示未知,要么不支持 android,不支持 ios,要么根本不显示任何内容。
我想做的是让我的应用程序仅在连接到一个特定wifi路由器时运行。因此,基本上,当连接到除我的以外的其他 WiFi 路由器时,应用程序的某些功能将被禁用。
请建议任何其他插件或任何解决方法。
最佳答案
您可以使用SystemConfiguration.CaptiveNetwork
框架从支持的接口(interface)复制当前的网络信息。请注意,您需要允许访问设备的位置并在应用程序功能中启用热点配置和访问 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
}
}
}
关于java - 使用 flutter 获取我的手机连接到的 wifi 路由器的 mac 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60218223/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!