Android 10 在后台扫描时的限制?
Restrictions when scanning in background on Android 10?
关于 Android 上的蓝牙扫描似乎有很多 "hidden features"。
对于初学者来说,有一个“30 秒限制”(Android 7.0 ble scan no result)。然后你必须在后台模式下设置一个 ScanFilter 否则你得不到结果(找不到这个的参考)。
最近我发现在 Android 10(我观察到这个Google Pixel 3 和 Google Pixel 4 上的行为,它在旧的 Androids 上工作正常):
/**
* Perform Bluetooth LE scan in balanced power mode. Scan results are returned at a rate that
* provides a good trade-off between scan frequency and power consumption.
*/
public static final int SCAN_MODE_BALANCED = 1;
还有一种扫描方式(我还没试过):
/**
* Perform Bluetooth LE scan in low power mode. This is the default scan mode as it consumes the
* least power. This mode is enforced if the scanning application is not in foreground.
*/
public static final int SCAN_MODE_LOW_POWER = 0;
问题: 我是否应该将此评论 "is enforced" 解释为使用 SCAN_MODE_LOW_POWER
以外的其他方式进行扫描将不会给我任何扫描结果?有人可以证实这一点吗?
我会自己进一步调查,但这需要时间...
注意:我有一个前台服务,我可以在 ADB 日志中看到定期启用/禁用扫描器。但是我没有得到任何扫描结果...
更新: 我现在确保在后台模式下使用 SCAN_MODE_LOW_POWER
但我仍然没有得到任何结果。我不知道发生了什么。
更新 2: 我尝试了 运行 旧版本的应用程序(不是为 Android 10 编译的)并且工作正常
更新 3: 我禁用了应用程序的电池优化以防万一。这没有帮助:
- 转到“设置”>“应用”>“您的应用”>“高级”>“电池”>“电池优化”
- 将视图更改为所有应用程序
- 搜索您的应用程序
- 选择未优化
Android 10 需要新权限才能获得 BLE 扫描 return 结果。不幸的是,扫描将不会 return 结果而不是记录警告。新权限是 ACCESS_FINE_LOCATION instead of ACCESS_COARSE_LOCATION required for sdk versions 23 through 28 and if you need to perform BLE scans while the app is in the background (even if you have a foreground service) you will need ACCESS_BACKGROUND_LOCATION. You must list these permissions in the manifest and prompt the user to grant them at runtime. There's a similar question here with a little more info in the answers: Android 10 not working with BLE Bluetooth scanning
关于 Android 上的蓝牙扫描似乎有很多 "hidden features"。 对于初学者来说,有一个“30 秒限制”(Android 7.0 ble scan no result)。然后你必须在后台模式下设置一个 ScanFilter 否则你得不到结果(找不到这个的参考)。
最近我发现在 Android 10(我观察到这个Google Pixel 3 和 Google Pixel 4 上的行为,它在旧的 Androids 上工作正常):
/**
* Perform Bluetooth LE scan in balanced power mode. Scan results are returned at a rate that
* provides a good trade-off between scan frequency and power consumption.
*/
public static final int SCAN_MODE_BALANCED = 1;
还有一种扫描方式(我还没试过):
/**
* Perform Bluetooth LE scan in low power mode. This is the default scan mode as it consumes the
* least power. This mode is enforced if the scanning application is not in foreground.
*/
public static final int SCAN_MODE_LOW_POWER = 0;
问题: 我是否应该将此评论 "is enforced" 解释为使用 SCAN_MODE_LOW_POWER
以外的其他方式进行扫描将不会给我任何扫描结果?有人可以证实这一点吗?
我会自己进一步调查,但这需要时间...
注意:我有一个前台服务,我可以在 ADB 日志中看到定期启用/禁用扫描器。但是我没有得到任何扫描结果...
更新: 我现在确保在后台模式下使用 SCAN_MODE_LOW_POWER
但我仍然没有得到任何结果。我不知道发生了什么。
更新 2: 我尝试了 运行 旧版本的应用程序(不是为 Android 10 编译的)并且工作正常
更新 3: 我禁用了应用程序的电池优化以防万一。这没有帮助:
- 转到“设置”>“应用”>“您的应用”>“高级”>“电池”>“电池优化”
- 将视图更改为所有应用程序
- 搜索您的应用程序
- 选择未优化
Android 10 需要新权限才能获得 BLE 扫描 return 结果。不幸的是,扫描将不会 return 结果而不是记录警告。新权限是 ACCESS_FINE_LOCATION instead of ACCESS_COARSE_LOCATION required for sdk versions 23 through 28 and if you need to perform BLE scans while the app is in the background (even if you have a foreground service) you will need ACCESS_BACKGROUND_LOCATION. You must list these permissions in the manifest and prompt the user to grant them at runtime. There's a similar question here with a little more info in the answers: Android 10 not working with BLE Bluetooth scanning