Android获取wifi列表的方法
更新时间:2021年10月19日 11:04:51 作者:习作
这篇文章主要为大家详细介绍了Android获取wifi列表的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android获取wifi列表的具体代码,供大家参考,具体内容如下
接着上篇的获取蓝牙列表,这次我们来获取android设备的wifi列表,话不多少直接上代码!
1.首先我们先获取wifi权限
<!-- 以下是使用wifi访问网络所需的权限 --> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />
接下来我们直接来定义wifiUtil
public class WifiUtil { private Context context; public WifiUtil(Context context) { this.context = context; } // TODO: 2021/9/15 获取本机WIFI设备详细信息 @SuppressLint("MissingPermission") public void getDetailsWifiInfo() { StringBuffer sInfo = new StringBuffer(); WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo mWifiInfo = mWifiManager.getConnectionInfo(); int Ip = mWifiInfo.getIpAddress(); String strIp = "" + (Ip & 0xFF) + "." + ((Ip >> 8) & 0xFF) + "." + ((Ip >> 16) & 0xFF) + "." + ((Ip >> 24) & 0xFF); sInfo.append("\n--BSSID : " + mWifiInfo.getBSSID()); sInfo.append("\n--SSID : " + mWifiInfo.getSSID()); sInfo.append("\n--nIpAddress : " + strIp); sInfo.append("\n--MacAddress : " + mWifiInfo.getMacAddress()); sInfo.append("\n--NetworkId : " + mWifiInfo.getNetworkId()); sInfo.append("\n--LinkSpeed : " + mWifiInfo.getLinkSpeed() + "Mbps"); sInfo.append("\n--Rssi : " + mWifiInfo.getRssi()); sInfo.append("\n--SupplicantState : " + mWifiInfo.getSupplicantState()+mWifiInfo); sInfo.append("\n\n\n\n"); Log.d("getDetailsWifiInfo", sInfo.toString()); } // TODO: 2021/9/15 获取附近wifi信号 public List<String> getAroundWifiDeviceInfo() { StringBuffer sInfo = new StringBuffer(); WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo mWifiInfo = mWifiManager.getConnectionInfo(); List<ScanResult> scanResults = mWifiManager.getScanResults();//搜索到的设备列表 List<ScanResult> newScanResultList = new ArrayList<>(); for (ScanResult scanResult : scanResults) { int position = getItemPosition(newScanResultList,scanResult); if (position != -1){ if (newScanResultList.get(position).level < scanResult.level){ newScanResultList.remove(position); newScanResultList.add(position,scanResult); } }else { newScanResultList.add(scanResult); } } List<String> stringList = new ArrayList<>(); for (int i = 0; i <newScanResultList.size() ; i++) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("设备名(SSID) ->" + newScanResultList.get(i).SSID + "\n"); stringBuilder.append("信号强度 ->" + newScanResultList.get(i).level + "\n"); stringBuilder.append("BSSID ->" + newScanResultList.get(i).BSSID + "\n"); stringBuilder.append("level ->" + newScanResultList.get(i).level + "\n"); stringBuilder.append("采集时间戳 ->" +System.currentTimeMillis() + "\n"); stringBuilder.append("rssi ->" + (mWifiInfo != null && (mWifiInfo.getSSID().replace("\"", "")).equals(newScanResultList.get(i).SSID) ? mWifiInfo.getRssi() : null) + "\n"); //是否为连接信号(1连接,默认为null) stringBuilder.append("是否为连接信号 ->" + (mWifiInfo != null && (mWifiInfo.getSSID().replace("\"", "")).equals(newScanResultList.get(i).SSID) ? 1: null) + "\n"); stringBuilder.append("信道 - >" +getCurrentChannel(mWifiManager) + "\n"); //1 为2.4g 2 为5g stringBuilder.append("频段 ->" + is24GOr5GHz(newScanResultList.get(i).frequency)); stringList.add(stringBuilder.toString()); } Log.d("getAroundWifiDeviceInfo", sInfo.toString()); return stringList; } public static String is24GOr5GHz(int freq) { if (freq > 2400 && freq < 2500){ return "1"; }else if (freq > 4900 && freq < 5900){ return "2"; }else { return "无法判断"; } } /** * 返回item在list中的坐标 */ private int getItemPosition(List<ScanResult>list, ScanResult item) { for (int i = 0; i < list.size(); i++) { if (item.SSID.equals(list.get(i).SSID)) { return i; } } return -1; } public static int getCurrentChannel(WifiManager wifiManager) { 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; } /** * 根据频率获得信道 * * @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; } }
记得获取wifi的过程中要去掉重复的信号哈,去重代码
for (ScanResult scanResult : scanResults) { int position = getItemPosition(newScanResultList,scanResult); if (position != -1){ if (newScanResultList.get(position).level < scanResult.level){ newScanResultList.remove(position); newScanResultList.add(position,scanResult); } }else { newScanResultList.add(scanResult); } }
2.在需要获取列表的地方设置数据,记得动态获取权限哈
// TODO: 2021/9/18 设置wifi管理器并动态获取手机权限 @SuppressLint({"CheckResult","NotifyDataSetChanged"}) private void setWifiManager() { RxPermissions rxPermissions = new RxPermissions(this); rxPermissions.requestEach(Manifest.permission.ACCESS_FINE_LOCATION) .subscribe(permission -> { if (permission.name.equalsIgnoreCase(Manifest.permission.ACCESS_FINE_LOCATION)) { wifiUtil = new WifiUtil(this); wifiListAdapter.getDatas().clear(); wifiList.clear(); wifiList.add(getString(R.string.wifi_list)); wifiList.addAll(wifiUtil.getAroundWifiDeviceInfo()); for (int i = 0; i < wifiList.size(); i++) { typeBeanList.add(new TypeBean(true, wifiList.get(i))); } wifiListAdapter.notifyDataSetChanged(); } }); }
3.展示结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android Vitamio和ExoPlayer两种播放器优劣分析
Vitamio和ExoPlayer都是用于安卓平台的视频播放器库,它们各有优缺点,具体使用哪一个,需要根据你的实际需求、开发经验、项目规模等多个因素综合考虑2023-04-04Android中分析Jetpack Compose动画内部的实现原理
这篇文章主要介绍了Android中分析Jetpack Compose动画内部的实现原理,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下2022-09-09
最新评论