MonoTouch WIFI SSID

时光总嘲笑我的痴心妄想 提交于 2019-12-05 07:21:04

You can do this like the sample code that @Jason linked to. But right now there are no bindings for CaptiveNetwork in the current versions of MonoTouch (but it will be included in a future beta release).

In the meantime you can copy-paste the following code inside your application to get the 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: The latest MonoTouch 5.2+ releases includes support for CaptiveNetwork. The above code is simplified to:

using MonoTouch.SystemConfiguration;

static string GetSSID ()
{
    var dict = CaptiveNetwork.CopyCurrentNetworkInfo ("en0");
    return dict [CaptiveNetwork.NetworkInfoKeySSID].ToString ();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!