基本概念

关于WifiConfiguration基本信息参看文章:
点此查看
也可以上官网去查看详细的api介绍连接
在此就截取部分别人已经翻译好的:
子类
class WifiConfiguration.AuthAlgorithm 公认的IEEE 802.11认证算法
class WifiConfiguration.GroupCipher 公认的组密码
class WifiConfiguration.KeyMgmt 公认的密钥管理方案
class WifiConfiguration.PairwiseCipher 公认的WPA配对密码
class WifiConfiguration.Protocol 公认的安全协议
class WifiConfiguration.Status 网络配置的可能状态
(WifiConfiguration.AuthAlgorthm用来判断加密方法。
WifiConfiguration.GroupCipher获取使用GroupCipher 的方法来进行加密。
WifiConfiguration.KeyMgmt获取使用KeyMgmt 进行。
WifiConfiguration.PairwiseCipher获取使用WPA 方式的加密。
WifiConfiguration.Protocol获取使用哪一种协议进行加密。
wifiConfiguration.Status获取当前网络的状态。

字段
public String BSSID 当设置了这个字段,这个网络配置只能被用于与访问点关联的指定MAC
public String FQDN AAA或者RADIUS等服务器的完全合格域名
public String SSID Wifi网络名称
public BitSet allowedAuthAlgorithms 该配置支持的身份验证协议集合
public BitSet allowedGroupCiphers 该配置所支持的组密码集合
public BitSet allowedKeyManagement 该配置所支持的密钥管理集合
public BitSet allowedPairwiseCiphers 该配置所支持的WPA配对密码集合
public BitSet allowedProtocols 该配置所支持的安全协议集合
public WifiEnterpriseConfig enterpriseConfig 企业详细配置指定的EAP方法,证书以及EAP相关的其他设置
public boolean hiddenSSID 设置该项会使一个网络不广播其SSID,因此这种特定的SSID只能用于浏览
public int networkId 客户端用于识别网络配置的ID
public String preSharedKey WPA-PSK使用的预共享密钥
public int priority 当选择了一个相关的网络访问点时,优先级决定了一个通过wpa客户端访问网络的优先权

设置热点及ssid

由于项目需求,我们需要在项目中去设置打开手机热点并设置ssid和密码,这个需求呢好像还挺多人有过的,但是网上一搜,大多数人的代码发现实现不了,要吗是不谦容5.0,6.0,7.0手机,要吗就是手机设置的热点无法连接上,还有的就是热点设置后没有密码,变成开发式。

最初实现方式

最初我参考了网上的代码实现如下,但一直无法设置热点的密码。
使用此方式设置,问题我一直没找到,希望有知道的哥们给我留言,帮我解困。

 private void startWifiAp() {if (mWifiManage.isWifiEnabled()) {mWifiManage.setWifiEnabled(false);}Method method = null;try {method = mWifiManage.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class, boolean.class);method.setAccessible(true);WifiConfiguration netConfig = new WifiConfiguration();netConfig.SSID =mSsid;netConfig.preSharedKey =  mPassword;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(mWifiManage, netConfig,true);} catch (Exception e) {}}
}

更改实现方式

鉴于上述的方式存在的问题,综合资料我更改了实现方式:

/*** @创建者 wyj* @创建时间 2017/10/1213:42.* @描述* @更新者 * @更新时间 2017/10/1213:42.*/public class WifiAPUtil {private static final String TAG = "WifiAPUtil";public final static boolean DEBUG = true;public static final int MESSAGE_AP_STATE_ENABLED = 1;public static final int MESSAGE_AP_STATE_FAILED = 2;//默认wifi秘密private static final String DEFAULT_AP_PASSWORD = "12345678";private static WifiAPUtil sInstance;private static Handler mHandler;private static Context mContext;private WifiManager mWifiManager;//监听wifi热点的状态变化public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED";public static final String EXTRA_WIFI_AP_STATE = "wifi_state";public static int WIFI_AP_STATE_DISABLING = 10;public static int WIFI_AP_STATE_DISABLED = 11;public static int WIFI_AP_STATE_ENABLING = 12;public static int WIFI_AP_STATE_ENABLED = 13;public static int WIFI_AP_STATE_FAILED = 14;public enum WifiSecurityType {WIFICIPHER_NOPASS, WIFICIPHER_WPA, WIFICIPHER_WEP, WIFICIPHER_INVALID, WIFICIPHER_WPA2}private WifiAPUtil(Context context) {if(DEBUG) Log.d(TAG,"WifiAPUtils construct");mContext = context;mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);IntentFilter filter = new IntentFilter();filter.addAction(WIFI_AP_STATE_CHANGED_ACTION);context.registerReceiver(mWifiStateBroadcastReceiver, filter);}protected void finalize() {if(DEBUG) Log.d(TAG,"finalize");mContext.unregisterReceiver(mWifiStateBroadcastReceiver);}public static WifiAPUtil getInstance(Context c) {if (null == sInstance)sInstance = new WifiAPUtil(c);return sInstance;}public boolean turnOnWifiAp(String str, String password,WifiSecurityType Type) {String ssid = str;//配置热点信息。WifiConfiguration wcfg = new WifiConfiguration();wcfg.SSID = new String(ssid);wcfg.networkId = 1;wcfg.allowedAuthAlgorithms.clear();wcfg.allowedGroupCiphers.clear();wcfg.allowedKeyManagement.clear();wcfg.allowedPairwiseCiphers.clear();wcfg.allowedProtocols.clear();if(Type == WifiSecurityType.WIFICIPHER_NOPASS) {if(DEBUG)Log.d(TAG, "wifi ap----no password");wcfg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN, true);wcfg.wepKeys[0] = "";wcfg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);wcfg.wepTxKeyIndex = 0;} else if(Type == WifiSecurityType.WIFICIPHER_WPA) {if(DEBUG)Log.d(TAG, "wifi ap----wpa");//密码至少8位,否则使用默认密码if(null != password && password.length() >= 8){wcfg.preSharedKey = password;} else {wcfg.preSharedKey = DEFAULT_AP_PASSWORD;}wcfg.hiddenSSID = false;wcfg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);wcfg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);wcfg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);//wcfg.allowedKeyManagement.set(4);wcfg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);wcfg.allowedProtocols.set(WifiConfiguration.Protocol.WPA);wcfg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);wcfg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);} else if(Type == WifiSecurityType.WIFICIPHER_WPA2) {if(DEBUG)Log.d(TAG, "wifi ap---- wpa2");//密码至少8位,否则使用默认密码if(null != password && password.length() >= 8){wcfg.preSharedKey = password;} else {wcfg.preSharedKey = DEFAULT_AP_PASSWORD;}wcfg.hiddenSSID = false;wcfg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);wcfg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);wcfg.allowedKeyManagement.set(4);//wcfg.allowedKeyManagement.set(4);wcfg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);wcfg.allowedProtocols.set(WifiConfiguration.Protocol.WPA);wcfg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);wcfg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);}try {Method method = mWifiManager.getClass().getMethod("setWifiApConfiguration",wcfg.getClass());Boolean rt = (Boolean)method.invoke(mWifiManager, wcfg);if(DEBUG) Log.d(TAG, " rt = " + rt);} catch (Exception e) {}return setWifiApEnabled();}//获取热点状态public int getWifiAPState() {int state = -1;try {Method method2 = mWifiManager.getClass().getMethod("getWifiApState");state = (Integer) method2.invoke(mWifiManager);} catch (Exception e) {Log.e(TAG, e.getMessage());}if(DEBUG)Log.i("WifiAP", "getWifiAPState.state " + state);return state;}private boolean setWifiApEnabled() {//开启wifi热点需要关闭wifiwhile(mWifiManager.getWifiState() != WifiManager.WIFI_STATE_DISABLED){mWifiManager.setWifiEnabled(false);try {Thread.sleep(200);} catch (Exception e) {Log.e(TAG, e.getMessage());return false;}}// 确保wifi 热点关闭。while(getWifiAPState() != WIFI_AP_STATE_DISABLED){try {Method method1 = mWifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class, boolean.class);method1.invoke(mWifiManager, null, false);Thread.sleep(200);} catch (Exception e) {
//                LogUtils.wyjlog(TAG+e.getMessage().toString());return false;}}//开启wifi热点try {Method method1 = mWifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class, boolean.class);method1.invoke(mWifiManager, null, true);Thread.sleep(200);} catch (Exception e) {
//            Log.e(TAG, e.getMessage());return false;}return true;}//关闭WiFi热点public void closeWifiAp() {if (getWifiAPState() != WIFI_AP_STATE_DISABLED) {try {Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");method.setAccessible(true);WifiConfiguration config = (WifiConfiguration) method.invoke(mWifiManager);Method method2 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);method2.invoke(mWifiManager, config, false);} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}}public void regitsterHandler(Handler handler){mHandler = handler;}public void unregitsterHandler(){mHandler = null;}//监听wifi热点状态变化private BroadcastReceiver mWifiStateBroadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if(DEBUG)Log.i(TAG,"WifiAPUtils onReceive: "+intent.getAction());if(WIFI_AP_STATE_CHANGED_ACTION.equals(intent.getAction())) {int cstate = intent.getIntExtra(EXTRA_WIFI_AP_STATE, -1);if(cstate == WIFI_AP_STATE_ENABLED) {if(mHandler != null){mHandler.sendEmptyMessage(MESSAGE_AP_STATE_ENABLED);}}if(cstate == WIFI_AP_STATE_DISABLED  || cstate == WIFI_AP_STATE_FAILED) {if(mHandler != null)mHandler.sendEmptyMessage(MESSAGE_AP_STATE_FAILED);}}}};//获取热点ssidpublic String getValidApSsid() {try {Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");WifiConfiguration configuration = (WifiConfiguration)method.invoke(mWifiManager);return configuration.SSID;} catch (Exception e) {Log.e(TAG, e.getMessage());return null;}}//获取热点密码public String getValidPassword(){try {Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");WifiConfiguration configuration = (WifiConfiguration)method.invoke(mWifiManager);return configuration.preSharedKey;} catch (Exception e) {Log.e(TAG, e.getMessage());return null;}}//获取热点安全类型public int getValidSecurity(){WifiConfiguration configuration;try {Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");configuration = (WifiConfiguration)method.invoke(mWifiManager);} catch (Exception e) {Log.e(TAG, e.getMessage());return WifiSecurityType.WIFICIPHER_INVALID.ordinal();}//        if(DEBUG)Log.i(TAG,"getSecurity security="+configuration.allowedKeyManagement);if(configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE)) {return WifiSecurityType.WIFICIPHER_NOPASS.ordinal();}else if(configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {return WifiSecurityType.WIFICIPHER_WPA.ordinal();}else if(configuration.allowedKeyManagement.get(4)) { //4 means WPA2_PSKreturn WifiSecurityType.WIFICIPHER_WPA2.ordinal();}return WifiSecurityType.WIFICIPHER_INVALID.ordinal();}
}

使用方式: WifiAPUtil.getInstance(this).turnOnWifiAp(mSsid, mPassword, WifiAPUtil.WifiSecurityType.WIFICIPHER_WPA2);

目前我在安卓5.0,6.0,7.0机子上都测试过没有问题,欢迎大家帮我解决第一个问题。

参考文献:http://www.jb51.net/article/109118.htm
http://blog.csdn.net/u010844304/article/details/53038562#t1
http://www.jb51.net/article/109118.htm
http://blog.csdn.net/cxq234843654/article/details/51122120
http://blog.csdn.net/king4148/article/details/70156760
http://blog.csdn.net/kongxingxing/article/details/48651785
http://blog.csdn.net/u013049709/article/details/42235829#comments
http://mikewang.blog.51cto.com/3826268/850572/
http://blog.csdn.net/huang3513/article/details/53489485

安卓设置热点ssid和密码相关推荐

  1. win7系统 无线路由关闭了ssid广播 我手动设置了SSID和密码仍然连接不上

    http://zhidao.baidu.com/link?url=KwDGWPc67avpj2OUPg5UqvtqE_80R80P3xzhNIRI1_X5WnSLG7PLEpybb4TnzDAYAB6 ...

  2. Android WiFi 热点SSID以及密码默认位数限制修改

    默认的位数配置,如下, WifiUtils.java private static final int SSID_ASCII_MIN_LENGTH = 1;private static final i ...

  3. Android rom开发:固定设置wifi热点的ip、ssid及密码

    Android9.0起,开启wifi热点的逻辑进行了调整,ip ssid 密码均为随机生成,为便于通过wifi热点进行无线通信传输,特将固定设置wifi热点的ip.ssid及密码的方法总结如下: mo ...

  4. Android 4.4 如何修改WiFi热点的默认SSID和密码

    需求:需要修改WiFi热点的默认SSID和密码 代码路径: frameworks/base/wifi/java/android/net/wifi/WifiApConfigStore.java 在Wif ...

  5. 通过 命令行PowerShell 开启、关闭 Windows 10 移动热点,可修改SSID、密码。解决神州网信政府版win10无法管理windows自带移动热点问题。

    问题一 神州网信政府版win10,可以在任务栏的WIFI图标启动移动热点,但是无法设置SSID和密码.在网上搜索好久,无解. 万能的网络解决不了,只能自己动手解决了. 问题二 我当前的WiFi驱动程序 ...

  6. Android12.0 默认开启WLAN热点设置默认热点名称和密码

    1.前言 在12.0的系统rom定制产品开发中,在产品开发中,有项目产品需求,需要在开机后默认开启热点的产品,这就需要在开机后默认打开热点而开机后完成后默认 添加开启热点,实现开启热点的功能,接下来就 ...

  7. Android 9.0 默认开启WLAN热点设置默认热点名称和密码

    1.概述 在android 9.0的系统产品开发中,在项目产品需求中,对于wifi和默认热点的使用也是常有的功能,在最近的项目开发中,需要开启WLAN热点,然后设置WLAN的默认热点 的名称和密码功能 ...

  8. Android之解决Android10.0通过热点名字和密码连接指定热点网络不能通信问题(2种解决办法)

    1 问题 Android10.0设备通过热点名字和密码连接另外一台设置指定热点,但是他们之间依然不能通信,网络不可达. Android10.0设备通过热点名字和密码连接另外一台设置指定热点,我用的是官 ...

  9. html id怎么设置密码,苹果手机怎么设置id账号和密码或重设密码

    安卓手机平时下载软件,只需要在手机对应的app市场上搜索自己所喜欢的软件进行下载安装,便可以打开使用.而 苹果 手机相对于安卓手机来说,就显的比较麻烦一些,因为苹果手机只要没有越狱,不论是下载什么软件 ...

最新文章

  1. 初学架构设计的第一步:需求、愿景与架构
  2. CephFS管理命令
  3. 1.10 字符串的替换(replace()、replaceFirst()和replaceAll())
  4. 数据恢复错误卡住 linux,如何处理ORA-00376错误的恢复问题
  5. Vue2.0+vuex+H5实现音乐播放及歌曲切换功能的方法及原理解释
  6. 解决ipfs 出现Error: can‘t publish while offline: pass `--allow-offline` to override的问题
  7. python+selenium获取cookie session_selenium获取cookie及设置cookie
  8. INF文件修改注册表
  9. 程序员的失业危机原因及应对方法汇总
  10. Bootstrap HTML编码规范之减少标签的数量
  11. python循环的基本思想是重复_python基础-循环
  12. 2020 年百度之星·程序设计大赛 - 初赛二
  13. ios音视频开发路线及技术点
  14. 实用EXCEL之考勤数据的表合并
  15. android三星滑动解锁,三星怎样取消滑动解锁
  16. 性能测试 性能测试实战(七)Jmeter分布式性能测试 influxDB Grafana Master Salve 主从配置 高并发性能测试 环境搭建 性能数据收集 性能数据可视化平台 搭建问题分析
  17. springAop原理之(三)Advised接口族
  18. 产品思维 -- 用户体验
  19. 波形发生器的工作原理
  20. 美版有锁iphone 如何上电信3G

热门文章

  1. 黑底白字html代码,如何用chrome扩展将网页变成黑底白字,用以保护视力
  2. Python之高阶函数(abs、map、reduce、filter、lambda匿名函数)
  3. Solar Putty如何修改命令提示符颜色PS1(ssh登录后执行脚本)
  4. linux云计算架构师:搭建DHCP服务和NTP网络时间同步
  5. 善用宝贝标题关键字 提高站内搜索流量
  6. mysql 数据连续不走索引6_MySql组合索引最左侧原则失效
  7. A Semi-supervised Graph Attentive Network for Financial Fraud Detection 个人总结
  8. pythonn说句心里话_《说句心里话》笑力满格 “战神”常远颠覆自我
  9. ibm服务器芯片架构,IBM服务器X架构技术综述(图)
  10. n维椭球体积公式_为了方差无偏估计为什么要用n-1?