参考nrf 高效蓝牙扫描(6.0兼容)

代码很简单,6.0以上的需要判断下权限就可以。
//参考nrf 工具扫描

    public void startScanV18() {
        BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
        final no.nordicsemi.android.support.v18.scanner.ScanSettings settings = new no.nordicsemi.android.support.v18.scanner.ScanSettings.Builder()
                .setLegacy(false)
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(1000).setUseHardwareBatchingIfSupported(false).build();
        final List<no.nordicsemi.android.support.v18.scanner.ScanFilter> filters = new ArrayList<>();
        filters.add(new ScanFilter.Builder().setServiceUuid(null).build());

        scanner.startScan(filters, settings, scanCallbackV18);
        isScanning = true;

    }

    private no.nordicsemi.android.support.v18.scanner.ScanCallback scanCallbackV18 = new no.nordicsemi.android.support.v18.scanner.ScanCallback() {
        @Override
        public void onScanResult(final int callbackType, final no.nordicsemi.android.support.v18.scanner.ScanResult result) {
            // do nothing
        }

        @Override
        public void onBatchScanResults(final List<no.nordicsemi.android.support.v18.scanner.ScanResult> results) {
       		//只需要这个一个回调即可 6.0上下都会走这里
        }

        @Override
        public void onScanFailed(final int errorCode) {
            // should never be called
        }
    };
	public void stopScanV18() {
        isScanning = false;
        final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
        scanner.stopScan(scanCallbackV18);
    }

你可能感兴趣的:(参考nrf 高效蓝牙扫描(6.0兼容))