【问题标题】:How to check null Bluetooth Low Energy device in android如何在android中检查null蓝牙低功耗设备
【发布时间】:2015-09-30 07:44:09
【问题描述】:

我有与低功耗蓝牙相关的项目。我扫描附近的所有 BLE 设备并通过很多不同的图标显示到屏幕上。我没关系。但我想检查是否扫描不是 BLE 设备,我会显示一条消息“未找到 BLE 设备”。我不知道检查 NULL BLE 设备。也许你知道!?

这是我的一些代码:

    private BluetoothAdapter mBluetoothAdapter;
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    mBluetoothAdapter.startLeScan(mLeScanCallback);

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice arg0, int arg1, byte[] arg2) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //TODO add device to screen

            });
        }
    };

【问题讨论】:

  • 澄清一下,您是否需要检查在 LeScan 中获得的 BluetoothDevice 是否不是 LE 设备(!),或者您是否要检查根本没有调用 onLeScan 的情况?
  • 我想检查一下 onLeScan 根本没有被调用过的情况。

标签: android bluetooth bluetooth-lowenergy


【解决方案1】:

https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#find

根据 Android 文档,开发人员有责任在一定时间间隔后停止 Le 扫描。

在扫描处于活动状态的那个时间间隔内,您可以跟踪触发 onLeScan 回调的次数。如果它甚至没有被触发一次,那么您可以在 Le Scan 停止后显示任何您想要的错误消息。

【讨论】:

  • 是的我明白了,但是当设备出去区域扫描时,我不能在ListView中清除它们。我清除了列表并更新了 ListAdapter,但它仍然显示。
  • 您必须清除要保存设备列表的数组/数组列表。
【解决方案2】:

简单明了,请按照以下步骤操作。

1) 运行 BLE 扫描一段时间。您可以使用 postDelayed 处理程序在一定时间假设 10 秒后调用停止 LE 扫描。

2) 您必须在列表或某处添加找到的设备才能在 UI 上显示它。你说你为不同的设备显示不同的图标。

3) 要检查您的进程是否找到任何设备,您可以检查图标集或设备列表大小。如果它为零,那么您将找不到任何东西。

或者你可以使用一些布尔标志,你可以在 onLeScan 方法中启用(true),这样你就会知道它至少被调用了一次。

快乐编码。

【讨论】:

  • 我确实关注了你,当 BLE 设备打开时,我扫描它并添加到屏幕上的 ListView 显示。但是在扫描时,设备处于关闭状态,我无法删除列表中的那个 BLE 设备。虽然我有明确的列表,但它仍然显示在屏幕上。我该怎么办?
猜你喜欢
  • 2015-06-25
  • 2013-11-17
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 2021-04-06
相关资源
最近更新 更多