如何知道我的设备在Xamarin中连接的WiFi SSID?

发布于 2025-01-18 03:28:30 字数 676 浏览 1 评论 0 原文

我有一个需要连接到特定网络的应用程序。我已经有一个代码强制该设备连接到该网络,但是每次我打开另一个屏幕时,都会重复该过程,并且需要2或3秒钟。因此,与其强迫每次我想在那一刻的网络中询问什么时间,因此,如果它是不正确的网络,它必须连接到正确的网络,如果它在正确的网络中,则没有做任何事情。如何获取网络的名称?

以下是我正在使用的代码,但是当我调试时,它说SSID名称为“<未知的ssid>”,因此,每次我检查名称是否正确时,它都会说是错误的,并且它再次连接到网络。

public static string GetSSID() {

 WifiManager wifiManager = (WifiManager)(Android.App.Application.Context.GetSystemService(Context.WifiService));

        if (wifiManager != null && !string.IsNullOrEmpty(wifiManager.ConnectionInfo.SSID))
        {
            return wifiManager.ConnectionInfo.SSID;
        }
        else
        {
            return "WiFiManager is NULL";
        }
    }

I have an app that needs to be connected to a specific network. I already have a code to force the device to connect to that network, but every time I open a different screen it repeats the process and it takes like 2 or 3 seconds. So, instead of forcing to connect every time I want to ask in what network it is in that moment, so, if it's the incorrect network it has to connect to the correct one, and if it's in the correct one it doesn't have to do anything. How can I get the network's name?

Below is the code I am using, but when I debug it it says the SSID name is "<unknown ssid>", so every time I check if the name is correct it says that is false and it connects again to the network.

public static string GetSSID() {

 WifiManager wifiManager = (WifiManager)(Android.App.Application.Context.GetSystemService(Context.WifiService));

        if (wifiManager != null && !string.IsNullOrEmpty(wifiManager.ConnectionInfo.SSID))
        {
            return wifiManager.ConnectionInfo.SSID;
        }
        else
        {
            return "WiFiManager is NULL";
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2025-01-25 03:28:30

首先,您需要授予该应用程序有关该位置的许可。

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

然后,您需要通过 activityCompat.requestpermissions 授予Permisson,然后再获取WiFi信息。

有关运行时间许可的官方文件:

此外,您还可以尝试以下代码以在授予许可后获取SSID。

 ConnectivityManager connectivityManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);
 NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
 var ssid = networkInfo.ExtraInfo;

At first, you need to grant the app the permission about the location.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

And then you need to grant the permisson by the ActivityCompat.RequestPermissions before you get the information of the wifi.

The official document about run-time permission:
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows

In addition, you can also try the following code to get the SSID after the permission granting.

 ConnectivityManager connectivityManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);
 NetworkInfo networkInfo = connectivityManager.ActiveNetworkInfo;
 var ssid = networkInfo.ExtraInfo;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文