1. 程式人生 > >[Android] 安卓開啟WIFI熱點程式碼 以及6.0開啟熱點報錯/異常解決方法

[Android] 安卓開啟WIFI熱點程式碼 以及6.0開啟熱點報錯/異常解決方法

前提是程式能在4.4 5.1 正常跑哦 6.0多加一個許可權即可

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

附上開啟wifi熱點程式碼

    private WifiManager wifiManager = null;

 private void startWifiAp() {
         if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }

        Method method = null;
try { method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method.setAccessible(true); WifiConfiguration netConfig = new WifiConfiguration(); netConfig.SSID = mSSID; netConfig.preSharedKey
= mPasswd; netConfig.allowedAuthAlgorithms .set(WifiConfiguration.AuthAlgorithm.OPEN); netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); netConfig.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.WPA_PSK); netConfig.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.CCMP); netConfig.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.TKIP); netConfig.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.CCMP); netConfig.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.TKIP); method.invoke(wifiManager, netConfig,true); } catch (Exception e) { Log.i(TAG, "startWifiAp: "+e.getMessage()); } }