当Android 8.1.0上的屏幕关闭时,BLE扫描不起作用 [英] BLE scan is not working when screen is off on Android 8.1.0

查看:842
本文介绍了当Android 8.1.0上的屏幕关闭时,BLE扫描不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的android 8.1.0更新的像素。

I am using pixel with latest android 8.1.0 update.

我面临与BLE广告扫描相关的问题。每当我关闭屏幕(即按下电源按钮)时,我的扫描都会停止。
,它将在屏幕打开后立即重新启动。

I am facing issue related to BLE advertisement scanning. Whenever I turned off the screen(i.e power button press) my scanning will stop. it will restart immediately after turn on the screen.

我已经检查了BLE的最新代码。 google新推出了此功能(参考链接)。

I have checked latest code for BLE. google newly introduce this feature (Reference Link).

有什么方法可以跳过这一部分,我的意思是无论屏幕打开还是关闭,扫描都不应停止。

Is there any way to skip this part, I mean scan should not stop regardless of the screen on or off.

推荐答案

从Android 8.1开始,关闭屏幕后,未经过滤的蓝牙扫描将被阻止。尽管在次要版本的Android中进行如此巨大的更改令人惊讶,但这无疑是基于提交中的注释的预期更改:
在屏幕关闭时停止未经过滤的BLE扫描。

As of Android 8.1, unfiltered bluetooth scans are blocked when the screen is turned off. While it is surprising for such a dramatic change to be made in a minor release of Android, this is certainly an intended change based on the comments in the commit: Stop unfiltered BLE scans when the screen goes off.

The解决方法是对所有扫描使用ScanFilter。新的8.1操作系统代码仅验证屏幕关闭时处于活动状态的任何扫描至少具有一个扫描过滤器。如果满足这些条件,则扫描结果将按照Android 8.0.x及更早版本的方式提供。

The workaround is to use a ScanFilter with all scans. The new 8.1 operating system code simply verifies that any scans active when the screen is off have at least one scan filter. If those conditions are met the scan results are delivered as in Android 8.0.x and earlier.

要进行此类扫描,您必须使用引入的API Android 5.0,并在每次扫描时创建 ScanFilter 。以下是一个过滤器,可以查找制造商ID为0x004c的Apple的任何设备的制造商广告(这将包括iBeacons):

In order to set up such a scan, you must use the APIs introduced in Android 5.0 and create a ScanFilter with each scan. Below is a filter that will find manufacturer advertisements for any device from Apple with manufacturer ID 0x004c (this will include iBeacons):

ScanFilter.Builder builder = new ScanFilter.Builder();
builder.setManufacturerData(0x004c, new byte[] {});
ScanFilter filter = builder.build();

类似地,如果您对GATT服务广告感兴趣(例如与Eddystone信标一起使用的广告),则可以用这样的过滤器搜索GATT服务UUID:

Similarly, if you are interested in GATT Service advertisements (like the kind used with Eddystone beacons) you can search for a GATT Service UUID with a filter like this:

ScanFilter.Builder builder = new ScanFilter.Builder();
String serviceUuidString = "0000feaa-0000-1000-8000-00805f9b34fb";
String serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
ParcelUuid parcelUuid = ParcelUuid.fromString(serviceUuidString);
ParcelUuid parcelUuidMask = ParcelUuid.fromString(serviceUuidMaskString);
builder.setServiceUuid(parcelUuid, parcelUuidMask);
ScanFilter filter = builder.build();

如果需要,可以将多个过滤器添加到单个扫描中,任何匹配的过滤器都将返回结果。唯一真正的限制是,至少在关闭屏幕扫描时,您必须预先知道所有可能匹配的制造商代码或所有GATT服务UUID。

If needed, you can add multiple filters to a single scan, and any that match will return results. The only real limitation here is that you must know all of the manufacturer codes or all of the GATT Service UUIDs that you might match up front, at least when scanning with the screen off.

您可以使用以下代码开始扫描:

You start your scan with code like this:

bluetoothAdapter.getBluetoothLeScanner().startScan(filters, settings, scanCallback);

编辑:也可以使用 ScanFilter 看起来像这样:

It is also possible to do this with an empty ScanFilter that looks like this:

ScanFilter.Builder builder = new ScanFilter.Builder();
ScanFilter filter = builder.build();

如果使用这样的扫描过滤器,它将匹配任何广告数据包,并且仍然允许使用在Android 8.1上关闭屏幕,有效地使您在Android 8.0.x和更早版本上具有相同的行为。

If you use such a scan filter, it will match any advertising packet, and still allow detections with the screen off on Android 8.1, effectively giving you the same behavior on Android 8.0.x and earlier.

编辑2 :在Galaxy Note 9上如果使用的是Android 8.1以及其他装有8.1的三星设备,则即使屏幕扫描滤镜为空,扫描也会在屏幕关闭的情况下被阻止。如上所述,可以使用非空扫描过滤器在屏幕关闭的情况下进行扫描。

EDIT 2: On the Galaxy Note 9 with Android 8.1 and perhaps other Samsung devices with 8.1, scans are blocked with the screen off even with an empty scan filter. Scans are allowed with the screen off with a non-empty scan filter as described above.

这篇关于当Android 8.1.0上的屏幕关闭时,BLE扫描不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆