文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android获取wifi列表的方法

2024-04-02 19:55

关注

本文实例为大家分享了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 "无法判断";
        }
    }
 
    
    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;
    }
 
    
    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.展示结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯