How to create wifi tethering Hotspot in Android Marshmallow?

假装没事ソ 提交于 2019-12-05 07:52:22

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!