1

I am trying that opening Wifi hotspot in android,When I run below code it runs successfully for Api 28 I can open Wifi hotspot without getting errror but When I run it in Api 25 for Android 7.1 device it gets below errors, it says get WRITE_SETTINGS permission,but When I tested for Api 28 it doesn't necessary for running application,Why can I get this errros and How can I run it for android Api 25

Getting Errors

W/System.err: java.lang.NoSuchMethodException: setWifiApEnabled  [class android.net.wifi.WifiConfiguration, boolean]
        at java.lang.Class.getMethod(Class.java:1981)
        at java.lang.Class.getMethod(Class.java:1637)
        at com.kocsistem.pixageoneandroid.utils.Utils.configApState(Utils.java:1126)
        at com.kocsistem.pixageoneandroid.utils.Utils.openHotspot(Utils.java:1095)
        at com.kocsistem.pixageoneandroid.network.broadcast.NetworkChangeReceiver$2.run(NetworkChangeReceiver.java:120)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Open Hotspot code

public static void openHotspot(Context context,boolean enable){
    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        assert manager != null;
        try {
            manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

                @SuppressLint("SetTextI18n")
                @Override
                public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                    super.onStarted(reservation);
                    Log.i("Wifi Hotspot is on now , reservation is : %s", reservation.toString());
                    mReservation = reservation;
                    WifiManager.LocalOnlyHotspotReservation mReservation = reservation;
                    String key = mReservation.getWifiConfiguration().preSharedKey;
                    Log.i("key:",key);
                    String ussid = mReservation.getWifiConfiguration().SSID;
                    Log.i("ussid:",ussid);
                }

                @Override
                public void onStopped() {
                    super.onStopped();
                    Log.i("onStopped: ","");
                }

                @Override
                public void onFailed(int reason) {
                    super.onFailed(reason);
                    Log.i("onFailed: ","");
                }
            }, new Handler());
        }catch (Exception e){
            e.printStackTrace();
        }
    }else{
      //for Api<26
        configApState(context);
    }
}

check whether wifi hotspot on or off

public static boolean isApOn(Context context) {
    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    try {
        Method method = wifimanager.getClass().getDeclaredMethod("isWifiAp Enabled");
        method.setAccessible(true);
        return (Boolean) method.invoke(wifimanager);
    }
    catch (Throwable ignored) {}
    return false;
}

toggle wifi hotspot on or off

    public static boolean configApState(Context context) {
        WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
        WifiConfiguration wificonfiguration = null;
        try {
// if WiFi is on, turn it off
            if(isApOn(context)) {
                wifimanager.setWifiEnabled(false);
            }
            Method method = wifimanager.getClass().getMethod("setWifiApEnabled ", WifiConfiguration.class, boolean.class);
            method.invoke(wifimanager, wificonfiguration, !isApOn(context));
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
Diego
  • 937
  • 8
  • 24

1 Answers1

1

this code works for Android Api 26 (Oreo) and above , its not gonna work with nougat (api 25) there is another way to do that , you have to get method called "setWifiApEnabled" Check this peace of code

PS: you have to convert the code from c# to java and dont forget to grant the write settings permission ...

  private bool ActivateTethering(string ssid, string password)
    {
        var myConfiguration = new WifiConfiguration();
        WifiManager wifimanager = (WifiManager)Context.GetSystemService(Context.WifiService);

        myConfiguration.Ssid = ssid;
        myConfiguration.PreSharedKey = password;
        myConfiguration.AllowedAuthAlgorithms.Set((int)AuthAlgorithmType.Shared);
        myConfiguration.AllowedProtocols.Set((int)ProtocolType.Rsn);
        myConfiguration.AllowedProtocols.Set((int)ProtocolType.Wpa);
        myConfiguration.AllowedKeyManagement.Set((int)KeyManagementType.WpaPsk);

    
        var enableWifi = wifimanager.Class.GetDeclaredMethods();

        try
        {
            bool setWifiConfig = false;
            foreach (var method in enableWifi)
            {
                if (method.Name.Equals("setWifiApEnabled"))
                {
                    setWifiConfig = (bool)method.Invoke(wifimanager, myConfiguration, true);
                    break;
                }
            }
        }
        catch (InvocationTargetException e)
        {
            Console.WriteLine(e.Data.ToString());
        }
       
        return setWifiConfig ;
    }
Raouf
  • 21
  • 1
  • 4