2011-12-29 40 views
6

是否有可能通过Monotouch获得连接的WIFI SSID的IPhone?MonoTouch WIFI SSID

我发现检查Wi-Fi状态的可能性,但没有办法检查SSID。 https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs 有没有人知道一种方式? 感谢所有评论

+1

这里是一个[使用Obj-C的示例] [1]。你应该能够在MT中使用类似的方法。 [1]:http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library – Jason 2011-12-29 15:13:10

回答

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

UPDATE:最新的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。 – 2012-11-20 18:16:09