Android Nougat系统蓝牙扫描一段时间后停止的问题求助
Hey there, let's dig into this Beacon scanning issue you're facing on Marshmallow and Nougat—great job already narrowing down those observations, that helps a ton!
First, let's recap the key context: you've set up 5-minute scan restarts to work around Nougat's 30-minute scan limit, but the scan still dies around the 90-minute mark. Your observations give us clear clues to target.
Breakdown of Your Observations & Targeted Fixes
Observation 1: Beacon transmission is confirmed normal across all devices
This rules out hardware/Beacon-side issues, so we can focus entirely on Android system restrictions and your scan restart logic.Observation 2: Scanning works continuously when WiFi is off
This is a critical clue! On Marshmallow and Nougat, WiFi's power-saving scan optimization often conflicts with BLE scanning. When WiFi is connected, the system may merge or throttle BLE scans over time to save power—even if you manually restart scans, it might silently block them after prolonged background runtime.
Fix suggestions:- Temporarily disable WiFi scan optimization (if your app has the right permissions). You can adjust
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, but note this requires theWRITE_SETTINGSpermission, which needs explicit user approval on Android 6.0+. - If WiFi isn't essential for your app, ask users to disable it during scanning, or switch to
SCAN_MODE_LOW_LATENCYinstead ofSCAN_MODE_LOW_POWERwhen WiFi is active. It uses more battery, but reduces the chance of system throttling.
- Temporarily disable WiFi scan optimization (if your app has the right permissions). You can adjust
Observation 3: Once scanning stops...
Even with 5-minute restarts, your logic might be getting blocked by Marshmallow/Nougat's background task restrictions. Here's how to fix that:- Replace basic
Handler.postDelayedwithAlarmManagerorJobSchedulerfor scan restarts. Regular handlers get paused in the background, but these system-level schedulers bypass most restrictions. Example withAlarmManager:
Then inprivate void scheduleScanRestart() { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, ScanRestartReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE ); long triggerTime = System.currentTimeMillis() + 5 * 60 * 1000; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); } else { alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); } }ScanRestartReceiver, restart your BLE scan and re-callscheduleScanRestartto keep the loop going. - Ensure proper location permissions: BLE scanning requires
ACCESS_COARSE_LOCATIONorACCESS_FINE_LOCATIONon Marshmallow+. Even on Nougat, background scanning can be restricted if the system doesn't recognize your app as needing location access. - Run scanning in a foreground service: Foreground services are marked as "critical" by the system and won't be throttled or killed easily. You'll need to show a persistent notification indicating the scan is active, but this guarantees your scan logic keeps running.
- Replace basic
Extra Debug Tips
- Check system logs with
adb logcat, searching for keywords likeBluetoothLeScanner,LocationManager, orPowerManager. Look for messages about scans being paused or restricted—this will pinpoint exactly which system mechanism is interfering. - Test with developer options enabled: Turn on "Don't keep activities" and set "Background process limit" to "No background processes" to simulate strict background conditions, verifying your restart logic and foreground service hold up.
内容的提问来源于stack exchange,提问作者Parth Parekh

