在Android 12中无法使用onCapabilitiesChanged获取WIFI SSID

7eumitmz  于 2023-02-06  发布在  Android
关注(0)|答案(4)|浏览(766)
    • 说明**

在Android-12中,我无法使用ConnectivityManager.NetworkCallback类中的onCapabilitiesChanged获取WIFI SSID。在Android-12中,getConnectionInfo已弃用。因此,正如Android文档所建议的,我正在尝试使用onCapabilitiesChanged获取WifiInfo对象。
像这样,

@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
        super.onCapabilitiesChanged(network, networkCapabilities);
        WifiInfo wifiInfo = (WifiInfo) networkCapabilities.getTransportInfo();
    }

但是,当我打印WifiInfo对象时,它将打印**SSID:〈未知ssid〉**和getHiddenSSID返回真。
意外行为是,
Android-12设备连接一个WIFI(例如ABC)

  • 当我尝试使用getConnectionInfo获取WIFI SSID时,它返回SSID:ABC和获取隐藏SSID:假的
  • 但是,对于同一个网络,当我尝试使用onCapabilitiesChanged获取WIFI SSID时,它返回SSID:〈未知ssid〉和getHiddenSSID:真

注:提供地点权限。

am46iovg

am46iovg1#

经过几天的时间,终于找到了为什么我没有得到SSID在onCapabilitiesChanged
在创建对象时,我们需要在ConnectivityManager.NetworkCallback构造函数中传递FLAG_INCLUDE_LOCATION_INFO
在默认构造函数中,位置敏感信息被隐藏,因为我们无法在onCapabilitiesChanged中获取SSID。一旦我们创建了带有标志的NetworkCallback对象,我们将获得SSID。
链接

h22fl7wq

h22fl7wq2#

获取SSID和BSSID API31 Xamarin C#示例

所需权限:更改网络状态、访问精细位置
如果API〈31TransportInfo将返回空值

using Android.Content;
using Android.Net;
using Android.Net.Wifi;

protected override void OnStart()
    {
        base.OnStart();

        NetworkRequest request = new NetworkRequest.Builder().AddTransportType(transportType: TransportType.Wifi).Build();
        ConnectivityManager connectivityManager = Android.App.Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;

        NetworkCallbackFlags flagIncludeLocationInfo = NetworkCallbackFlags.IncludeLocationInfo;
        NetworkCallback networkCallback = new NetworkCallback((int)flagIncludeLocationInfo);
        connectivityManager.RequestNetwork(request, networkCallback);
    }

    private class NetworkCallback : ConnectivityManager.NetworkCallback
    {
        public NetworkCallback(int flags) : base(flags)
        {
        }

        public override void OnCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities)
        {
            base.OnCapabilitiesChanged(network, networkCapabilities);
            WifiInfo wifiInfo = (WifiInfo)networkCapabilities.TransportInfo;

            if (wifiInfo != null)
            {
                string ssid = wifiInfo.SSID.Trim(new char[] {'"', '\"' });
                string bssid = wifiInfo.BSSID;
            }
        }
    }

点击Android API参考.连接管理器.网络回调(int)!

jfewjypa

jfewjypa3#

new NetworkCallback()替换为new NetworkCallback(ConnectivityManager.NetworkCallback.FLAG_INCLUDE_LOCATION_INFO)

示例代码:

final NetworkRequest request =
      new NetworkRequest.Builder()
      .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
      .build();
 final ConnectivityManager connectivityManager =
      context.getSystemService(ConnectivityManager.class);
 final NetworkCallback networkCallback = new NetworkCallback(ConnectivityManager.NetworkCallback.FLAG_INCLUDE_LOCATION_INFO) {
      ...
      @Override
      void onAvailable(Network network) {}

      @Override
      void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
          WifiInfo wifiInfo = (WifiInfo) networkCapabilities.getTransportInfo();
      }
      // etc.
 };
 connectivityManager.requestNetwork(request, networkCallback); // For request
 connectivityManager.registerNetworkCallback(request, networkCallback); // For listen

参考链接:

网络回调#网络回调(整数)

r55awzrz

r55awzrz4#

此方法适用于所有Android版本,但适用于Android 8. 0(Oreo)和更高版本,**您需要有定位服务(GPS)以访问Wi-Fi SSID。**此外,您需要在运行时请求ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION权限,并在清单文件中声明它们。如果您希望在后台访问Wi-Fi SSID,你可以写一个前台服务来处理这个问题。

private fun checkWIFIName(context: Context) : Boolean {
    val wifiManager = context.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
    val wifiInfo = wifiManager.connectionInfo
    println("SSID : ${wifiInfo.ssid}")
    return wifiInfo.ssid.trim().equals("\"$SSID\"", true)

}

相关问题