2016-05-25 88 views
2

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

回答

2

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

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

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

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

下面是一个例子。我没有测试。但它可以帮助你明白这个想法。

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(); 
    } 
} 

希望我可以帮助你。

+0

嗨@GuilhermeP感谢您的回复。 不幸的是,时机对我的设置非常重要。在实验室中,我们启用和禁用特定频率(大于1 Hz)的干扰信号。干扰器启用时,RSSI值将下降。所以我想知道如果在例如10赫兹启用和禁用干扰器时RSSI可以刷新多快来检测这个信号下降。 – mvanderreek

+1

@mvanderreek最初,我认为RSSI没有更新得那么快。通常情况下,CP(调制解调器)不会通知AP侧(Android)任何RSSI波动。这可能会严重影响电流消耗并降低电池寿命。上面的实施在RSSI值更新后立即通知您。新通知之前的任何rssi检查都应返回相同的状态。也许,你应该搜索如何通过一些RIL请求强制RSSI更新到CP端。我希望你找到更好的解决方案。 – W0rmH0le