Android TelephonyManager getAllCellInfo() 刷新率 RSSI

标签 android gsm telephonymanager rssi

我目前正在为我的大学研究项目开发 Android 应用程序。此应用程序应该能够读取附近 GSM 基站的 RSSI 级别。我编写了以下函数,它成功读取了附近小区的 RSSI 电平。我每 200 毫秒调用一次此函数,但 RSSI 值几乎不会随时间变化。我怀疑基带处理器仅在每个 x s 更新这些值,但我找不到有关刷新率的任何信息。有谁知道 getAllCellInfo() 中的信息刷新速度有多快?我想知道这一点的原因是因为时机对我的设置非常重要。在实验室中,我们以特定频率(大于 1 Hz)启用和禁用干扰器。启用干扰器后,RSSI 值将下降。所以我想知道如果以 10 赫兹的频率启用和禁用干扰器,RSSI 可以多快刷新以检测此信号下降。

public HashMap<Integer,Integer> RSSI_values() {

    HashMap<Integer,Integer> result = new HashMap<Integer,Integer>();

    TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.getNetworkType();
    List<CellInfo> CellInfo_list = telephonyManager.getAllCellInfo();
    if(telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE) {
        for (int i = 0; i < CellInfo_list.size(); i++) {
            CellInfoGsm cellinfogsm = (CellInfoGsm) CellInfo_list.get(i);

            int cid = cellinfogsm.getCellIdentity().getCid();

            CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
            int rssi = cellSignalStrengthGsm.getDbm();

            result.put(cid,rssi);
        }
    } 
    return result;
}

如果此刷新率是设备特定的,我使用的是 nexus 5 android 6.0.1

最佳答案

我认为您可以在此实现上使用更好的方法。

您可以注册一个监听器,而不是使用定义的时间间隔来查询单元信息,每次单元信息更改时都会调用该监听器。

这样,您无需担心理想值,也不会浪费资源检查可能尚未更改的信息。

您可以使用 PhoneStateListener。您创建它并注册它以接收单元格信息更改。当不再需要它时(后台 Activity 或已销毁),您必须取消注册它。

下面是一个例子。我没有测试。但它可能会帮助你理解这个想法。

public class MainActivity extends Activity {

    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
        @Override
        public void onCellInfoChanged(List<CellInfo> cellInfoList) {
            // This callback method will be called automatically by Android OS
            // Every time a cell info changed (if you are registered)
            // Here, you will receive a cellInfoList....
            // Same list that you will receive in RSSI_values() method that you created
            // You can maybe move your whole code to here....
        }
    };

    @Override
    public void onStart() {
        super.onStart();

        // Code below register your mPhoneStateListener will start to be called everytime cell info changed.
        // If you update any UI element, be sure that they were created already (since they are created during "onCreate".. and not at onStart)
        // I added LISTEN_CELL_LOCATION.. But I think that PhoneStateListener.LISTEN_CELL_INFO is enough
        TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_CELL_INFO);
    }

    @Override
    public void onStop() {
        // Code below unregister your listener... You will not receive any cell info change notification anymore
        TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
        super.onStop();
    }
}

希望能帮到你。

关于Android TelephonyManager getAllCellInfo() 刷新率 RSSI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37441832/

相关文章:

python - 如何在 Python 中创建 GSM-7 编码?

c# - GSM 调制解调器使用什么字符集

java - 邻区信息不准确

在没有连接蜂窝网络的情况下,Android 仍会检测蜂窝信号强度

android - 使用 telephonyserivce.endcall() 在 android 中结束通话

Android - 状态栏有时会隐藏部分 View

Android SSH 示例代码

java - Android Studio 预期为 'class' 或 'interface'

android - 如何在多个功能模块中访问同一个用例

java - 如何将我的通话设置为对其他人保密?