gpt4 book ai didi

android - ScanCallback onBatchScanResults 被无限期调用

转载 作者:行者123 更新时间:2023-11-29 19:03:40 27 4
gpt4 key购买 nike

我试图在找到我的设备后扫描 BLE 设备,或者在 10 秒后,我试图停止扫描。但由于某种原因,onBatchScanResults 被无限期地调用。

我发现即使在停止扫描之后,也会调用 onBatchScanResults 直到扫描结果队列被耗尽。但就我而言,它永远不会停止。下面是我如何尝试实现这一目标的代码。

public void scan() {
scanner = BluetoothLeScannerCompat.getScanner();
final ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(1000).setUseHardwareBatchingIfSupported(false).build();
final List<ScanFilter> filters = new ArrayList<>();
filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(getFilterUUID()))
.build());
Log.e(TAG, "Scanning.....");
scanner.startScan(filters, settings, scanCallback);

mIsScanning = true;
mHandler.postDelayed(() -> {
if (mIsScanning) {
showToast("Not able to find any new device.");
stopScan();
}
}, SCAN_DURATION);
}

private void stopScan() {
if (mIsScanning) {
final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scanner.stopScan(scanCallback);
mIsScanning = false;
closeWaitDialog();
}

private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(final int callbackType, final ScanResult result) {
// do nothing
stopScan();
}

@Override
public void onBatchScanResults(final List<ScanResult> results) {
Log.e(TAG, results.toString() + " mIsScanning " + mIsScanning);
if (results.size() == 1) {
stopScan();
ScanResult scanResult = results.get(0);
launchSomeActivity();
} else if (results.size() > 1) {
stopScan();
showToast("Too many new devices. Please scan one device at a time.");
} else {
// Do nothing. As we will stop anyway stop scanning after 5 sec.
}
}

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

非常感谢任何帮助。

最佳答案

首先,您调用 stopScan() 的次数太多了。在 stopScan() 中,您正在创建具有相同名称 scannerBluetoothLeScannerCompat 的新实例。另外,从 ScanCallback 调用 stopScan() 不是一个好的做法。这是工作示例:

BluetoothLeScannerCompat scanner;
public void scan() {
scanner = BluetoothLeScannerCompat.getScanner();
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(10)
.build();
List<ScanFilter> filters = new ArrayList<>();
filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(mUUid)).build());
scanner.startScan(filters,settings, scanCallback);


final Handler handle = new Handler();
handle.postDelayed(new Runnable() {
@Override
public void run() {
stopScan();
}
},2000);
}

private void stopScan() {
Log.i("Device Found: ", "Scan Stoped");
scanner.stopScan(scanCallback);
}

private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(final int callbackType, final ScanResult result) {
}

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

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

日志

当您从 scanCallback 调用 StopScan() 时:

01-17 14:52:53.870 32434-32434/com.dleague.salman.example I/OnBatchScan:                     
Scanning...
01-17 14:52:53.870 32434-32434/com.dleague.salman.example I/StopScan:
Scanning Stop...
01-17 14:52:53.881 32434-32434/com.dleague.salman.example I/OnBatchScan:
Scanning...
01-17 14:52:53.881 32434-32434/com.dleague.salman.example I/StopScan:
Scanning Stop...
01-17 14:52:53.892 32434-32434/com.dleague.salman.example I/OnBatchScan:
Scanning...
01-17 14:52:53.892 32434-32434/com.dleague.salman.example I/StopScan:
Scanning Stop...
01-17 14:52:53.902 32434-32434/com.dleague.salman.example I/OnBatchScan:
Scanning...
01-17 14:52:53.902 32434-32434/com.dleague.salman.example I/StopScan:
Scanning Stop...
01-17 14:52:53.912 32434-32434/com.dleague.salman.example I/OnBatchScan:
Scanning...
01-17 14:52:53.912 32434-32434/com.dleague.salman.example I/StopScan:
Scanning Stop...
01-17 14:52:53.923 32434-32434/com.dleague.salman.example I/OnBatchScan:
Scanning...

并且当您从 ScanPeriod 在 scanCallback 中执行 StopScan() 时:

01-17 14:55:10.703 1673-1673/com.dleague.salman.example I/OnBatchScan: 
Scanning...
01-17 14:55:10.714 1673-1673/com.dleague.salman.example I/OnBatchScan:
Scanning...
....
01-17 14:55:12.684 1673-1673/com.dleague.salman.example I/OnBatchScan:
Scanning...
01-17 14:55:12.694 1673-1673/com.dleague.salman.example I/OnBatchScan:
Scanning...
01-17 14:55:12.700 1673-1673/com.dleague.salman.example I/StopScan: Scanning
Stop...

关于android - ScanCallback onBatchScanResults 被无限期调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47981327/

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