MonoTouch WIFI SSID

6

1
这是一个使用Obj-C的示例。您应该能够在MT中使用类似的方法。 - Jason
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);
        }
    }

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

using MonoTouch.SystemConfiguration;

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

2
CopyCurrentNetworkInfo在MT 6.0.6中已经过时。请使用TryCopyCurrentNetworkInfo代替。 - Jonas Stawski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接