Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Lollipop - WiFi Hotspot setWifiApEnabled() get InvocationTargetException

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, some phones with 4.4,...) but I tested it with Samsung Galaxy S5 and I get

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
...

I'm using similar code like there Android 2.2 wifi hotspot API or How and what to set to Android WifiConfiguration.preSharedKey to connect to the WPA2 PSK WiFi network but It´s a little old code.

Do you have experience with it? What do you suggest?

P.S. it's not working also in some Android 4.4 devices (but I don't get InvocationTargetException).

like image 470
Pepa Zapletal Avatar asked May 29 '15 10:05

Pepa Zapletal


3 Answers

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

like image 124
Sahil Bahl Avatar answered Nov 11 '22 12:11

Sahil Bahl


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.

like image 3
vldmkr Avatar answered Nov 11 '22 11:11

vldmkr


I had the same problem.

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

like image 2
unrealsoul007 Avatar answered Nov 11 '22 12:11

unrealsoul007