Android:连续扫描所有AP(接入点)

标签 android android-wifi java-threads indoor-positioning-system

我是 Android 应用程序开发的新手。我正在开发一个 Android 应用程序来 ping 访问点以访问其 RSSI 值以估计用户的位置。

虽然我目前有这个“工作”,但我相信我的实现中存在一个错误,它会创建对“onReceive()”的过多调用。在应用程序的整个生命周期中,对此函数的调用量呈线性增加。

我即将发布的代码的目标是简单地扫描 WiFi 接入点,获取它们的 RSSI 值,然后连续循环。电池生命周期不是问题,性能才是更重要的指标。

主要 Activity .java:

Handler handler = new Handler();
final Runnable locationUpdate = new Runnable() {
    @Override
    public void run() {
        getLocation();

      //customView.setLocation(getX_pixel(curLocation.getX()), getY_pixel(curLocation.getY()));
       //customView.invalidate();

        handler.postDelayed(locationUpdate, 1000);
    }
};

private void getLocation() {
    Context context = getApplicationContext();
    WifiScanReceiver wifiReceiver = new WifiScanReceiver();
    registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiManager.startScan();
    Log.d("START SCAN CALLED", "");
}

然后在同一个文件中,在 onCreate() 方法中:

handler.post(locationUpdate);

然后在同一个文件中,在 onCreate() 方法之外:

class WifiScanReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context c, Intent intent) {
        WifiManager wifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);

        List<ScanResult> scan = wifiManager.getScanResults();
        // Application specific code:
        sortScan(scan);
        count+= 1;
        System.out.println("Count: " + count);

        }
    }
};

我确认了斜坡/线程问题,因为我在程序到达“sortScan(scan)”时递增并输出到控制台,您可以清楚地看到结果是线性斜坡。

正如我之前所说,我的 Intent 是在第一次扫描完成后立即重新扫描,并在应用程序的整个生命周期内循环扫描。

非常感谢任何帮助,谢谢。

最佳答案

您正在重复注册您的接收器,这是不必要的。只需在 onCreate() 中注册 WifiScanReceiver 一次即可。然后在getLocation()函数中调用start scan。

 WifiManager wifiManager;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    Context context = getApplicationContext();
    WifiScanReceiver wifiReceiver = new WifiScanReceiver();
    registerReceiver(wifiReceiver, new 
                 IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    wifiManager =
                (WifiManager)context.getSystemService(Context.WIFI_SERVICE);



Handler handler = new Handler();
final Runnable locationUpdate = new Runnable() {
    @Override
    public void run() {
        getLocation();
        //This line will continuously call this Runnable with 1000 milliseconds gap
        handler.postDelayed(locationUpdate, 1000);
    }
};

private void getLocation() {
    wifiManager.startScan();
    Log.d("START SCAN CALLED", "");
}

}


 class WifiScanReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent intent) {

   if(intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)){

        //New scan results are available. Arrange a callback to the activity here.
    }
  }
}

您不应该在 onReceive() 中进行繁重的处理。安排对 Activity 的回调来执行此操作。

关于Android:连续扫描所有AP(接入点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44511880/

相关文章:

android - Itelephony 服务结束调用 () 不适用于三星 GT-S7562 android 版本 4.0.4

android - 图钉内带有用户图像的自定义标记

android - "Wi-Fi Direct"Android 库?

linux - wpa_supplicant 守护程序的计划扫描

android - 比较方法违反了它的总契约!数组排序()

java - 在 Playstore 上为您的 Android 应用程序提供升级时,是否可以对本地保存的数据运行代码/任务?

java - 如何使用 Appium 禁用 Android 模拟器中的互联网连接?

java - 对执行 Swing 应用程序的主类中的 SwingUtilities.invokeLater() 方法的调用到底是什么?

java - 如何使用 ScheduledExecutorService 强制创建单线程并避免多线程

Java:由 HTTP 连接创建的等待连接线程存活时间很长