2011-12-29 110 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