调整Wifi满格信号的阀值
作者:互联网
要想wifi在信号比较差的时候也显示满格信号,可参考如下方法
/** Anything worse than or equal to this will show 0 bars. */
@UnsupportedAppUsage
private static final int MIN_RSSI = -100;
/** Anything better than or equal to this will show the max bars. */
@UnsupportedAppUsage
private static final int MAX_RSSI = -55;
/**
* Number of RSSI levels used in the framework to initiate {@link #RSSI_CHANGED_ACTION}
* broadcast, where each level corresponds to a range of RSSI values.
* The {@link #RSSI_CHANGED_ACTION} broadcast will only fire if the RSSI
* change is significant enough to change the RSSI signal level.
* @hide
*/
@UnsupportedAppUsage
public static final int RSSI_LEVELS = 5;
/**
* Calculates the level of the signal. This should be used any time a signal
* is being shown.
*
* @param rssi The power of the signal measured in RSSI.
* @param numLevels The number of levels to consider in the calculated level.
* @return A level of the signal, given in the range of 0 to numLevels-1 (both inclusive).
* @deprecated Callers should use {@link #calculateSignalLevel(int)} instead to get the
* signal level using the system default RSSI thresholds, or otherwise compute the RSSI level
* themselves using their own formula.
*/
@Deprecated
public static int calculateSignalLevel(int rssi, int numLevels) {
if (rssi <= MIN_RSSI) {
return 0;
} else if (rssi >= MAX_RSSI) {
return numLevels - 1;
} else {
float inputRange = (MAX_RSSI - MIN_RSSI);
float outputRange = (numLevels - 1);
return (int)((float)(rssi - MIN_RSSI) * outputRange / inputRange);
}
}
对于Android11,接口有变化
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.wifi.util;
import android.annotation.NonNull;
import android.content.Context;
import com.android.wifi.resources.R;
/** Utilities for computations involving RSSI. */
public class RssiUtil {
private RssiUtil() {}
/** Calculate RSSI level from RSSI using overlaid RSSI level thresholds. */
public static int calculateSignalLevel(Context context, int rssi) {
int[] thresholds = getRssiLevelThresholds(context);
for (int level = 0; level < thresholds.length; level++) {
if (rssi < thresholds[level]) {
return level;
}
}
return thresholds.length;
}
@NonNull
private static int[] getRssiLevelThresholds(Context context) {
// getIntArray() will never return null, it will throw instead
return context.getResources().getIntArray(R.array.config_wifiRssiLevelThresholds);
}
}
http://aospxref.com/android-11.0.0_r21/xref/frameworks/opt/net/wifi/service/res/values/config.xml
<!--
Controls the mapping between RSSI and RSSI levels.
RSSI RSSI Level
(-infinity, thresholds[0]) 0
[threshold[0], threshold[1]) 1
[threshold[1], threshold[2]) 2
... ...
[threshold[len-2], threshold[len-1]) len-1
[threshold[len-1], +infinity) len
where:
[a, b) is the range of integers `n` such that a <= n < b
`threshold[i]` represents the i'th element of the config_wifiRssiLevelThresholds array
and `len` is the length of the config_wifiRssiLevelThresholds array.
-->
<integer-array translatable="false" name="config_wifiRssiLevelThresholds">
<!-- RSSI RSSI Level -->
<item>-88</item> <!-- (-infinity, -88) 0 -->
<item>-77</item> <!-- [-88, -77) 1 -->
<item>-66</item> <!-- [-77, -66) 2 -->
<item>-55</item> <!-- [-66, -55) 3 -->
<!-- [-55, +infinity) 4 -->
</integer-array>
如何确认修改有效,打开开发者选项,勾选开启wlan详细日记记录,然后到wifi的搜索界面上看下已连接wifi信号的rssi值,然后移动手机,看信号格数有没有符合要求。
标签:wifi,return,level,int,Wifi,RSSI,android,满格,阀值 来源: https://blog.csdn.net/mike8825/article/details/118856424