MonoTouch WIFI SSID

妖精的绣舞 提交于 2020-01-13 09:24:19

问题


is there a possibility to get on an IPhone the connected WIFI SSID with Monotouch?

I have found a possibility to check the Wi-Fi States but there is no way to check the SSID. https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs So did anyone know a way? Thanks for all Comments


回答1:


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 ();
}


来源:https://stackoverflow.com/questions/8666397/monotouch-wifi-ssid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!