Android Lollipop - WiFi Hotspot setWifiApEnabled() get InvocationTargetException

前端 未结 5 1844
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-10 11:12

I\'m using in the app

setWifiApEnabled()

from Hidden API (access by reflection). In some older phone it´s working (also with Samsung Galaxy S3,

相关标签:
5条回答
  • 2021-02-10 11:20

    I found the cause:

    Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10260 nor current process has android.permission.CONNECTIVITY_INTERNAL.
    

    Info:

    How can system app located in /system/app have system permission in Android 4.4 KitKat build?

    It´s a little big problem :)

    0 讨论(0)
  • 2021-02-10 11:26
    public void setWiFiApMode(boolean mode) {
            if (mContext == null) return;
            WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            if (wifiManager == null) return;
            try {
                Method setWifiApEnabled = WifiManager.class.getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                setWifiApEnabled.invoke(wifiManager, null, mode);
            } catch (Exception e) {
            }
        }
    

    and

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    

    Fully works on Android N.

    0 讨论(0)
  • 2021-02-10 11:32

    I had the same problem.

    Added android.permission.WRITE_SETTINGS to the manifest file and it worked. Try this.

    0 讨论(0)
  • 2021-02-10 11:39

    You need to add these permissions to your AndroidManifest.xml:

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    It works for me.

    0 讨论(0)
  • 2021-02-10 11:43

    Try This.

    ConnectivityManager cman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Method[] methods = cman.getClass().getMethods();
    
            try
            {
                wifiManager.setWifiEnabled(false);
                Method enableWifi = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                String ssid  =   " " //your SSID 
                String pass  =   " " // your Password
                WifiConfiguration  myConfig =  new WifiConfiguration();
                myConfig.SSID = ssid;
                myConfig.preSharedKey  = pass ;
                myConfig.status = WifiConfiguration.Status.ENABLED;
                myConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                myConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                myConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
                myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
                myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
                myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                result = (Boolean) enableWifi.invoke(wifiManager, myConfig, true);
    
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                result = false;
            }
    

    In case of your exception try add this permission to your Manifest android.permission.WRITE_SETTINGS

    0 讨论(0)
提交回复
热议问题