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

3

以下是我开始扫描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个回答

3

扫描一直运行直到

  1. 您明确停止它。
  2. 调用了onScanFailed回调函数。
  3. 蓝牙被关闭。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接