Android 8 wifi 扫描时间间隔
wifi setting界面扫描时间间隔:10s
不在wifi setting界面,扫描时间间隔,最小20s,然后按找2倍的间隔进行递增,40s,60s..., 最大160s
PNO 即Preferred Network Offload,用于系统在休眠的时候连接WiFi
1. wifi setting界面
packages\apps\Settings\src\com\android\settings\wifi\WifiSettings.java
public void onStart() {
mWifiTracker.startTracking();
frameworks\base\packages\SettingsLib\src\com\android\settingslib\wifi\WifiTracker.java
public void startTracking() {
synchronized (mLock) {
registerScoreCache();
// 是否显示wifi评分的UI
mNetworkScoringUiEnabled =
Settings.Global.getInt(
mContext.getContentResolver(),
Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1;
// 是否显示wifi速度
mMaxSpeedLabelScoreCacheAge =
Settings.Global.getLong(
mContext.getContentResolver(),
Settings.Global.SPEED_LABEL_CACHE_EVICTION_AGE_MILLIS,
DEFAULT_MAX_CACHED_SCORE_AGE_MILLIS);
resumeScanning();
if (!mRegistered) {
mContext.registerReceiver(mReceiver, mFilter);
// NetworkCallback objects cannot be reused. http://b/20701525 .
mNetworkCallback = new WifiTrackerNetworkCallback();
mConnectivityManager.registerNetworkCallback(mNetworkRequest, mNetworkCallback);
mRegistered = true;
}
}
}
扫描服务
public void resumeScanning() {
if (mScanner == null) {
mScanner = new Scanner();
}
mWorkHandler.sendEmptyMessage(WorkHandler.MSG_RESUME);
if (mWifiManager.isWifiEnabled()) {
mScanner.resume(); // 发送扫描消息
}
}
//
class Scanner extends Handler {
static final int MSG_SCAN = 0;
private int mRetry = 0;
void resume() {
if (!hasMessages(MSG_SCAN)) {
sendEmptyMessage(MSG_SCAN);
}
}
void forceScan() {
removeMessages(MSG_SCAN);
sendEmptyMessage(MSG_SCAN);
}
void pause() {
mRetry = 0;
removeMessages(MSG_SCAN);
}
@VisibleForTesting
boolean isScanning() {
return hasMessages(MSG_SCAN);
}
@Override
public void handleMessage(Message message) {
if (message.what != MSG_SCAN) return;
if (mWifiManager.startScan()) { // 启动扫描
mRetry = 0;
} else if (++mRetry >= 3) {
mRetry = 0;
if (mContext != null) {
Toast.makeText(mContext, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show();
}
return;
}
// 扫描界面扫描间隔。
sendEmptyMessageDelayed(MSG_SCAN, WIFI_RESCAN_INTERVAL_MS);
}
}
//private static final int WIFI_RESCAN_INTERVAL_MS = 10 * 1000;
2. 不在wifisetting界面,亮屏情况下。
frameworks\opt\net\wifi\service\java\com\android\server\wifi\WifiConnectivityManager.java
public void handleScreenStateChanged(boolean screenOn) {
localLog("handleScreenStateChanged: screenOn=" + screenOn);
mScreenOn = screenOn;
mOpenNetworkNotifier.handleScreenStateChanged(screenOn);
startConnectivityScan(SCAN_ON_SCHEDULE);
}
private void startConnectivityScan(boolean scanImmediately) {
localLog("startConnectivityScan: screenOn=" + mScreenOn
+ " wifiState=" + stateToString(mWifiState)
+ " scanImmediately=" + scanImmediately
+ " wifiEnabled=" + mWifiEnabled
+ " wifiConnectivityManagerEnabled="
+ mWifiConnectivityManagerEnabled);
if (!mWifiEnabled || !mWifiConnectivityManagerEnabled) {
return;
}
// Always stop outstanding connecivity scan if there is any
stopConnectivityScan();
// Don't start a connectivity scan while Wifi is in the transition
// between connected and disconnected states.
if (mWifiState != WIFI_STATE_CONNECTED && mWifiState != WIFI_STATE_DISCONNECTED) {
return;
}
if (mScreenOn) {
startPeriodicScan(scanImmediately);
} else {
if (mWifiState == WIFI_STATE_DISCONNECTED && !mPnoScanStarted) {
startDisconnectedPnoScan();
}
}
}
// Start a periodic scan when screen is on
private void startPeriodicScan(boolean scanImmediately) {
// 从新设置PNO扫描的时间间隔。
// 当扫描的网络,Rssi比较低时,将会进行从新扫描
mPnoScanListener.resetLowRssiNetworkRetryDelay();
// 漫游关闭时,不进行扫描
// No connectivity scan if auto roaming is disabled.
if (mWifiState == WIFI_STATE_CONNECTED && !mEnableAutoJoinWhenAssociated) {
return;
}
// Due to b/28020168, timer based single scan will be scheduled
// to provide periodic scan in an exponential backoff fashion.
if (scanImmediately) {
resetLastPeriodicSingleScanTimeStamp();
}
mPeriodicSingleScanInterval = PERIODIC_SCAN_INTERVAL_MS;
startPeriodicSingleScan();
}
@VisibleForTesting
public static final int PERIODIC_SCAN_INTERVAL_MS = 20 * 1000; // 20 seconds
// Start a single scan and set up the interval for next single scan.
private void startPeriodicSingleScan() {
long currentTimeStamp = mClock.getElapsedSinceBootMillis();
if (mLastPeriodicSingleScanTimeStamp != RESET_TIME_STAMP) {
long msSinceLastScan = currentTimeStamp - mLastPeriodicSingleScanTimeStamp;
if (msSinceLastScan < PERIODIC_SCAN_INTERVAL_MS) {
localLog("Last periodic single scan started " + msSinceLastScan
+ "ms ago, defer this new scan request.");
schedulePeriodicScanTimer(PERIODIC_SCAN_INTERVAL_MS - (int) msSinceLastScan);
return;
}
}
boolean isFullBandScan = true;
// 判断wifi使用率是不是比较高,比较高只进行一部分扫描
// 通过发送和接收速率判断
// If the WiFi traffic is heavy, only partial scan is initiated.
if (mWifiState == WIFI_STATE_CONNECTED
&& (mWifiInfo.txSuccessRate > mFullScanMaxTxRate
|| mWifiInfo.rxSuccessRate > mFullScanMaxRxRate)) {
localLog("No full band scan due to ongoing traffic");
isFullBandScan = false;
}
// 再次判断,如果发送接收速率超过最大值,将不进行扫描。
mLastPeriodicSingleScanTimeStamp = currentTimeStamp;
if (mWifiState == WIFI_STATE_CONNECTED
&& (mWifiInfo.txSuccessRate
> MAX_TX_PACKET_FOR_PARTIAL_SCANS
|| mWifiInfo.rxSuccessRate
> MAX_RX_PACKET_FOR_PARTIAL_SCANS)) {
Log.e(TAG,"Ignore scan due to heavy traffic");
} else {
startSingleScan(isFullBandScan, WIFI_WORK_SOURCE);
}
schedulePeriodicScanTimer(mPeriodicSingleScanInterval);
// 设置下一次扫描时间间隔,是当前的2倍。
// 最大扫描时间间隔160s
// public static final int MAX_PERIODIC_SCAN_INTERVAL_MS = 160 * 1000; // 160 seconds
// Set up the next scan interval in an exponential backoff fashion.
mPeriodicSingleScanInterval *= 2;
if (mPeriodicSingleScanInterval > MAX_PERIODIC_SCAN_INTERVAL_MS) {
mPeriodicSingleScanInterval = MAX_PERIODIC_SCAN_INTERVAL_MS;
}
}
3. pno扫描
private void startDisconnectedPnoScan() {
// TODO(b/29503772): Need to change this interface.
// Initialize PNO settings
PnoSettings pnoSettings = new PnoSettings();
List<PnoSettings.PnoNetwork> pnoNetworkList = mConfigManager.retrievePnoNetworkList();
int listSize = pnoNetworkList.size();
if (listSize == 0) {
// No saved network
localLog("No saved network for starting disconnected PNO.");
return;
}
pnoSettings.networkList = new PnoSettings.PnoNetwork[listSize];
pnoSettings.networkList = pnoNetworkList.toArray(pnoSettings.networkList);
pnoSettings.min5GHzRssi = mMin5GHzRssi;
pnoSettings.min24GHzRssi = mMin24GHzRssi;
pnoSettings.initialScoreMax = mInitialScoreMax;
pnoSettings.currentConnectionBonus = mCurrentConnectionBonus;
pnoSettings.sameNetworkBonus = mSameNetworkBonus;
pnoSettings.secureBonus = mSecureBonus;
pnoSettings.band5GHzBonus = mBand5GHzBonus;
// Initialize scan settings
ScanSettings scanSettings = new ScanSettings();
scanSettings.band = getScanBand();
scanSettings.reportEvents = WifiScanner.REPORT_EVENT_NO_BATCH;
scanSettings.numBssidsPerScan = 0;
scanSettings.periodInMs = DISCONNECTED_PNO_SCAN_INTERVAL_MS; // 20s
mPnoScanListener.clearScanDetails();
mScanner.startDisconnectedPnoScan(scanSettings, pnoSettings, mPnoScanListener);
mPnoScanStarted = true;
}
待续。。。
Android 8 wifi 扫描时间间隔的更多相关文章
- android 8 wifi wifi 扫描过程
查看一下android wifi扫描的过程. packages\apps\Settings\src\com\android\settings\wifi\WifiSettings.java public ...
- 【Android】wifi开发
WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为“热点 ...
- android.net.wifi的简单使用方法
获取Wifi的控制类WifiManager. WifiManager wm=(WifiManager)getSystemService(Context.WIFI_SERVICE); 接下来可以对w ...
- 【移动开发】Android中WIFI开发总结(二)
搞了好几天终于有点眉目了,这里接着总结一下Android中WiFi的使用,在前面(http://smallwoniu.blog.51cto.com/3911954/1334951)我们已经简单了解了W ...
- 【移动开发】Android中WIFI开发总结(一)
WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为“热 ...
- Android 连接Wifi和创建Wifi热点 demo
android的热点功能不可见,用了反射的技术搞定之外. Eclipse设置语言为utf-8才能查看中文注释 上代码: MainActivity.java package com.widget.hot ...
- android之wifi开发
WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为“热点 ...
- android 获取wifi列表,如果你忽略了这个细节,可能你的软件会崩溃
一:业务描述 最近公司有一个小需求,用户点击wifi扫描按钮(注意:是用户主动点击wifi扫描按钮),app去扫描附近的wifi,显示在listView中,仅此而已,app都不用去连接某个wifi,看 ...
- Android之Wifi学习(1)
在Android中对Wifi操作,android本身提供了一些实用的包.在android.net.wifi包以下.简介一下: 大致能够分为四个基本的类ScanResult,wifiConfigurat ...
随机推荐
- sql server2000导出表结构说明
SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.n ...
- 第二章 使用接口(Using Interfaces)-书籍翻译
PDF预览 下载地址 http://files.cnblogs.com/DKSoft/CodingInDelphi.pdf 1.1. 解耦(Decoupling) All through this b ...
- 线上服务CPU100%问题快速定位实战
功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 58到家架构部,运维部,58速运技术部联合进行了一次线上 ...
- Docker 镜像操作
列出镜像列表 我们可以使用 docker images 来列出本地主机上的镜像. runoob@runoob:~$ docker images REPOSITORY TAG IMAGE ID CREA ...
- 设计模式之模板方法模式&&迪米特法则(代码Objective-C展示)
模板方法模式 模板方法模式:定义一个操作中的算法骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变一个算法的结构即可重定义该算法的某些特定步骤. 比如说,小时候数学老师的随堂检测,都是在黑板 ...
- pual_bot 天气插件编写
最近在玩pual_bot,感觉很不错,最近天气插件失效了,就结合百度api重新写了一个,也提交了. https://github.com/coldnight/pual_bot #!/usr/bin/e ...
- C#学习笔记(30)——系统自带委托Func和Action
说明(2017-11-23 10:46:33): 1. Func有返回值,Action无返回值,以后就不用定义delegate委托了. 2. 不过还是不知道什么时候该用委托,蒋坤在讲完事件后,留了个作 ...
- ASP.NET学习笔记(4)——上传图片
说明(2017-10-8 23:03:43): 1. 后面的内容都是一些杂七杂八的,零零碎碎的,之前都直接略过了,不过其实还是挺重要的,这次重新学习要认认真真敲一遍. 2. 明天中午9号要回北京了,今 ...
- 什么是POP3、SMTP和IMAP?
POP3 POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议.它是因特网电子邮件的第 ...
- One-hot 编码/TF-IDF 值来提取特征,LAD/梯度下降法(Gradient Descent),Sigmoid
1. 多值无序类数据的特征提取: 多值无序类问题(One-hot 编码)把“耐克”编码为[0,1,0],其中“1”代表了“耐克”的中 间位置,而且是唯一标识.同理我们可以把“中国”标识为[1,0],把 ...