5

当我想通过 WifiManager.connectionInfo 获取有关当前 wifi 连接的信息时,我得到了这个:

“连接信息的吸气剂:WifiInfo!” 已弃用。在 Java 中已弃用

我怎样才能在 Android 12 中做到这一点?(只想获取rssi)

4

1 回答 1

2

Starting with Build.VERSION_CODES#S, WifiInfo retrieval is moved to ConnectivityManager API surface. WifiInfo is attached in NetworkCapabilities#getTransportInfo() which is available via callback in NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities) or on-demand from ConnectivityManager#getNetworkCapabilities(Network).

final NetworkRequest request =
       new NetworkRequest.Builder()
       .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
       .build();
  final ConnectivityManager connectivityManager =
       context.getSystemService(ConnectivityManager.class);
  final NetworkCallback networkCallback = new NetworkCallback() {
       ...
       @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

https://developer.android.com/reference/kotlin/android/net/wifi/WifiManager#getConnectionInfo()

于 2021-08-31T03:14:29.140 回答