MonoTouch WIFI SSID

时间:2011-12-29 10:06:36

标签: iphone ios xamarin.ios ssid captivenetwork

是否可以使用Monotouch连接IPhone连接的WIFI SSID?

我发现有可能检查Wi-Fi状态,但无法检查SSID。 https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs 所以有人知道吗? 感谢所有评论

1 个答案:

答案 0 :(得分:6)

您可以像@Jason链接到的示例代码那样执行此操作。但是目前MonoTouch的当前版本中没有 CaptiveNetwork 的绑定(但它将包含在未来的测试版中)。

与此同时,您可以在应用程序中复制粘贴以下代码以获取SSID。

    using System;
    using System.Runtime.InteropServices;
    using MonoTouch;
    using MonoTouch.CoreFoundation;
    using MonoTouch.Foundation;
    using MonoTouch.ObjCRuntime;

    [DllImport (Constants.SystemConfigurationLibrary)]
    extern static IntPtr CNCopyCurrentNetworkInfo (IntPtr interfaceName);

    static string GetSSID ()
    {
        IntPtr scl = Dlfcn.dlopen (Constants.SystemConfigurationLibrary, 0);
        try {
            using (NSString en0 = new NSString ("en0")) {
                using (NSDictionary dict = new NSDictionary (CNCopyCurrentNetworkInfo (en0.Handle))) {
                    using (NSString key = Dlfcn.GetStringConstant (scl, "kCNNetworkInfoKeySSID")) {
                        return dict [key].ToString ();
                    }
                }
            }
        }
        catch (EntryPointNotFoundException) {
            // this is not available when running on the simulator
            return String.Empty;
        }
        finally {
            Dlfcn.dlclose (scl);
        }
    }

更新:最新的MonoTouch 5.2+版本包括对CaptiveNetwork的支持。上面的代码简化为:

using MonoTouch.SystemConfiguration;

static string GetSSID ()
{
    var dict = CaptiveNetwork.CopyCurrentNetworkInfo ("en0");
    return dict [CaptiveNetwork.NetworkInfoKeySSID].ToString ();
}