我希望对广播的设备进行实时响应。自Android Lollipop(SDK 21)以来,有新API,我在
上进行了测试https://developer.android.com/reference/android/bluetooth/le/ScanSettings.html ScanSettings.SCAN_MODE_LOW_LATENCY
使用最高占空比扫描。建议仅在应用程序在前台运行时才使用此模式。
这是代码开始扫描
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
List<ScanFilter> filters = new ArrayList<ScanFilter>();
ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
bluetoothLeScanner.startScan(filters, scanSettings, scanCallback );
}
它只是测量ScanCallback()
中的帧速率private MeasureFps measureFps = new MeasureFps(6);
private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
measureFps.AddRecord(true);
}
};
MeasureFps的源代码是here。
我在Nexus 5X上运行测试,这是Android Marshmallow。
结果大多超过300毫秒,可能是2秒
这些是预期的吗?
如果它们稳定在300毫秒以下就会很棒。
22:18:34.203 I/MeasureFps: Average frame time = 2237.4 ms
22:18:36.792 I/MeasureFps: Average frame time = 485.6 ms
22:18:40.481 I/MeasureFps: Average frame time = 724.0 ms
22:18:43.467 I/MeasureFps: Average frame time = 343.8 ms
22:18:44.904 I/MeasureFps: Average frame time = 251.8 ms
22:18:47.238 I/MeasureFps: Average frame time = 427.8 ms
22:18:48.491 I/MeasureFps: Average frame time = 73.4 ms
22:18:56.139 I/MeasureFps: Average frame time = 288.6 ms
22:18:57.411 I/MeasureFps: Average frame time = 237.4 ms
22:18:58.393 I/MeasureFps: Average frame time = 182.2 ms
22:18:59.953 I/MeasureFps: Average frame time = 310.4 ms
22:19:02.446 I/MeasureFps: Average frame time = 497.2 ms
22:19:04.349 I/MeasureFps: Average frame time = 342.8 ms
22:19:12.984 I/MeasureFps: Average frame time = 1715.0 ms