无法正确调用ScanCallback onBatchScanResults

时间:2017-12-26 16:40:15

标签: android bluetooth-lowenergy android-bluetooth android-ble

我尝试在找到设备后扫描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
    }
};

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

首先,您要多次调用stopScan()方式。在BluetoothLeScannerCompat中,您正在使用同名scanner创建stopScan()的新实例。另外,从ScanCallback致电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 } }; 不是一个好习惯。 这是工作示例:

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...

<强>日志

从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...

当您从scanCallback中的 ScanPeriod 中阻止扫描()

{{1}}

答案 1 :(得分:1)

如果要在使用.setUseHardwareBatchingIfSupported(true)时使用ScanSettings,只需将stopScan添加到setReportDelay > 0

ScanSettings settings = new ScanSettings.Builder()
            .setUseHardwareBatchingIfSupported(true)
            .setReportDelay(1000)
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
            .build();

答案 2 :(得分:0)

您正在创建ScanCallback的新实例并且丢失对先前实例的引用。这就是为什么您无法停止ScanCallback旧实例的原因。 只需在类的开头创建一个ScanCallBack实例,并在扫描逻辑中使用它。

答案 3 :(得分:0)

为了得到答案。

此错误已在最新版本的库(Scanner Compact Library 1.2.0)中修复。