AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.84k stars 836 forks source link

How to set scanning period when app is in foreground? #1096

Closed fhlkm closed 2 years ago

fhlkm commented 2 years ago

I run the test code and when the app is in foreground, it will observe beacons every 1 second, the log is like below:

Ranged: 1 beacons Ranged: 0 beacons Ranged: 1 beacons Ranged: 0 beacons Ranged: 1 beacons Ranged: 0 beacons .............

I think the reason because scanPeroid is too short, Is there a way I can set the scan Period(for example 2 seconds) when app is in foreground(not foreground service)?

I tried this code, and it scan every 5 second and can observed beacons every time. However, it only works when app is in background.

// setupForegroundService() // beaconManager.setEnableScheduledScanJobs(false); // beaconManager.setBackgroundBetweenScanPeriod(2000); // beaconManager.setBackgroundScanPeriod(3000);

fhlkm commented 2 years ago

My mistake, I added this code, the problem is solved: setupForegroundService() beaconManager.foregroundScanPeriod=2000L beaconManager.foregroundBetweenScanPeriod=1000L