简体   繁体   English

在 iOS 上检测 WiFi 启用/禁用的更好方法?

[英]Better way to detect WiFi enabled/disabled on iOS?

After an unbelievable amount of teeth gnashing, I finally have a method that successfully detects if WiFi is enabled on iOS independent of whether it's connected or not.经过令人难以置信的咬牙切齿之后,我终于有了一种方法,可以成功检测 iOS 上是否启用了 WiFi,而不管它是否已连接。 There are at least a couple uses for such a thing, and I don't think this violates the spirit or the letter of Apple Law(tm).这种事情至少有几种用途,我认为这并不违反 Apple Law(tm) 的精神或文字。

However, it's ugly and probably won't work forever.但是,它很丑陋,可能永远不会起作用。 It currently works on iOS 10.2.1, as of Jan 31, 2017. I'll put my answer below and hope that someone can improve it.它目前适用于 iOS 10.2.1,截至 2017 年 1 月 31 日。我会将我的答案放在下面,希望有人可以改进它。 I've heavily researched Reachability ( doesn't meet the requirements ), CaptiveNetwork, HotspotHelper, SCNetworkConfiguration, Xamarin's System.Net.NetworkInterface, and more.我对可达性(不符合要求)、CaptiveNetwork、HotspotHelper、SCNetworkConfiguration、Xamarin 的 System.Net.NetworkInterface 等进行了大量研究。 This is it that actually works as far as I can tell.据我所知,这实际上是有效的。

The gist of the solution is that when there's TWO interfaces reported by getifaddrs() with the name "awdl0" then WiFi is enabled.该解决方案的要点是,当 getifaddrs() 报告了两个名为“awdl0”的接口时,WiFi 已启用。 Just one and it's disabled.只有一个,它被禁用。

I credit pebble8888 for pointing me to https://github.com/alirp88/SMTWiFiStatus which is Objective-C, where the lack of comments make it hard to understand what's going on or what the author's intention was.我感谢 pebble8888 将我指向https://github.com/alirp88/SMTWiFiStatus ,它是 Objective-C,其中缺乏评论使得很难理解发生了什么或作者的意图是什么。

Here's my full and complete Xamarin/C# solution, which ought to be pretty readable for any other major language user:这是我完整的 Xamarin/C# 解决方案,对于任何其他主要语言用户来说,它应该是非常可读的:

using System;
using System.Runtime.InteropServices;

namespace Beacon.iOS
{
/// <summary>
/// code stolen from the Xamarin source code to work around goofy interactions between
/// the good-god-why-would-it-work-that-way iOS and the entirely reasonable Xamarin
/// (it doesn't report interfaces that are reported multiple times)
/// </summary>
class XamHack
{
    //
    // Types
    //
    internal struct ifaddrs
    {
#pragma warning disable 0649
        public IntPtr ifa_next;
        public string ifa_name;
        public uint ifa_flags;
        public IntPtr ifa_addr;
        public IntPtr ifa_netmask;
        public IntPtr ifa_dstaddr;
        public IntPtr ifa_data;
#pragma warning restore
    }

    //
    // OS methods
    //
    [DllImport("libc")]
    protected static extern int getifaddrs(out IntPtr ifap);

    [DllImport("libc")]
    protected static extern void freeifaddrs(IntPtr ifap);


    //
    // Methods
    //

    /// <summary>
    /// Our glorious hack.  I apologize to the programming gods for my sins
    /// but this works (for now) and functionality trumps elegance.  Even this.
    /// Reverse engineered from: https://github.com/alirp88/SMTWiFiStatus
    /// </summary>
    public static bool IsWifiEnabled()
    {
        int count = 0;
        IntPtr ifap;

        // get the OS to put info about all the NICs into a linked list of buffers
        if (getifaddrs(out ifap) != 0)
            throw new SystemException("getifaddrs() failed");

        try
        {
            // iterate throug those buffers
            IntPtr next = ifap;
            while (next != IntPtr.Zero)
            {
                // marshall the data into our struct
                ifaddrs addr = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs));

                // count the instances of the sacred interface name
                if ("awdl0" == addr.ifa_name)
                    count++;

                // move on to the next interface
                next = addr.ifa_next;
            }
        }
        finally
        {
            // leaking memory is for jerks
            freeifaddrs(ifap);
        }

        // if there's two of the sacred interface, that means WiFi is enabled.  Seriously.
        return (2 == count);
    }

} // class
} // namespace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM