How to save access point settings in android 7 programatically

623 views Asked by At

I'm working on an app that creates a hotspot as soon as it starts up. This has benn working fine until Android 7 Nougat came. I'm using the WifiApManager class.

Like I said everything worked perfect but when using API 25 the hotspot is created with the correct settings (ssid, password, etc.) and my laptop recognizes it and connects .

However it has "no Internet" hence no data exchange happens. Waht I need to do is to go to the phones hotspot settings and press save. It will turn of and on again and eventually work as it should.

I don't know if this is an android bug or is it intentional but I belive there was no "save" button in previous API's!? I have been searching the web but couldn't find anything. Thanks in advance. kEbO

1

There are 1 answers

1
Dandan On
  public static boolean setHotspotNameAndPassword(String newName,String password, Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
            Method getConfigMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
            WifiConfiguration wifiConfig = (WifiConfiguration) getConfigMethod.invoke(wifiManager);
            wifiConfig.preSharedKey=password;
            wifiConfig.SSID = newName;

            Method setConfigMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
            setConfigMethod.invoke(wifiManager, wifiConfig);
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }

It is work for me! Change setting.

but I can't find way to turn on/off Hotspot on Android 7.0+