2

What is the API call I need to make in Android 9.0 (Android P) to get a Wifi hotspot name?

public static String getWifiApSSID(Context context) {
        try {
            WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            Method method = manager.getClass().getDeclaredMethod("getWifiApConfiguration");
            WifiConfiguration configuration = (WifiConfiguration) method.invoke(manager);
            if (configuration != null) {
                return configuration.SSID;
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return "";
    }

Android 9.0 returns "".

nkr
  • 3,026
  • 7
  • 31
  • 39
J.Chen
  • 51
  • 1
  • 7

2 Answers2

3

You're using reflection method getWifiApiConfiguration which doesn't works for API>=26. Good news, for API>=26, you don't need to use reflection. You can just use the public exposed API by android i.e. startLocalOnlyHotspot

It requires Manifest.permission.CHANGE_WIFI_STATE and ACCESS_FINE_LOCATION permissions.

Here's a simple example of how you can turn on hotspot using this API.

    private WifiManager wifiManager;
WifiConfiguration currentConfig;
WifiManager.LocalOnlyHotspotReservation hotspotReservation;

The method to turn on hotspot:

`
@RequiresApi(api = Build.VERSION_CODES.O)
public void turnOnHotspot() {

      wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

        @Override
        public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
          super.onStarted(reservation);
          hotspotReservation = reservation;
          currentConfig = hotspotReservation.getWifiConfiguration();

          Log.v("DANG", "THE PASSWORD IS: "
              + currentConfig.preSharedKey
              + " \n SSID is : "
              + currentConfig.SSID);

          hotspotDetailsDialog();

        }

        @Override
        public void onStopped() {
          super.onStopped();
          Log.v("DANG", "Local Hotspot Stopped");
        }

        @Override
        public void onFailed(int reason) {
          super.onFailed(reason);
          Log.v("DANG", "Local Hotspot failed to start");
        }
      }, new Handler());
    }
`

Here's how you can get details of the locally created hotspot

private void hotspotDetaisDialog()
{

    Log.v(TAG, context.getString(R.string.hotspot_details_message) + "\n" + context.getString(
              R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString(
              R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey);

}
`

I recently created a demo app called Spotserve. That turns on wifi hotspot for all devices with API>=15 and hosts a demo server on that hotspot. You can check that for more details. Hope this helps!

Adeel Zafar
  • 361
  • 2
  • 16
  • 1
    But this method have a detail with hotspot, the hotspot don't have access to internet if you look the oficial documentation on Android page. – Eduardo Rotundo Apr 09 '20 at 21:14
  • Spotserve does not turn on wifi hotspot, does it? Looking at the code, it seems like it only tries to detect the ip on which the http server is running, but relies on the user to start the hotspot... Am I missing something? – JonasVautherin May 07 '21 at 11:37
1

You have another option if your device its rooted.

Use this command to enable:

service call connectivity 24 i32 0 i32 1 i32 0 s16 text > /dev/null

In the official documentation of Android, the method startLocalOnlyHotspot allows sharing an internal connection but doesn't have access to internet.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • 1
    This worked amazingly, i'm not sure what library or api this comes from, because it seems like lots of things could be configured this way. Is there an official resource or did a file need to be debugged to figure this out? i was trying to use tasker and the plugin in tasker and it wasn't working, but "code" option and this with root worked! When i turn on my car, my phone connects bluetooth to my stereo and my hotspot turns on and my stero connects to my hotspot and i'm on! woohoo!! Thanks again – Null Jul 28 '20 at 22:15
  • I feel glad you found the solution with this code. I hope in the future android return the control over hotspot and we can use the api again – Eduardo Rotundo Jul 30 '20 at 12:48
  • By the way this is for a regular pixel 4 that it worked. – Null Jul 31 '20 at 17:20