Measuring DHCP time in Android

时间:2016-03-02 10:52:37

标签: java android

For my thesis I need to measure how long in each stage it takes to connect to a WiFi AP.

I can easily measure everything using SUPPLICANT_STATE_CHANGED_ACTION, but I'm having trouble figuring out how I can measure the time it takes to get the DHCP information.

I just really need to know with a certain precision when the android device gets a new IP. I've tried using CONNECTIVITY_ACTION and inside the BroadcastReceiver checking the IP address, but it's off by at least 1 second, so not too precise.

One more thing if it's relevant: The AP on which I will do the tests won't have internet connection.

Any ideas how this can be done?

Thanks in advance

1 个答案:

答案 0 :(得分:1)

我设法通过以下方式解决了这个问题:

使用WifiManager.NETWORK_STATE_CHANGED_ACTION

创建了一个过滤器

当有新事件时,我会按以下方式检查:

if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
  elapseTime = System.currentTimeMillis() - startTime;
  WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  int ipAddress = wifiInfo.getIpAddress();
  if (ipAddress != 0 && !wifiConnected) {
    wifiConnected = true;
    wifiTimeLog = wifiTimeLog + elapseTime + "," + "DHCP Done" + "\n";
    state.setText(wifiTimeLog);
 }

wifiConnected是一个在测试之前设置为false的布尔值。获得IP后还会发生一些其他触发因素。