6

是否有可能在 iPhone 上使用 Monotouch 连接 WIFI SSID?

我找到了检查 Wi-Fi 状态的可能性,但无法检查 SSID。 https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs 那么有人知道方法吗?感谢所有评论

4

1 回答 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 ();
}
于 2012-01-12T21:05:47.190 回答