How to create wifi tethering Hotspot in Android Marshmallow?
Asked Answered
S

1

6

I've tried to create a Wi-Fi tethering hotspot in Android Marshmallow using the following code.

public class WifiAccessManager {

    private static final String SSID = "1234567890abcdef";

    public static boolean setWifiApState(Context context, boolean enabled) {
        //config = Preconditions.checkNotNull(config);
        try {
            WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (enabled) {
                mWifiManager.setWifiEnabled(false);
            }
            WifiConfiguration conf = getWifiApConfiguration();
            mWifiManager.addNetwork(conf);

            return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static WifiConfiguration getWifiApConfiguration() {
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = SSID;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        return conf;
    }
}

But it shows the following permission problem:

java.lang.SecurityException: googleplus.tarun.info.hotspotcreation was not granted  either of these permissions: android.permission.CHANGE_NETWORK_STATE, android.permission.WRITE_SETTINGS.

Even though I have already added those on the manifest.

How can I solve the problem?

Shoreward answered 9/12, 2015 at 6:46 Comment(0)
T
1

I was working in Android Marshmallow and have found a way to create WiFi tethering as describe below. Note that according to Android 6.0 Changes Your apps can now change the state of WifiConfiguration objects only if you created these objects. And beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. Read this article to know more about this. I can see you are creating Hotspot by your own. So no issue. The permission in Manifest is given below:

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

I am using the following function to create WiFi tethering Hotspot in android marshmallow:

public void setWifiTetheringEnabled(boolean enable) {
    //Log.d(TAG,"setWifiTetheringEnabled: "+enable);
    String SSID=getHotspotName(); // my function is to get a predefined SSID
    String PASS=getHotspotPassword(); // my function is to get a predefined a Password

    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

    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")) {
            WifiConfiguration netConfig = new WifiConfiguration();
            if(!SSID.isEmpty() || !PASS.isEmpty()){
                netConfig.SSID=SSID;
                netConfig.preSharedKey = PASS;
                netConfig.hiddenSSID = false;
                netConfig.status = WifiConfiguration.Status.ENABLED;
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            }
            try {
                method.invoke(wifiManager, netConfig, enable);
                Log.e(TAG,"set hotspot enable method");
            } catch (Exception ex) {
            }
            break;
        }
    }
}

Enabling the Hotspot the function call is: setWifiTetheringEnabled(true) and for disable setWifiTetheringEnabled(false).

That's it.

N.B. Be noted that SIM less devices are not supported to use Hotspot. You will not be able to create the Hotspot on those devices without root.

Hope this will be helpful for upcoming visitors.

Tolly answered 1/3, 2017 at 8:47 Comment(2)
WRITE_SETTINGS is only available to system apps, how do we go about it?Nippur
Yes, @Manny265, you are right. It allows an application to read or write the system settings. Please read details in the this link developer.android.com/reference/android/….Tolly

© 2022 - 2024 — McMap. All rights reserved.