Android Question setWifiApEnabled doen't work in Android 7.1.2

welu1805

Active Member
Licensed User
Longtime User
Hi all,

I want to turn on a HotSpot on my tablet with Android 7.1.2 but it doesn't work.
Here is my Java-code. The return value is "Error in method.invoke(wifiManager, netConfig, enable)"

B4X:
#If JAVA
import android.text.SpannableStringBuilder;
import android.text.Spannable;
import android.widget.TextView;
import android.view.View;
import android.content.Context;
import android.graphics.Color;
import android.text.style.ImageSpan;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public String SetAccessPoint(boolean enable, String SSID, String PASS, Context context) {
    String name;
        name = "leer";
        
    WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        
        if(enable){
      wifiManager.setWifiEnabled(!enable); // Disable all existing WiFi Network
    }
        else {
      if(!wifiManager.isWifiEnabled())
        wifiManager.setWifiEnabled(!enable);
    }
        
    Method[] methods = wifiManager.getClass().getDeclaredMethods();
        for (Method method : methods) {
              
          if (method.getName().equals("setWifiApEnabled")) {
            name = method.getName();
            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.SSID = SSID;
            netConfig.preSharedKey = PASS;
            netConfig.allowedKeyManagement.set(1);
                    netConfig.allowedAuthAlgorithms.set(0);
            
              try {
                method.invoke(wifiManager, netConfig, enable);
              }  catch (Exception ex) { 
         
                 name = "Error in method.invoke(wifiManager, netConfig, enable)";

                 }
                    
          break;
      }
    }
        return name;
} 
#END IF

On a tablet with Android 5.1.1 it works! What is the problem?

Lutz
 

DonManfred

Expert
Licensed User
Longtime User
i have in mind that there is something like
This is a restriction in Android newer versions.

This does only work if the AP is created by your app.

But i may be wrong
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
The method "setWifiApEnabled" was found. But in the try .. catch block "catch" is invoked.
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Do full code cache collection, code=90KB, data=125KB
Starting a blocking GC JitCodeCache
After code cache collection, code=78KB, data=102KB
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at de.wedersoft.musicbook.main.SetAccessPoint(main.java:18644)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at de.wedersoft.musicbook.clshotspot._setwifiapenabled(clshotspot.java:89)
at de.wedersoft.musicbook.clsserver._start(clsserver.java:138)
at de.wedersoft.musicbook.main._btnstart_click(main.java:6678)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22445)
at android.os.Handler.handleCallback(Handler.java:755)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
Caused by: java.lang.SecurityException: de.wedersoft.musicbook was not granted this permission: android.permission.WRITE_SETTINGS.
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1514)
at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1547)
... 25 more
EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0
Top