简体   繁体   English

Android 上的 WiFi 扫描

[英]WiFi scan on Android

I have an app that is scanning Wi-Fi networks.我有一个正在扫描 Wi-Fi 网络的应用程序。 When one Wi-Fi scan is finished I'm starting new one.完成一次 Wi-Fi 扫描后,我将开始新的扫描。 I'm testing this on Pixel3 and SamsungS10 phones.我正在 Pixel3 和 SamsungS10 手机上对此进行测试。 On both phones I disabled Wi-Fi scan throttling from developer options.在这两款手机上,我都禁用了开发人员选项中的 Wi-Fi 扫描限制。 I do scans in the office where is a lot of different Wi-Fi networks.我在办公室进行扫描,那里有很多不同的 Wi-Fi 网络。 The problem that I'm observing, is that on Pixel3 Wi-Fi scans are working just fine for some time, then they start to returning scan errors for like a dozen consecutive scans and after that scans are working just fine again.我观察到的问题是,Pixel3 Wi-Fi 扫描在一段时间内工作正常,然后它们开始返回扫描错误,连续扫描十多次,之后扫描再次正常工作。 This is happening periodically.这是定期发生的。 The issue does not occur on SamsungS10 phones at all.该问题在 SamsungS10 手机上根本不会发生。 Here is how I'm doing the scan:这是我进行扫描的方式:

wifiManager.startScan();

BroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context c, Intent intent) {
    boolean success = intent.getBooleanExtra(
                       WifiManager.EXTRA_RESULTS_UPDATED, false);
    if (success) {
      scanSuccess();
    } else {
      scanFailure();
    }

    wifiManager.startScan();
  }
}; 

Any idea why Wi-Fi scan are working much worse on Pixel3 phones?知道为什么 Pixel3 手机上的 Wi-Fi 扫描效果更差吗? Is there any way to get exact Wi-Fi scan error code / error message instead of simple boolean from boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);有没有办法从boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);获取精确的 Wi-Fi 扫描错误代码/错误消息而不是简单的布尔boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false); ? ?

This Page ( https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-process ) contains this peace of code:此页面 ( https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-process ) 包含以下代码:

private void scanFailure() {
  // handle failure: new scan did NOT succeed
  // consider using old scan results: these are the OLD results!
  List<ScanResult> results = wifiManager.getScanResults();
  //... potentially use older scan results ...
}

I believe that Google's OS developers, in order to get a better battery life, simply prevent an app from doing a "wifi scan" all the time.我相信谷歌的操作系统开发人员,为了获得更好的电池寿命,简单地阻止应用程序一直进行“wifi扫描”。 And as the comments in the code describes, in case of an error you should simply use the old results.正如代码中的注释所描述的,如果出现错误,您应该简单地使用旧结果。 BierDav比尔戴夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM