java - 使用 flutter 获取我的手机连接到的 wifi 路由器的 mac 地址

标签 java ios swift flutter dart

我一直在尝试获取我的设备所连接的 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/

相关文章:

ios - 如何将 TextField 和 CheckBox 添加到同一个 AlertView

ios - 每次重置标题时如何阻止 UIButton 突出显示/闪烁?

swift - 为什么 switch 在 optional 中不接受 case nil?

java - 自定义分层容器中的绘画问题?

java - LinkedList.java 使用未经检查或不安全的操作。注: Recompile with -Xlint:unchecked for details

ios - NSFetchedResultsController 无法识别删除

iphone - Swift 的字符串连接问题

java - 使用 createNativeQuery 调用 Oracle 存储过程

列表读取应用程序中的 Java NumberFormatException

json - 如何为 JSON 生成过滤字符串