Search code examples
iphoneiosxamarin.iosssidcaptivenetwork

MonoTouch WIFI SSID


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


Solution

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