【问题标题】:onLeScan called twice when discovering bluetooth le devices发现蓝牙文件设备时调用了两次 onLeScan
【发布时间】:2017-04-17 02:55:18
【问题描述】:

我正在尝试发现蓝牙低功耗设备,但我对 onLeScan 方法有疑问。它被调用了两次,结果所有设备都翻了一番。

我用来启动扫描的方法:

private void scanLeDevice(final boolean enable) {
        if (enable) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    bluetoothAdapter.stopLeScan(getLeScanCallback());
                    scanning = false;
                }
            }, SCAN_PERIOD);

            scanning = true;
            bluetoothAdapter.startLeScan(getLeScanCallback());
        } else {
            bluetoothAdapter.stopLeScan(getLeScanCallback());
            scanning = false;
        }
    }

返回 LeScanCallback 的方法:

private BluetoothAdapter.LeScanCallback getLeScanCallback(){
        BluetoothAdapter.LeScanCallback leScanCallback =
                new BluetoothAdapter.LeScanCallback() {
                    @Override
                    public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi
                            , byte[] scanRecord) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, "device found");
                                Device device = new Device();
                                device.setName(bluetoothDevice.getName());
                                device.setAddress(bluetoothDevice.getAddress());
                                devices.add(device);
                                deviceListAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                };

        return leScanCallback;
    }

【问题讨论】:

  • 发布的代码可能不足以诊断问题。尝试调试并在 getLeScanCallback 上设置断点以查看它是否被调用 2x。此外,有时会在一次扫描中找到两次设备。所以你需要在添加之前检查它们是否已经在设备中
  • getLeScanCallback 没有被调用两次,但你是对的,我应该检查重复项。我认为每个设备只能被发现一次。谢谢你的帮助。

标签: android bluetooth bluetooth-lowenergy


【解决方案1】:

是的,如果 BLE 设备的广告周期与扫描周期相比足够频繁,则它们可能会在一个扫描周期内出现多次。因此,您应该识别每个 BLE 设备(可能带有名称或地址),并且只将它们添加到您的列表中一次。您可以保留最近看到的信标的单独列表,或者只检查devices 是否已包含找到的设备并避免添加重复项。 (或使用Set 而不是List。)如果列表中已经存在BLE 设备,只需更新任何数据(如显示RSSI)。您可能需要覆盖 Device 中的 equals()hashCode() 以使 List.contains()Set 正常工作

【讨论】:

    【解决方案2】:

    我知道这是很久以前问过的,我在这里发布我的解决方案。代码的问题是在protected void onResume() 中调用了scanLeDevice(true)。为什么这被称为两次?看这里Why is my onResume being called twice?

    你只需要在这里更新你的逻辑。

    if (!scanning ) {
    //If not scanning start scanning
     mLeDeviceListAdapter = new LeDeviceListAdapter();
                // setListAdapter(mLeDeviceListAdapter);
                dialog = ProgressDialog.show(AdjActivity.this, "Searching for the Ring",
                        "Scanning for Ring. Please wait...", true);
                scanLeDevice(true);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-19
      • 2017-03-17
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多