wifi rssi在android中需要多长时间?

时间:2014-12-31 18:01:52

标签: android rssi

我有代码显示连接网络的RSSI值。后台进程不断调用getRssi,循环时间为几毫秒。

在循环中,我通过处理程序将值发布到UI线程,在那里我显示当前时间以及循环速率。

但是,UI显示每隔几秒更新一次,而循环速率则为几毫秒。为什么会出现这种差异?

package uk.co.moonsit.apps.wifi;

import uk.co.moonsit.apps.sensors.R;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.view.View;
import android.widget.TextView;

public class WifiRssiActivity extends Activity {

private WifiManager wifi;
private Handler handler = new Handler();
// private String toastText;
private TextView tvStrength;
private TextView tvSpeed;
private TextView tvSSID;
private TextView tvTime;
private View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wifi_rssi);

    tvStrength = (TextView) findViewById(R.id.textViewWifiRssi);
    tvSpeed = (TextView) findViewById(R.id.textViewSpeed);
    tvSSID = (TextView) findViewById(R.id.textViewSSID);
    tvTime = (TextView) findViewById(R.id.textViewTime);

    String service = Context.WIFI_SERVICE;
    wifi = (WifiManager) getSystemService(service);

    Thread thread = new Thread(null, doBackgroundThreadProcessing,
            "Background");
    thread.start();
    view = this.getWindow().getDecorView();
    view.setKeepScreenOn(true);
}

private Runnable doBackgroundThreadProcessing = new Runnable() {

    public void run() {
        backgroundThreadProcessing();
    }
};

private boolean isRunning = true;

// Method which does some processing in the background.
private void backgroundThreadProcessing() {

    long mark = System.currentTimeMillis();
    while (isRunning) {
        int strength = 0;
        int speed = 0;
        String units = null;
        String ssid = null;
        long ms = 0;
        WifiInfo info = wifi.getConnectionInfo();
        if (info.getBSSID() != null) {
            strength = WifiManager
                    .calculateSignalLevel(info.getRssi(), 100);
            speed = info.getLinkSpeed();
            units = WifiInfo.LINK_SPEED_UNITS;
            ssid = info.getSSID();
        }
        long now = System.currentTimeMillis();
        ms = now - mark;
        mark = now;
        GUIRunnable doUpdateGUI = new GUIRunnable(strength, speed, units,
                ssid, ms);
        handler.post(doUpdateGUI);
    }
}

public class GUIRunnable implements Runnable {

    private int strength;
    private int speed;
    private String units;
    private String ssid = null;
    private long ms;

    public GUIRunnable(int st, int sp, String u, String ss, long m) {
        strength = st;
        speed = sp;
        units = u;
        ssid = ss;
        ms = m;
    }

    public void run() {
        updateGUI(strength, speed, units, ssid, ms);
    }
}

private void updateGUI(int strength, int speed, String units, String ssid,
        long ms) {
    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();

    String millis = String.valueOf(System.currentTimeMillis());
    String time = today.format("%k:%M:%S.")
            + millis.substring(millis.length() - 3);

    tvStrength.setText("" + strength);
    tvTime.setText(time + " - " + ms);

    if (speed > 0)
        tvSpeed.setText("" + speed + units);
    if (ssid == null || ssid.length() == 0)
        tvSSID.setText("No wifi connection");
    else
        tvSSID.setText(ssid);
    // Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_LONG)
    // .show();
}

@Override
protected void onResume() {
    super.onResume();
    isRunning = true;

}

@Override
protected void onPause() {
    super.onPause();
    // ssid = null;
    isRunning = false;
}

}

2 个答案:

答案 0 :(得分:1)

检查了谷歌资源,在我看来Android每3秒只会调查一次WIFI RSSI(最有可能节省CPU和电池资源)。

以下是WifiStateMachine.java的片段: -

/**
 * Interval in milliseconds between polling for RSSI
 * and linkspeed information
 */
private static final int POLL_RSSI_INTERVAL_MSECS = 3000;

答案 1 :(得分:0)

android api没有指定更新RSSI或更新RSSI的速率,但总的来说,我怀疑它是否比每秒一次节电更快。

我发现了一篇关于如何使用广播接收器获取RSSI更新(而不是连续轮询)的文章:http://android-er.blogspot.com/2011/01/check-rssi-by-monitoring-of.html

API确实建议getRSSI和其他方法只是从上次扫描返回数据,因此更新速率可能是wifi扫描间隔。这通常由build.prop文件控制(参见此示例文件的第103行:http://androidforums.com/threads/stock-build-prop-download.561012/),并且可能因设备而异,这是在RSSI(或其他wifi参数)上更新用户的最有效方式)是使用广播接收器并在可用时更新。

注意,根据这个问题的答案,一些wifi设备驱动程序不支持由build.prop文件配置,所以即使有一个root权限来修改文件,它也不会保证您可以更改扫描间隔:https://android.stackexchange.com/questions/37621/where-can-i-find-settings-for-wifi-internal-scan-period