You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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!

Analysis & Fixes: Beacon Scanning Stops After ~90 Minutes on Marshmallow/Nougat

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:

    1. 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 the WRITE_SETTINGS permission, which needs explicit user approval on Android 6.0+.
    2. If WiFi isn't essential for your app, ask users to disable it during scanning, or switch to SCAN_MODE_LOW_LATENCY instead of SCAN_MODE_LOW_POWER when WiFi is active. It uses more battery, but reduces the chance of system throttling.
  • 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:

    1. Replace basic Handler.postDelayed with AlarmManager or JobScheduler for scan restarts. Regular handlers get paused in the background, but these system-level schedulers bypass most restrictions. Example with AlarmManager:
      private 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);
          }
      }
      
      Then in ScanRestartReceiver, restart your BLE scan and re-call scheduleScanRestart to keep the loop going.
    2. Ensure proper location permissions: BLE scanning requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION on Marshmallow+. Even on Nougat, background scanning can be restricted if the system doesn't recognize your app as needing location access.
    3. 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.

Extra Debug Tips

  • Check system logs with adb logcat, searching for keywords like BluetoothLeScanner, LocationManager, or PowerManager. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:55:00