JavaShuo
栏目
标签
WifiManager自动链接wifi接入点
时间 2019-11-18
标签
wifimanager
自动
链接
wifi
栏目
无线
繁體版
原文
原文链接
[java]
view plain
copy
<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
/**
*
* wifi管理类
*
*
*/
public
class
WifiOperator
{
/**
* wifiManager
*/
private
WifiManager wm;
/**
* 上下文
*/
private
Context mContext;
/**
* 数据库配置信息
*/
private
NetworkConfigDbHelper configDBHelper;
/**
* <默认构造函数>
* @param context 上下文
*/
public
WifiOperator(Context context)
{
mContext = context;
wm = (WifiManager)mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
configDBHelper =
new
NetworkConfigDbHelper(context);
}
/**
* 切换网络
* @param type 网络类型(1为中心网络 2 为车载网络)
* @return 结果码 -1-本地设置为空;0-链接成功;1-已经为当前链接
* @see [类、类#方法、类#成员]
*/
public
int
access2Wifi(String type)
{
// 获取本地的网络配置信息
NetworkConfigBean setting = configDBHelper.getNetworkConfigBySettingType(type).get(type);
if
(setting ==
null
)
{
// 本地设置为空
return
-
1
;
}
// 无线未打开时,开启无线
if
(!wm.isWifiEnabled() && WifiManager.WIFI_STATE_ENABLING != wm.getWifiState())
{
wm.setWifiEnabled(
true
);
}
// 获取本地的配置信息
String sSSID =
"\""
+ setting.getSsid() +
"\""
;
String sKey =
"\""
+ setting.getPassword() +
"\""
;
int
encryptionType = getKeyMgmtType(setting.getEncryptionType());
List<WifiConfiguration> configurations = wm.getConfiguredNetworks();
WifiConfiguration config =
null
;
boolean
isExisted =
false
;
int
networkId = -
1
;
for
(
int
i = configurations.size() -
1
; i >=
0
; i--)
{
config = configurations.get(i);
if
(config.SSID.equals(sSSID))
{
networkId = config.networkId;
isExisted =
true
;
break
;
}
}
if
(!isExisted)
{
// 安全类型 无、WEP(128)、WPA(TKIP)、WPA2(AES)
config =
new
WifiConfiguration();
// 名称
config.SSID = sSSID;
config.allowedKeyManagement.set(encryptionType);
if
(encryptionType !=
0
)
{
// 密码
config.preSharedKey = sKey;
}
config.hiddenSSID =
false
;
config.priority =
30
;
config.status = WifiConfiguration.Status.ENABLED;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
// 必须添加,不然无线路由没法链接
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
networkId = wm.addNetwork(config);
if
(networkId != -
1
)
{
wm.saveConfiguration();
}
}
else
{
// 获取当前的wifi链接
WifiInfo curConnection = wm.getConnectionInfo();
if
(curConnection !=
null
&& sSSID.equals(curConnection.getSSID()))
{
// 已是当前链接
return
1
;
}
config.allowedKeyManagement.set(encryptionType);
if
(encryptionType !=
0
)
{
// 密码
config.preSharedKey = sKey;
}
wm.updateNetwork(config);
}
if
(networkId != -
1
)
{
wm.disconnect();
wm.enableNetwork(networkId,
true
);
}
return
0
;
}
/**
* 获取加密类型
* @param type 加密类型
* @return 加密类型
* @see [类、类#方法、类#成员]
*/
private
int
getKeyMgmtType(String type)
{
if
(type ==
null
)
{
return
WifiConfiguration.KeyMgmt.NONE;
}
if
(
"WEP"
.equals(type))
{
return
WifiConfiguration.KeyMgmt.IEEE8021X;
}
else
if
(
"WPA-PSK"
.equals(type))
{
return
WifiConfiguration.KeyMgmt.WPA_PSK;
}
else
if
(
"WPA2-PSK"
.equals(type))
{
return
WifiConfiguration.KeyMgmt.WPA_PSK;
}
return
WifiConfiguration.KeyMgmt.NONE;
}
}
上一篇
使用WindowManager进行progress提示
下一篇
Android 结束进程的几种方法
相关文章
1.
android Wifi自动链接
2.
win10 系统 wifi自动断开链接 wifi热点不稳定
3.
android自动链接接入本身WIfi热点的设备的IP地址
4.
ESP32 / ESP8266 MicroPython教程:自动链接WiFi
5.
Wifi没法自动链接的问题
6.
android自动链接指定wifi
7.
Windows下自动链接WiFi 脚本
8.
iOS开发APP内自动链接wifi
9.
wpa_cli 链接 wifi
10.
centos8 链接wifi
更多相关文章...
•
Markdown 链接
-
Markdown 教程
•
PHP 连接 MySQL
-
PHP教程
•
SpringBoot中properties文件不能自动提示解决方法
•
IntelliJ IDEA中SpringBoot properties文件不能自动提示问题解决
相关标签/搜索
自链接
链接
接点
动态链接库
wifimanager
外部链接
MySQL链接
超链接
超级链接
无线
MyBatis教程
PHP教程
SQLite教程
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
js中 charCodeAt
2.
Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
3.
【Android】日常记录:BottomNavigationView自定义样式,修改点击后图片
4.
maya 文件检查 ui和数据分离 (一)
5.
eclipse 修改项目的jdk版本
6.
Android InputMethod设置
7.
Simulink中Bus Selector出现很多? ? ?
8.
【Openfire笔记】启动Mac版Openfire时提示“系统偏好设置错误”
9.
AutoPLP在偏好标签中的生产与应用
10.
数据库关闭的四种方式
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
1.
android Wifi自动链接
2.
win10 系统 wifi自动断开链接 wifi热点不稳定
3.
android自动链接接入本身WIfi热点的设备的IP地址
4.
ESP32 / ESP8266 MicroPython教程:自动链接WiFi
5.
Wifi没法自动链接的问题
6.
android自动链接指定wifi
7.
Windows下自动链接WiFi 脚本
8.
iOS开发APP内自动链接wifi
9.
wpa_cli 链接 wifi
10.
centos8 链接wifi
>>更多相关文章<<