Android开启热点softAP模式

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

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

    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模式,
但是设置密码均失败,好吧,这个并不是重点,有空再研究。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,239评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,092评论 19 139
  • 让app 的service常驻其实是很流氓的做法,但是需求摆在那里。。。 但是要清除一点:想百分百保活servic...
    PengPengPro阅读 16,483评论 10 65
  • ♀♀ 我们都是不是不想爱,不是不敢爱,而是爱情从来都讲究势均力敌。 无论是才识,实力,还是情趣,都是只在同一水平上...
    南方小余阅读 4,237评论 2 2
  • 古风 瑞士 温志龄 欧洲屋脊人如潮,世界公园风光娇。 民殷国富百业旺,金融诚信独占鳌。 注:瑞士因海拔较高,有...
    碧野牧歌阅读 1,535评论 0 3

友情链接更多精彩内容