使用flutter获取手机连接的wifi路由器的mac地址 [英] Get mac address of the wifi router my phone is connected to, using flutter

查看:895
本文介绍了使用flutter获取手机连接的wifi路由器的mac地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取设备所连接的wifi路由器的mac地址.我正在使用Flutter.

I have been trying to get the the mac address of my wifi router that my device is connected to. I am using Flutter.

我遇到了一些插件,例如 get_ip flutter_ip .但是它要么显示未知,要么不支持android,不支持ios,要么什么都不显示.

I have come across few plugins like get_ip, wifi_info_plugin and also flutter_ip. But either it shows unknown, doesn't support android, doesn't support ios or just shows nothing at all.

我想做的是,当连接到一个特定的wifi路由器时,我的应用只能运行.因此,当连接到除我以外的其他wifi路由器时,基本上将禁用该应用程序的某些功能.

What I am trying to do is to make my app run only when connected to one specific wifi router. So basically some features of the app will be disabled when connected to a different wifi router other than mine.

请建议其他任何插件或其他解决方法.

Please suggest any other plugin or any work around.

推荐答案

您可以使用SystemConfiguration.CaptiveNetwork框架从受支持的接口复制当前的网络信息.请注意,您需要允许访问设备的位置,并在应用程序功能中启用热点配置"和访问WiFi信息":

You can use you SystemConfiguration.CaptiveNetwork framework to copy your current network info from the supported interfaces. Note that you would need to allow access your device's location and enable Hotspot Configuration and Access WiFi Information in your app capabilities:

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
        }
    }
}

这篇关于使用flutter获取手机连接的wifi路由器的mac地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆