Mar*_*bil 1 c# ssid wifi xamarin.android xamarin.forms
我需要获取当前连接的网络的SSID 。
这是我用来在 Xamarin.Android 中查找 SSID 的代码:
WifiManager wifiManager = (WifiManager)(Application.Context.GetSystemService(WifiService));
if (wifiManager != null)
{
var ssid = wifiManager.ConnectionInfo.SSID;
}
else
{
var str = "WiFiManager is NULL";
}
Run Code Online (Sandbox Code Playgroud)
但我需要在Xamarin.Forms中实现这一点。
我怎样才能做到这一点 ?
您可以使用DependencyService。
\n\n\n\n\nDependencyService 类是一个服务定位器,它使 Xamarin.Forms 应用程序能够从共享代码调用本机平台功能。
\n
1\xc2\xba \n创建一个公共接口(为了组织起见,可能在 下Mobile > Services > IGetSSID
)
public interface IGetSSID\n{\n string GetSSID();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n2\xc2\xba创建Android实现
\n\n[assembly: Dependency(typeof(GetSSIDAndroid))]\nnamespace yournamespace\n{\n public class GetSSIDAndroid : IGetSSID\n {\n public string GetSSID()\n {\n WifiManager wifiManager = (WifiManager)(Android.App.Application.Context.GetSystemService(Context.WifiService));\n\n if (wifiManager != null && !string.IsNullOrEmpty(wifiManager.ConnectionInfo.SSID))\n {\n return wifiManager.ConnectionInfo.SSID;\n }\n else\n {\n return "WiFiManager is NULL";\n }\n }\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n3\xc2\xba \n然后在您的表单中您将获得如下所示的 SSID:
\n\nvar ssid = DependencyService.Get<IGetSSID>().GetSSID();\n
Run Code Online (Sandbox Code Playgroud)\n\n注意:不要忘记在 Android Manifest 中添加此权限
\n\n<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
4765 次 |
最近记录: |