gpt4 book ai didi

android - android 8.1.0 手机屏幕关闭时 BLE 扫描停止

转载 作者:行者123 更新时间:2023-11-29 02:33:55 24 4
gpt4 key购买 nike

我将此权限放在 list 文件中。

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

这是一个开始扫描方法。当屏幕打开时扫描工作正常。但是当屏幕关闭时,扫描停止并在屏幕再次打开时恢复。我试过这个solution但仍然面临同样的问题。

 void startScan() {
if (!mScanning) {
final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
final ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(0).setUseHardwareBatchingIfSupported(false).setUseHardwareFilteringIfSupported(false).build();

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

builder.setServiceUuid(new ParcelUuid(THINGY_BASE_UUID));
ScanFilter filter = builder.build();

final List<ScanFilter> filters = new ArrayList<>();
filters.add(filter); // Add Filter using UUID
scanner.startScan(filters, settings, scanCallback);
mScanning = true;
}
}

这是获取扫描结果的回调方法。

 private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(final int callbackType, final ScanResult result) {
// do nothing
Log.e("MainActivity", "onScanResult: " + result.getDevice().getName());
}

@Override
public void onBatchScanResults(final List<ScanResult> results) {
Log.e("MainActivity", "onBatchScanResults: " + results.size());
}

@Override
public void onScanFailed(final int errorCode) {
// should never be called
}

};

最佳答案

如果您使用的是北欧扫描库,试试这个:

 private void scan(){
scanner = BluetoothLeScannerCompat.getScanner();
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(10)
.setCallbackType(DEFAULT_KEYS_SEARCH_GLOBAL)
.setUseHardwareBatchingIfSupported(false).build();
List<ScanFilter> filters = new ArrayList<>();
scanner.startScan(filters, settings, mScanCallback);
}

关于android - android 8.1.0 手机屏幕关闭时 BLE 扫描停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48114602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com