【问题标题】:Get mac address of the wifi router my phone is connected to, using flutter使用颤振获取我的手机连接的wifi路由器的mac地址
【发布时间】:2020-05-29 18:35:45
【问题描述】:

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

我遇到过几个插件,例如 get_ipwifi_info_pluginflutter_ip。但要么显示未知,不支持android,不支持ios,要么什么都不显示。

我要做的是让我的应用程序仅在连接到一个特定的wifi路由器时运行。所以基本上,当连接到我以外的其他 wifi 路由器时,应用程序的某些功能将被禁用。

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

【问题讨论】:

  • 你知道MAC地址欺骗是微不足道的吧?
  • 是的,但因为这更像是一个严格针对员工的内部应用程序。这就是我希望很少有服务被锁定到特定办公室 wifi 路由器的原因。

标签: java ios swift flutter dart


【解决方案1】:

您可以使用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
        }
    }
}

【讨论】:

  • 我试试这个可以回复你。谢谢。
  • fetchNetworkInfo() 仅返回网络对象几秒钟,主要是 4-5 秒,然后它发送 nil 对象而不是任何 netwok 对象。我正在检查 iOS 14 ......我想要 SSID背景信息...有什么想法吗??
【解决方案2】:

谢谢大家,我也在找这个答案。

参考下面提到的代码。 并启用位置权限并启用位置。

class _MyAppState extends State<MyApp> {
  WifiInfoWrapper _wifiObject;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    WifiInfoWrapper wifiObject;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      wifiObject = await WifiInfoPlugin.wifiDetails;
    } on PlatformException {}
    if (!mounted) return;

    setState(() {
      _wifiObject = wifiObject;
    });
  }

  @override
  Widget build(BuildContext context) {
    String macAddress = _wifiObject != null ? _wifiObject.bssId.toString() : "mac";
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Mac of AP - ' + macAddress),
        ),
      ),
    );
  }
}




【讨论】:

  • 仍然有和以前一样的错误,得到 02:00:00:00:00:00:00。但现在我在模拟器中得到了 02:00:00:00:00:00:00 而在真实设备中得到了空白。
【解决方案3】:

看来wifi_info_plugin 是您最好的选择。按照文档,尝试像这样获取 AP MAC:

wifiObject = await  WifiInfoPlugin.wifiDetails;
AP_MAC = wifiObject.bssId.toString();

确保AP_MAC != "missing",并包含在try/catch 中,以防它抛出PlatformException。这可能不适用于 iOS 设备。

【讨论】:

  • 我也这样做了,得到了 02:00:00:00:00:00:00。
  • @takiuddin93 某种权限问题不知道怎么解决你发现了吗?
【解决方案4】:

我遇到了同样的问题。我最终使用了位置包。

 location: ^3.0.2


bool _serviceEnabled;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
    _serviceEnabled = await location.requestService();
}

【讨论】:

    猜你喜欢
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2011-09-25
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多