Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Wifi SSID from ConnectivityManager NetworkCapabilities synchronously

Currently, the most popular method of getting the SSID of the Wifi network the Android device is connected to is using WifiManager's getConnectionInfo() method. However, that method is deprecated in API level 31. According to the documentation, we're expected to use ConnectivityManager's getNetworkCapabilities() method to get the Wifi info. Unfortunately, the documentation states

This will remove any location-sensitive data in TransportInfo embedded in NetworkCapabilities#getTransportInfo(). Some transport info instances like WifiInfo contain location-sensitive information. Retrieving this location-sensitive information (subject to the app's location permissions) will be noted by the system. To include any location-sensitive data in TransportInfo, use a NetworkCallback with NetworkCallback#FLAG_INCLUDE_LOCATION_INFO flag.

It seems like the only way now to get the SSID is by registering a NetworkCallback with the FLAG_INCLUDE_LOCATION_INFO flag and waiting until the callback is invoked. This is not a suitable replacement for the old method as the old method was synchronous and could be called on-demand. Is there currently any way to get the Wifi SSID synchronously without using the deprecated getConnectionInfo() method?

like image 227
kevdliu Avatar asked Feb 28 '26 00:02

kevdliu


2 Answers

I am having issues with this also. Do you need to query this synchronously? Can you not just store the last SSID output from onCapabilitiesChanged() in a livedata object, this is a really nice resource to help you do that. Unfortunately my code is still giving me null for SSID information using transport info. Can you share your code?

like image 128
958 Avatar answered Mar 02 '26 15:03

958


Try the example below, call the method getWiFiSSID() inside Thread

public static String getWiFiSSID() {
 final NetworkRequest request =
      new NetworkRequest.Builder()
      .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
      .build();
 List<String> ssidList = new ArrayList<>();
 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();
          ssidList.add(wifiInfo.getSSID());
          connectivityManager.unregisterNetworkCallback(this);
      }
      // etc.
 };
 connectivityManager.requestNetwork(request, networkCallback); // For request
 connectivityManager.registerNetworkCallback(request, networkCallback); // For listen
 int timer = 5000;
 while (ssidList.size() == 0 && timer >= 0) {
   try {
      Thread.sleep(250);
      timer -= 250;
   } catch (InterruptedException e) {
      break;
   }
 }
 if(ssidList.size()>0) {
   return ssidList.get(0);
 }else {
   connectivityManager.unregisterNetworkCallback(networkCallback);
   return null;
 }
}
like image 23
user3374790 Avatar answered Mar 02 '26 13:03

user3374790