Android WiFi使用记录
最近在做Android的WiFi部分的开发,连接的工具类参照了这个文章的工具类。
http://www.cnblogs.com/zhuqiang/p/3566686.html
开发中碰上的一些问题,在这里对一些解决办法做了些记录。
1.对于WiFi加密方式的识别
String capabilities = scanResult.capabilities;
if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
((WifiHolder) holder).setType(WifiConnector.WifiCipherType.WIFICIPHER_WPA);
} else if (capabilities.contains("WEP") || capabilities.contains("wep")) {
((WifiHolder) holder).setType(WifiConnector.WifiCipherType.WIFICIPHER_WEP);
} else {
((WifiHolder) holder).setType(WifiConnector.WifiCipherType.WIFICIPHER_NOPASS);
} 通过获取scanResult的capabilities来判断WiFi的加密方式
2.连接WiFi的成功率
开发中发现当我输入密码正确时,没能连接到我的指定wifi。目前发现原因是由于WifiManager会保存历史的WiFi配置造成了影响。
public List<WifiConfiguration> cleanWifiConfiguredNetworks(String ssid) {
List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks(); for (int i = 0; i < configuredNetworks.size(); i++) {
wifiManager.removeNetwork(configuredNetworks.get(i).networkId);
}
return configuredNetworks;
}
很简单直接清空了历史配置或者设置其他为不可用。
3.wifi连接成功的监听
不太喜欢广播监听,各种问题很多,我直接对当前连接wifi信息进行判断。
先通过wifiManager.getConnectionInfo() 获取当前的连接WiFi信息,
connectionInfo.getSSID().contains(ssid) ||
connectionInfo.getSupplicantState() != SupplicantState.COMPLETED 再判断wifi的ssid以及wifimanager的状态,两条件都满足时则WiFi连接成功。
项目需求我这接下来要进行socket通信,这种状态下获取的IP地址存在延迟增加了新的条件判断,对于wifiinfo中的MeteredHint参数额外的判断。当这变为true
时,获取了正确IP。这个参数获取方法给hide,不能调用需反射才能成功使用代码如下: //通过反射方法调用hide方法
public boolean getWifiInfoHint(WifiInfo connectionInfo) throws InstantiationException {
Class aClass = connectionInfo.getClass();
try {
Method getMeteredHint = aClass.getDeclaredMethod("getMeteredHint");
getMeteredHint.setAccessible(true);
Object invoke = getMeteredHint.invoke(connectionInfo);
return (boolean) invoke;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false;
} 获取服务器的IP地址方法:
DhcpInfo info = wifiManage.getDhcpInfo();
String serverAddress = intToIp(info.serverAddress); // 将获取的int转为真正的ip地址,参考的网上的,修改了下
private String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
}
OK 结束 修改过后的全部代码如下: package com.dopool.usersystem.hotspot; import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log; import com.dopool.usersystem.Constant;
import com.zhy.http.okhttp.utils.L; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; public class WifiConnector {
public Handler mHandler;
WifiManager wifiManager; /**
* 向UI发送消息
*
* @param info 消息
*/
public void sendMsg(String info) {
if (mHandler != null) {
Message msg = new Message();
msg.obj = info;
mHandler.sendMessage(msg);// 向Handler发送消息
} else {
Log.e("wifi", info);
}
} //WIFICIPHER_WEP是WEP ,WIFICIPHER_WPA是WPA,WIFICIPHER_NOPASS没有密码
public enum WifiCipherType {
WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
} // 构造函数
public WifiConnector(Context context) {
// 取得WifiManager对象
this.wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
} // 提供一个外部接口,传入要连接的无线网
public void connect(String ssid, String password, WifiCipherType type) {
Thread thread = new Thread(new ConnectRunnable(ssid, password, type));
thread.start();
} // 查看以前是否也配置过这个网络
private WifiConfiguration isExsits(String SSID) {
List<WifiConfiguration> existingConfigs = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs) {
if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
return existingConfig;
}
}
return null;
} private WifiConfiguration createWifiInfo(String SSID, String Password,
WifiCipherType Type) {
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
// nopass
if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
config.allowedKeyManagement.set(KeyMgmt.NONE);
}
// wep
if (Type == WifiCipherType.WIFICIPHER_WEP) {
if (!TextUtils.isEmpty(Password)) {
if (isHexWepKey(Password)) {
config.wepKeys[0] = Password;
} else {
config.wepKeys[0] = "\"" + Password + "\"";
}
}
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
// wpa
if (Type == WifiCipherType.WIFICIPHER_WPA) {
config.preSharedKey = "\"" + Password + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.set(AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
// 此处需要修改否则不能自动重联
// config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
} // 打开wifi功能
private boolean openWifi() {
boolean bRet = true;
if (!wifiManager.isWifiEnabled()) {
bRet = wifiManager.setWifiEnabled(true);
}
return bRet;
} private boolean closeifi() {
boolean bRet = true;
if (wifiManager.isWifiEnabled()) {
bRet = wifiManager.setWifiEnabled(false);
}
return bRet;
} class ConnectRunnable implements Runnable {
private String ssid; private String password; private WifiCipherType type; public ConnectRunnable(String ssid, String password, WifiCipherType type) {
this.ssid = ssid;
this.password = password;
this.type = type;
} @Override
public void run() {
try {
// 打开wifi
openWifi();
sendMsg("opened");
Thread.sleep(200);
// 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
// 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
try {
// 为了避免程序一直while循环,让它睡个100毫秒检测……
Thread.sleep(100);
} catch (InterruptedException ie) {
}
} //去掉以往的WiFi配置
List<WifiConfiguration> saveConfigureNetworks = cleanWifiConfiguredNetworks(ssid); WifiConfiguration wifiConfig = createWifiInfo(ssid, password,
type); if (wifiConfig == null) {
sendMsg("wifiConfig is null!");
return;
} int netID = wifiManager.addNetwork(wifiConfig);
boolean enabled = wifiManager.enableNetwork(netID, true);
boolean connected = wifiManager.reconnect(); WifiInfo connectionInfo = wifiManager.getConnectionInfo(); int num = 0;
int sum = 25;
if (ssid.contains("DoPool")) {
sum = 35;
}
while (!connectionInfo.getSSID().contains(ssid) ||
connectionInfo.getSupplicantState() != SupplicantState.COMPLETED ||
ssid.contains("DoPool") ? !getWifiInfoHint(connectionInfo) : false) {
sendMsg(connectionInfo.getSSID() + "dd" + ssid + connectionInfo.getSupplicantState() + getWifiInfoHint(connectionInfo));
connectionInfo = wifiManager.getConnectionInfo();
try {
num++;
if (num == sum) {
sendMsg(ssid + "exception");
return;
} // 为了避免程序一直while循环,让它睡个100毫秒检测……
Thread.sleep(200);
} catch (InterruptedException ie) {
}
} for (int i = 0; i < saveConfigureNetworks.size(); i++) {
wifiManager.addNetwork(saveConfigureNetworks.get(i));
} sendMsg(connectionInfo.toString());
sendMsg(ssid + "连接成功!");
} catch (Exception e) {
// TODO: handle exception
sendMsg(ssid + "exception");
e.printStackTrace();
}
}
} private static boolean isHexWepKey(String wepKey) {
final int len = wepKey.length(); // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
if (len != 10 && len != 26 && len != 58) {
return false;
} return isHex(wepKey);
} private static boolean isHex(String key) {
for (int i = key.length() - 1; i >= 0; i--) {
final char c = key.charAt(i);
if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a'
&& c <= 'f')) {
return false;
}
}
return true;
} public List<WifiConfiguration> cleanWifiConfiguredNetworks(String ssid) {
List<WifiConfiguration> configuredNetworks = wifiManager
.getConfiguredNetworks(); for (int i = 0; i < configuredNetworks.size(); i++) {
wifiManager.removeNetwork(configuredNetworks.get(i).networkId);
}
return configuredNetworks;
} ArrayList<ScanResult> list; public List<ScanResult> getNoDopoolWifi() {
List<ScanResult> mSortWifi = getSortWifi();
//去掉带Dopool的wifi
for (int i = 0; i < mSortWifi.size(); i++) {
if (mSortWifi.get(i).SSID.contains("DoPool")) {
mSortWifi.remove(i);
}
}
return mSortWifi;
} public List<ScanResult> getSortWifi() { wifiManager.startScan();
list = (ArrayList<ScanResult>) wifiManager.getScanResults();
ArrayList<ScanResult> scanResults = new ArrayList<>(); //去掉重复的WiFi SSID的名字
for (int i = 0; i < list.size(); i++) {
if (list.get(i).SSID.equals("") || list.get(i).frequency > 3000) {
continue;
} for (int j = 0; j <= scanResults.size(); j++) {
if (j == scanResults.size()) {
scanResults.add(list.get(i));
break;
} else if (scanResults.get(j).SSID.equals(list.get(i).SSID)) {
break;
}
}
}
//按照信号强度排序
for (int i = 0; i < scanResults.size(); i++) {
for (int j = 0; j < scanResults.size(); j++) {
if (scanResults.get(i).level > scanResults.get(j).level) {
ScanResult temp = null;
temp = scanResults.get(i);
scanResults.set(i, scanResults.get(j));
scanResults.set(j, temp);
}
}
}
return scanResults;
} //通过反射方法调用hide方法
public boolean getWifiInfoHint(WifiInfo connectionInfo) throws InstantiationException {
Class aClass = connectionInfo.getClass();
try {
Method getMeteredHint = aClass.getDeclaredMethod("getMeteredHint");
getMeteredHint.setAccessible(true);
Object invoke = getMeteredHint.invoke(connectionInfo);
return (boolean) invoke;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false;
} }
Android WiFi使用记录的更多相关文章
- Android WiFi 日志记录(ASSOC_REJECT)
记录Android N关联拒绝之后的相关的log. 10-26 20:35:35.844 2215 2215 D wpa_supplicant: * bssid_hint=44:48:c1:d6:57 ...
- Android WiFi 日志记录(四次握手)
记录一下四次握手的log. PMK: PMK(Pairwise Master Key,成对主密钥 STA和AP得到PMK后,将进行密匙派生以得到PTK.最后,PTK被设置到硬件中, 用于数据的加解密. ...
- S3c6410 平台 Android系统的Wi-Fi调试记录
硬件平台:S3c6410 操作系统:Android 网卡芯片:GH381(SDIO接口 sdio8688) 1.SDIO驱动 因为是SDIO接口,所以请先保证mmc驱动(代码在“kernel\driv ...
- Android WIFI 分析(一)
本文基于<深入理解Android WiFi NFC和GPS 卷>和 Android N 代码结合分析 WifiService 是 Frameworks中负责wifi功能的核心服务,它主 ...
- android wifi P2P CONNECT, INVITE和JOIN流程选择
android wifi P2P CONNECT, INVITE和JOIN流程选择
- android wifi ANR问题分析总结
android wifi ANR问题分析总结 1 看看main进程阻塞在那里? 2 调用关系的函数阻塞在那里? 3 最终阻塞函数的阻塞前的log以及状态
- arcgis android 图上记录gps轨迹
原文 arcgis android 图上记录gps轨迹 public class MainActivity extends Activity { MapView mMapView; Location ...
- android wifi驱动移植详细过程
转自:http://bbs.imp3.net/thread-10558924-1-1.html 对于刚入手android没多久的人来说,android wifi 驱动的移植确实还是有难度的,不过参考了 ...
- Android WIFI 启动流程(TIP^^)
前几天因为解决一堆Bug,没时间写.我不会每天都写,就是为了存档一些资料. 内容来源:工作中接触到的+高手博客+文档(Books)=自己理解 仅限参考^^ 此博客是上一个<<Android ...
随机推荐
- $bzoj4569$
$st表+并查集$ $考虑暴力方法:我们每次将对应相等的位置用并查集连起来,那么最终答案就是9*10^{连通块个数-1}$ $很明显上面这个办法过不去,问题在于重复次数太多了,如果一个区间已经对应相等 ...
- SyntaxError: can't assign to operator
变量名不能有'-'
- 面试题:sql数据查询
前几天参加一个面试,面试公司让做一套题,sql题不是很难,但是我第一次还是写错了,回来后,重新写了下.简单记录下吧, 1.题目: 2.测试数据 select * from student ; inse ...
- WordCount作业提交到FileInputFormat类中split切分算法和host选择算法过程源码分析
参考 FileInputFormat类中split切分算法和host选择算法介绍 以及 Hadoop2.6.0的FileInputFormat的任务切分原理分析(即如何控制FileInputForm ...
- Flutter实战视频-移动电商-16.补充_保持页面状态
16.补充_保持页面状态 修正一个地方: 设置了item的高度为380 横向列表为380.最终build的高度也增加了50为430. 增加了上面的高度以后,下面那个横线划掉的价格可以显示出来了. 但是 ...
- Eclipse中建立自己的类库,给不同的工程使用
win7 进入服务 开始 运行 services.msc 在多个工程当中,可能使用到相同的jar包,这时,如果我们建立一个自己的类库,该类库中存放着所有工程均需要的jar包,就可以免去重复导入的麻烦. ...
- struts2注解的作用
Struts2注解 1 Struts2注解的作用 使用注解可以用来替换struts.xml配置文件!!! 2 导包 必须导入struts2-convention-plugin-2.3.15.jar包, ...
- jquery快速入门三
事件 常用事件 click(function(){.......}) #触发或将函数绑定到指定元素的click事件 hover(function(){.....}) 当鼠标指针悬停在上面时触发.... ...
- POJ 3662 Telephone Lines (二分+dijkstra)
题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...
- LeetCode: 448 Find All Numbers Disappeared in an Array(easy)
题目: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice an ...