Android:如何获得蓝牙扫描状态?

时间:2018-04-14 19:51:36

标签: android bluetooth bluetooth-lowenergy

以下是我开始扫描BLE设备的方法

    if(bluetoothLeScanner!=null) {
        bluetoothLeScanner.startScan(scanCallback);
        isScanning = true;
    }

我如何以及何时将isScanning设置为false?

有没有办法在scanCallback内找到它?

private ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, final ScanResult result) {
        super.onScanResult(callbackType, result);
        if(result==null) return;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bluetoothDeviceAdapter.update(result.getDevice());
                bluetoothDeviceAdapter.notifyDataSetChanged();
            }
        });

        // This doesn't mean end of scan does it?
        // isScanning = false;
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
        final List<BluetoothDevice> deviceList = new ArrayList<>();
        for(ScanResult result: results)
            deviceList.add(result.getDevice());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bluetoothDeviceAdapter.updateBatch(deviceList);
                bluetoothDeviceAdapter.notifyDataSetChanged();
            }
        });

        // This doesn't mean end of scan does it?
        // isScanning = false;
    }
};

1 个答案:

答案 0 :(得分:2)

扫描一直持续到

  1. 你明确地阻止了它。
  2. 调用onScanFailed回调。
  3. 蓝牙已关闭。