首页 文章

MonoTouch WIFI SSID

提问于
浏览
6

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

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

1 回答

  • 6

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

    在此期间,您可以在应用程序中复制粘贴以下代码以获取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);
            }
        }
    

    UPDATE :最新的MonoTouch 5.2版本包括对 CaptiveNetwork 的支持 . 以上代码简化为:

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

相关问题