首页 > 其他分享 >初识Setting 应用WIFI设置

初识Setting 应用WIFI设置

时间:2023-11-03 14:45:33浏览次数:43  
标签:WIFI private 初识 Setting Wi WifiManager ACTION import Fi

最近负责的一个简单定制化的setting,需要学习Wifi这一块方面的内容。通过这篇文章来了解一下原生的Setting 处理Wifi 的方式。有错误也希望大家提出来,我改进!

使用步骤

  • 申请权限、获取系统服务 WifiManager。
  • 通过 wifiManager.startScan(); 扫描WiFi 列表 。注意这个动作是耗时的
  • 注册广播获取wifi扫描结果

简单用法

示例代码

下面是Android Studio Bito 生成的一个简单的示例代码,展示如何搜索Wi-Fi并使用列表展示。

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
 public class MainActivity extends AppCompatActivity {
    private static final int PERMISSIONS_REQUEST_CODE = 100;
    private WifiManager wifiManager;
    private List<ScanResult> wifiList;
    private ListView listView;
    private Button scanButton;
    private WifiScanReceiver wifiScanReceiver;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         listView = findViewById(R.id.listView);
        scanButton = findViewById(R.id.scanButton);
         wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
         scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scanWifi();
            }
        });
         wifiScanReceiver = new WifiScanReceiver();
    }
     private void scanWifi() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_CODE);
        } else {
            performWifiScan();
        }
    }
     private void performWifiScan() {
        registerReceiver(wifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifiManager.startScan();
    }
     private class WifiScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                wifiList = wifiManager.getScanResults();
                if (wifiList != null) {
                    List<String> wifiNames = new ArrayList<>();
                    for (ScanResult scanResult : wifiList) {
                        wifiNames.add(scanResult.SSID);
                    }
                    WifiListAdapter adapter = new WifiListAdapter(MainActivity.this, wifiNames);
                    listView.setAdapter(adapter);
                } else {
                    Toast.makeText(MainActivity.this, "No Wi-Fi networks found", Toast.LENGTH_SHORT).show();
                }
            }
            unregisterReceiver(this);
        }
    }
     @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                performWifiScan();
            } else {
                Toast.makeText(this, "Permission denied. Unable to scan Wi-Fi networks.", Toast.LENGTH_SHORT).show();
            }
        }
    }
     @Override
    protected void onDestroy() {
        super.onDestroy();
        if (wifiScanReceiver != null) {
            unregisterReceiver(wifiScanReceiver);
        }
    }
}

ScanResult

是Android中的一个类,用于表示Wi-Fi扫描的结果。当您使用Wi-Fi功能进行扫描时,将返回一个ScanResult对象的列表,每个对象表示一个扫描到的Wi-Fi网络。ScanResult类提供了一些有用的信息,可以帮助您获取和分析附近的Wi-Fi网络。以下是一些ScanResult类提供的常用信息:

  • SSID:表示Wi-Fi网络的名称。
  • BSSID:表示Wi-Fi网络的MAC地址。
  • level:表示Wi-Fi信号的强度,以dBm为单位。
  • frequency:表示Wi-Fi信号的频率。
  • capabilities:表示Wi-Fi网络的安全和认证功能。
  • timestamp:表示扫描结果的时间戳。
  • channelWidth:表示Wi-Fi信号的通道宽度。
  • centerFreq0和centerFreq1:表示Wi-Fi信号的中心频率。
  • is80211mcResponder:表示Wi-Fi网络是否支持802.11mc(Wi-Fi Round-Trip Time)协议。
    等等。

Setting 源码分析

平常可以在这里看Android 源码 http://aospxref.com/ (偶尔可能访问不了)
这里是Android 13 http://aospxref.com/android-13.0.0_r3/xref/packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java 不同的系统版本可能不一样,应该是大差不差的。

红色圈住的部分比较重要。WifiPickerTracker 是什么,接着走进去发现又继承了BaseWifiTracker。可以直接看这个基类。

BaseWifiTracker

构造方法

传参注册生命周期

/**
     * Constructor for BaseWifiTracker.
     * @param wifiTrackerInjector injector for commonly referenced objects.
     * @param lifecycle Lifecycle this is tied to for lifecycle callbacks.
     * @param context Context for registering broadcast receiver and for resource strings.
     * @param wifiManager Provides all Wi-Fi info.
     * @param connectivityManager Provides network info.
     * @param mainHandler Handler for processing listener callbacks.
     * @param workerHandler Handler for processing all broadcasts and running the Scanner.
     * @param clock Clock used for evaluating the age of scans
     * @param maxScanAgeMillis Max age for tracked WifiEntries.
     * @param scanIntervalMillis Interval between initiating scans.
     */
    BaseWifiTracker(
            @NonNull WifiTrackerInjector injector,
            @NonNull Lifecycle lifecycle, @NonNull Context context,
            @NonNull WifiManager wifiManager,
            @NonNull ConnectivityManager connectivityManager,
            @NonNull Handler mainHandler,
            @NonNull Handler workerHandler,
            @NonNull Clock clock,
            long maxScanAgeMillis,
            long scanIntervalMillis,
            BaseWifiTrackerCallback listener,
            StLifecyclering tag) {
        mInjector = injector;
        lifecycle.addObserver(this);   //这里注册了生命周期Lifecycle
        mContext = context;
        mWifiManager = wifiManager;
        mConnectivityManager = connectivityManager;
        mMainHandler = mainHandler;     //主线程处理监听器回调。
        mWorkerHandler = workerHandler;  //处理所有广播和运行扫描器的处理程序。
        mMaxScanAgeMillis = maxScanAgeMillis;  //设定ScanResult的最大存活时间
        mScanIntervalMillis = scanIntervalMillis; //启动WifiPickerTracker扫描的间隔时间
        mListener = listener;
        mTag = tag;

        mScanResultUpdater = new ScanResultUpdater(clock,
                maxScanAgeMillis + scanIntervalMillis);  
        mScanner = new BaseWifiTracker.Scanner(workerHandler.getLooper());
        sVerboseLogging = mWifiManager.isVerboseLoggingEnabled();
        updateDefaultRouteInfo();
    }

Scanner

扫描WiFi动作

@WorkerThread  
private class Scanner extends Handler {  
    private static final int SCAN_RETRY_TIMES = 3;  //扫描WiFi 最大失败次数
  
    private int mRetry = 0;  
    private boolean mIsActive;  
  
    private Scanner(Looper looper) {  
        super(looper);  
    }  
  
    private void start() {  
        Log.d("TAG", "start: 疑问不知道哪里发的广播????");
        if (!mIsActive) {  
            mIsActive = true;  
            if (isVerboseLoggingEnabled()) {  
                Log.v(mTag, "Scanner start");  
            }  
            postScan();  
        }  
    }  
  
    private void stop() {  
        mIsActive = false;  
        if (isVerboseLoggingEnabled()) {  
            Log.v(mTag, "Scanner stop");  
        }  
        mRetry = 0;  
        removeCallbacksAndMessages(null);  
    }  

    //一直重复扫描WiFi设备。
    private void postScan() {  
        if (mWifiManager.startScan()) {  
            mRetry = 0;  
        } else if (++mRetry >= SCAN_RETRY_TIMES) {  //失败3次跳出循环
            // TODO(b/70983952): See if toast is needed here  
            if (isVerboseLoggingEnabled()) {  
                Log.v(mTag, "Scanner failed to start scan " + mRetry + " times!");  
            }  
            mRetry = 0;  
            return;       
         }  
        postDelayed(this::postScan, mScanIntervalMillis);  
    }  
}

ScanResultUpdater

主要更新扫描结果

package com.android.wifitrackerlib;

import android.net.wifi.ScanResult;
import android.util.Pair;

import androidx.annotation.NonNull;

import java.time.Clock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Utility class to keep a running list of scan results merged by SSID+BSSID pair.
 *
 * Thread-safe.
 */
public class ScanResultUpdater {
    private Map<Pair<String, String>, ScanResult> mScanResultsBySsidAndBssid = new HashMap<>();
    private final long mMaxScanAgeMillis;
    private final Object mLock = new Object();
    private final Clock mClock;

    /**
     * Creates a ScanResultUpdater with no max scan age.
     *
     * @param clock Elapsed real time Clock to compare with ScanResult timestamps.
     */
    public ScanResultUpdater(Clock clock) {
        this(clock, Long.MAX_VALUE);
    }

    /**
     * Creates a ScanResultUpdater with a max scan age in milliseconds. Scans older than this limit
     * will be pruned upon update/retrieval to keep the size of the scan list down.
     */
    public ScanResultUpdater(Clock clock, long maxScanAgeMillis) {
        mMaxScanAgeMillis = maxScanAgeMillis;
        mClock = clock;
    }

    /**
     * Updates scan result list and replaces older scans of the same SSID+BSSID pair.
     * 更新扫描结果列表并替换相同SSID+BSSID对的旧扫描。
     */
    public void update(@NonNull List<ScanResult> newResults) {
        synchronized (mLock) {
            evictOldScans();

            for (ScanResult result : newResults) {
                final Pair<String, String> key = new Pair(result.SSID, result.BSSID);
                ScanResult prevResult = mScanResultsBySsidAndBssid.get(key);
                if (prevResult == null || (prevResult.timestamp < result.timestamp)) {
                    mScanResultsBySsidAndBssid.put(key, result);
                }
            }
        }
    }

    /**
     * Returns all seen scan results merged by SSID+BSSID pair.
     */
    @NonNull
    public List<ScanResult> getScanResults() {
        return getScanResults(mMaxScanAgeMillis);
    }

    /**
     * Returns all seen scan results merged by SSID+BSSID pair and newer than maxScanAgeMillis.
     * maxScanAgeMillis must be less than or equal to the mMaxScanAgeMillis field if it was set.
     */
    @NonNull
    public List<ScanResult> getScanResults(long maxScanAgeMillis) throws IllegalArgumentException {
        if (maxScanAgeMillis > mMaxScanAgeMillis) {
            throw new IllegalArgumentException(
                    "maxScanAgeMillis argument cannot be greater than mMaxScanAgeMillis!");
        }
        synchronized (mLock) {
            List<ScanResult> ageFilteredResults = new ArrayList<>();
            for (ScanResult result : mScanResultsBySsidAndBssid.values()) {
                if (mClock.millis() - result.timestamp / 1000 <= maxScanAgeMillis) {
                    ageFilteredResults.add(result);
                }
            }
            return ageFilteredResults;
        }
    }

    //清除旧的扫描结果 
    //即扫描结果的时间戳距离当前时间超过了设定的最大扫描时间,那么该扫描结果会被移除
    private void evictOldScans() {
        synchronized (mLock) {
            mScanResultsBySsidAndBssid.entrySet().removeIf((entry) ->
                    mClock.millis() - entry.getValue().timestamp / 1000 > mMaxScanAgeMillis);
        }
    }
}

生命周期

  • Lifecycle.Event.ON_START 在onStart 中注册广播接收器处理网络回调
  • Lifecycle.Event.ON_STOP 取消广播接收器的注册,网络回调,并暂停扫描机制。
/**
     * Registers the broadcast receiver and network callbacks and starts the scanning     mechanism.
     * 注册广播接收器和网络回调,并启动扫描机制。
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    @MainThread
    public void onStart() {
        mWorkerHandler.post(() -> {
            updateDefaultRouteInfo();
            IntentFilter filter = new IntentFilter();
            filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
            filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
            filter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
            filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
            filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
            filter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
            mContext.registerReceiver(mBroadcastReceiver, filter,
                    /* broadcastPermission */ null, mWorkerHandler);
            mConnectivityManager.registerNetworkCallback(mNetworkRequest, mNetworkCallback,
                    mWorkerHandler);
            NonSdkApiWrapper.registerSystemDefaultNetworkCallback(
                    mConnectivityManager, mDefaultNetworkCallback, mWorkerHandler);
            handleOnStart();
            mIsInitialized = true;
        });
    }

    /**
     * Unregisters the broadcast receiver, network callbacks, and pauses the scanning mechanism.
     * 取消广播接收器的注册,网络回调,并暂停扫描机制。
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    @MainThread
    public void onStop() {
        mWorkerHandler.post(() -> {
            mScanner.stop();
            mContext.unregisterReceiver(mBroadcastReceiver);
            mConnectivityManager.unregisterNetworkCallback(mNetworkCallback);
            mConnectivityManager.unregisterNetworkCallback(mDefaultNetworkCallback);
        });
    }



/**
*广播接收器处理网络回调
1、WiFi状态改变  WIFI_STATE_CHANGED_ACTION
2、扫描结果  SCAN_RESULTS_AVAILABLE_ACTION
3、Wi-Fi 信号强度变化  RSSI_CHANGED_ACTION
4、配置的网络发生变化 CONFIGURED_NETWORKS_CHANGED_ACTION
5、网络状态改变  NETWORK_STATE_CHANGED_ACTION
*/
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        @WorkerThread
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (isVerboseLoggingEnabled()) {
                Log.v(mTag, "Received broadcast: " + action);
            }

            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
                mWifiState = intent.getIntExtra(
                        WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
                if (mWifiState == WifiManager.WIFI_STATE_ENABLED) {
                    mScanner.start();
                } else {
                    mScanner.stop();
                }
                notifyOnWifiStateChanged();
                handleWifiStateChangedAction();
            } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
                handleScanResultsAvailableAction(intent);
            } else if (WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action)) {
                handleConfiguredNetworksChangedAction(intent);
            } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
                handleNetworkStateChangedAction(intent);
            } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
                handleRssiChangedAction();
            } else if (TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED.equals(action)) {
                handleDefaultSubscriptionChanged(intent.getIntExtra(
                        "subscription", SubscriptionManager.INVALID_SUBSCRIPTION_ID));
            }
        }
    };

标签:WIFI,private,初识,Setting,Wi,WifiManager,ACTION,import,Fi
From: https://www.cnblogs.com/mhzf/p/17807550.html

相关文章

  • Android WiFi工具类
    ✍️作者简介:沫小北/码农小北(专注于Android、Web、TCP/IP等技术方向)</br>......
  • 编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of unde
    编译第三方的前端项目时候出现下面问题 ERROR Failedtocompilewith1error                                                             ......
  • PasteSpider之appsettings.json中的Serilog的配置,分流不同日志层级的信息!
    在实际使用Serilog中,我们通常会有不一样的需求,常见的比如1.按照等级,高级哪个等级的才记录2.记录文件每个多大,超过的划分到下一个文件中3.不同等级的记录到不同的位置中4.按照不一样的格式输出以下是PasteSpider中的appsettings.json中关于Serilog的配置"Serilog":{......
  • .NET 反序列化 GetterSettingsPropertyValue 攻击链
    0x01 链路1 SettingsPropertyValueSettingsPropertyValue位于命名空间 System.Configuration,用于应用程序存储和检索设置的值,此类具有Name、IsDirty、Deserialized、PropertyValue、SerializedValue等多个公共成员,其中SerializedValue属性用于获取或者设置序列化的值,便于持久......
  • N1中openwrt实现不插网线就能上网,通过wifi连接路由器
    环境说明:路由器:N1盒子系统版本:openwrt前言既然想用这个功能,基本上就是没有网线连接盒子了,不仅限于N1盒子只要是openwrt系统就可以创建接口在openwrt界面上依次点击“网络”->“无线”,第一项是2.4GHz的wifi配置,第二项是5GHz的wifi设置,确定要转接的wifi的频率,点击对应项后面的“扫......
  • TP-LINK XDR6088 WiFi6路由器 简单开箱评测
    TL-XDR6088易展版AX6000双频WiFi6路由器简单开箱测评,双2.5G网口,双频8流,8颗FEM,支持Docker。TP-LINKXDR5470WiFi6路由器简单开箱评测:https://blog.zeruns.tech/archives/725.htmlTP-LINKXDR3040WiFi6路由器简单开箱评测:https://blog.zeruns.tech/archives/683.html参数CPU:MT79......
  • wifi密码
    如果无法下载或者不方便下载请加微:pjwifi2024请打开链接进行下载:https://pan.baidu.com/s/1-w2GgTLGy6FVU18svIbp0g?pwd=1234如果无法下载或者不方便下载请加微:pjwifi2024建议您务必添加技术好友,按照技术指导操作:技术微:pjwifi2024或者可以直接用微扫描下面二维码添加:......
  • 数据结构之散列表与哈希函数初识
    一:概述一:为什么需要散列表*在我们的程序世界里,往往也需要在内存中存放这样一个“词典”,方便我们进行高效的查询和统计。*例如开发一个学生管理系统,需要有*通过输入学号快速查找对应学生的姓名的功能。*这里不必要每次都去查询数据库,而可以在内存中建立一个缓存表,这样做可以......
  • 一种编程思想——利用settings文件实现功能的增减
    一.正常函数版本的思路1.notify.pydefwechat(content):print('微信通知:%s'%content)defqq(content):print('qq通知:%s'%content)defemail(content):print('邮箱通知:%s'%content) 2.start.pyfromnotifyimport*defsend_all(c......
  • 第一章 初识Linux
    1.Linux简介1.1Linux是多用户多任务的操作系统,免费开源,主要用于搭建服务器,性能稳定。但是Linux系统是专业系统,原始环境较为简单,大部分功能的实现都需要自我搭建。1.2  Linux系统都是由文件构成。对于操作系统内核而言,命令、硬件、软件设备都被视为一个拥有特定功能的......