Android如何获取当前连接wifi的信道?
【摘要】
做网络诊断,需要诊断当前wifi所在信道是否拥挤,目前只是解决了获取当前wifi所在信道,主要是用ScanResult 的frequency字段,还需要用SSID和BSSID从ScanResults匹配到...
做网络诊断,需要诊断当前wifi所在信道是否拥挤,目前只是解决了获取当前wifi所在信道,主要是用ScanResult 的frequency字段,还需要用SSID和BSSID从ScanResults匹配到当前的wifi信号wifiInfo,具体代码如下:
public static int getCurrentChannel(Context context) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();// 当前wifi连接信息
List<ScanResult> scanResults = wifiManager.getScanResults();
for (ScanResult result : scanResults) {
if (result.BSSID.equalsIgnoreCase(wifiInfo.getBSSID())
&& result.SSID.equalsIgnoreCase(wifiInfo.getSSID()
.substring(1, wifiInfo.getSSID().length() - 1))) {
return getChannelByFrequency(result.frequency);
}
}
return -1;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
/**
* 根据频率获得信道
*
* @param frequency
* @return
*/
public static int getChannelByFrequency(int frequency) {
int channel = -1;
switch (frequency) {
case 2412:
channel = 1;
break;
case 2417:
channel = 2;
break;
case 2422:
channel = 3;
break;
case 2427:
channel = 4;
break;
case 2432:
channel = 5;
break;
case 2437:
channel = 6;
break;
case 2442:
channel = 7;
break;
case 2447:
channel = 8;
break;
case 2452:
channel = 9;
break;
case 2457:
channel = 10;
break;
case 2462:
channel = 11;
break;
case 2467:
channel = 12;
break;
case 2472:
channel = 13;
break;
case 2484:
channel = 14;
break;
case 5745:
channel = 149;
break;
case 5765:
channel = 153;
break;
case 5785:
channel = 157;
break;
case 5805:
channel = 161;
break;
case 5825:
channel = 165;
break;
}
return channel;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
文章来源: chengsy.blog.csdn.net,作者:程思扬,版权归原作者所有,如需转载,请联系作者。
原文链接:chengsy.blog.csdn.net/article/details/99646211
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)