wifimanager有个函数叫setWifiApEnabled
参数为1,WifiConfiguration;2,boolean
第二个变量设置为true标示开,false表示关。这个函数是hide的,需要进行反射。
不知道反射的建议去深入看下,不要向我一样因为项目忙加上惰性就没深入,结果找工作现场写都忘了怎么写了。
如下展示一下打开热点的函数:
private boolean stratWifiAp(String wifiName) {
Method method1 = null;
boolean ret = false;
try {
method1 = mWifiManager.getClass().getMethod("setWifiApEnabled",
WifiConfiguration.class, boolean.class);
//下面这句话是在创建WifiConfiguration,这个函数
WifiConfiguration apConfig = new WifiConfiguration();
//设置apConfig
.......
ret = (Boolean) method1.invoke(mWifiManager, apConfig, true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
LogUtil.d(TAG, "stratWifiAp() IllegalArgumentException e");
} catch (IllegalAccessException e) {
e.printStackTrace();
LogUtil.d(TAG, "stratWifiAp() IllegalAccessException e");
} catch (InvocationTargetException e) {
e.printStackTrace();
LogUtil.d(TAG, "stratWifiAp() InvocationTargetException e");
} catch (SecurityException e) {
e.printStackTrace();
LogUtil.d(TAG, "stratWifiAp() SecurityException e");
} catch (NoSuchMethodException e) {
e.printStackTrace();
LogUtil.d(TAG, "stratWifiAp() NoSuchMethodException e");
}
return ret;
}
//最后这个是创建wificonfig的函数,其中参数就请按自己需求进行更改吧。当然也可以通过getWifiApConfiguration获取wificonfig,然后再按自己需求改某些参数.
private WifiConfiguration createPassHotWifiConfig(String mSSID,
String mPasswd) {
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = mSSID;
config.wepKeys[0] = mPasswd;
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
config.priority = 0;
return config;
}
具体各个参数什么意义请再查看下wifi相关知识了。
CCMP(Counter CBC-MAC Protocol) 计数器模式密码块链消息完整码协议
其他资料可以查看
http://blog.csdn.net/mengweiqi33/article/details/8541943
知识有限,有讲得不对不好的地方,敬请讨论学习。