Android开启热点softAP模式

想在程序里打开softAP模式,网上抄了一段代码
5.0以上手机打不开,5.0以下的手机可以,后来发现是少添加了一个权限

    public boolean setWifiApEnabled(boolean enabled) {
        if (enabled) {
            wifiManager.setWifiEnabled(false);
        }
        try {
            WifiConfiguration apConfig = new WifiConfiguration();
            apConfig.SSID = "lucien";
            apConfig.preSharedKey = "123456789";
            apConfig.allowedAuthAlgorithms
                    .set(WifiConfiguration.AuthAlgorithm.OPEN);
            apConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            apConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            apConfig.allowedKeyManagement
                    .set(WifiConfiguration.KeyMgmt.WPA_PSK);
            apConfig.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.CCMP);
            apConfig.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.TKIP);
            apConfig.allowedGroupCiphers
                    .set(WifiConfiguration.GroupCipher.CCMP);
            apConfig.allowedGroupCiphers
                    .set(WifiConfiguration.GroupCipher.TKIP);
            Method method = wifiManager.getClass().getMethod(
                    "setWifiApEnabled", WifiConfiguration.class, boolean.class);
            method.setAccessible(true);
            return (Boolean) method.invoke(wifiManager, apConfig, enabled);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

上面这段代码在三星的手机上可以设置密码成功,但是在国产华为、小米、中兴、魅族等手机上可以打开AP模式,
但是设置密码均失败,好吧,这个并不是重点,有空再研究。

你可能感兴趣的:(Android开启热点softAP模式)