【问题标题】:How to use bluetoothctl like hcitool lescan to report repeated proximity beacons如何使用像 hcitool lescan 这样的 bluetoothctl 来报告重复的接近信标
【发布时间】:2016-08-05 02:15:00
【问题描述】:

我可以使用带有 --duplicates 标志的 hcitool lescan 从附近的两个 BLE 设备捕获定期 LE 广告报告(接近信标):

$ sudo hcitool lescan --duplicates
LE Scan ...
C8:0F:10:29:4D:98 MI1S
C8:0F:10:29:4E:75 MI1S
C8:0F:10:29:4E:75 MI1S
C8:0F:10:29:4D:98 MI1S
C8:0F:10:29:4E:75 MI1S
C8:0F:10:29:4D:98 MI1S
<snip>

这是再次扫描,添加了时间戳以显示周期性:

$ sudo stdbuf -i0 -o0 -e0 hcitool lescan --duplicates | perl -nle 'print scalar(localtime), " ", $_'
Wed Apr 13 13:46:45 2016 LE Scan ...
Wed Apr 13 13:46:46 2016 C8:0F:10:29:4E:75 MI1S
Wed Apr 13 13:46:47 2016 C8:0F:10:29:4D:98 MI1S
Wed Apr 13 13:46:48 2016 C8:0F:10:29:4E:75 MI1S
Wed Apr 13 13:46:48 2016 C8:0F:10:29:4D:98 MI1S
Wed Apr 13 13:46:50 2016 C8:0F:10:29:4D:98 MI1S
Wed Apr 13 13:46:52 2016 C8:0F:10:29:4E:75 MI1S
<snip>

在上周(2016 年 4 月)的嵌入式 Linux 会议和物联网峰会上,一位从事 BlueZ 堆栈工作的演讲者表示不再使用 hcitool,而是改用 bluetoothctl。

我今天试过了,但它只显示设备的第一个 LE 广告报告(就像 hcitool 没有 --duplicates 选项一样):

$ sudo bluetoothctl
[NEW] Controller 5C:F3:70:62:68:28 BlueZ 5.38 [default]

[bluetooth]# power on
Changing power on succeeded
[CHG] Controller 5C:F3:70:62:68:28 Powered: yes

[bluetooth]# scan on
Discovery started
[CHG] Controller 5C:F3:70:62:68:28 Discovering: yes
[CHG] Device C8:0F:10:29:4E:75 RSSI: -72
[CHG] Device C8:0F:10:29:4D:98 RSSI: -65

[bluetooth]# devices
Device C8:0F:10:29:4D:98 MI1S
Device C8:0F:10:29:4E:75 MI1S

您如何使用 bluetoothctl 从同一设备捕获重复的 LE 广告报告,就像 hcitool 使用 --duplicates 选项一样?

【问题讨论】:

  • 我很想知道为什么建议改用 bluetoothctl,以及您是否能够使其正常工作。
  • 因为bluetoothctl 通过 DBus 与蓝牙守护进程对话,而不是像 hcitool 那样直接与硬件对话。而且 hcitool 现在通常会失败,因为 BT 守护进程具有独占访问权限。

标签: bluetooth linux-kernel bluetooth-lowenergy embedded-linux


【解决方案1】:

刚刚发现以下对我有用(Ubuntu 18.04.1、bluez 5.48):

$ bluetoothctl
[bluetooth]# scan on
[bluetooth]# menu scan
[bluetooth]# clear
SetDiscoveryFilter success
[NEW] Device de:ad:be:ef:ca:fe SampleDev
[CHG] Device de:ad:be:ef:ca:fe RSSI: -73
[CHG] Device de:ad:be:ef:ca:fe RSSI: -73
[CHG] Device de:ad:be:ef:ca:fe RSSI: -74

似乎有一个默认扫描过滤器处于活动状态,可以阻止大多数广告。

【讨论】:

  • ?。除了这些步骤之外,我需要在启动后第一次运行时在scan on之前输入power on
【解决方案2】:

感谢弗洛里安的建议。

自从我发表最初的帖子以来,我已迁移到包含 bluez 5.42 的 Ubuntu 16.04.4 嵌入式 Linux 发行版。不幸的是,此版本的 bluetoothctl 无法识别“菜单扫描”或“清除”:

[bluetooth]# menu scan
Invalid command

[bluetooth]# clear
Invalid command

但是,由于您提到“阻止大多数广告的默认扫描过滤器处于活动状态”,我尝试了我的 bluetoothctl 版本中可用的命令,如 --help 输出所示,并得到了一些工作:

root@iot:~# bluetoothctl
[NEW] Controller 00:1A:7D:DA:71:13 iot #1 [default]
[NEW] Controller 70:2C:1F:31:F4:AF iot 

[bluetooth]# set-scan-filter-clear
SetDiscoveryFilter success

[bluetooth]# set-scan-filter-transport le
SetDiscoveryFilter success

[bluetooth]# scan on
Discovery started

[CHG] Controller 00:1A:7D:DA:71:13 Discovering: yes
[NEW] Device 0F:64:64:EE:E7:C4 0F-64-64-EE-E7-C4
[NEW] Device 0D:6F:45:77:87:F3 0D-6F-45-77-87-F3
[NEW] Device 40:CB:C0:F2:96:27 40-CB-C0-F2-96-27
[CHG] Device 0D:6F:45:77:87:F3 RSSI: -71
[CHG] Device FC:F1:36:73:77:B3 RSSI: -57
[CHG] Device 0F:64:64:EE:E7:C4 RSSI: -49

需要输入一些 bluetoothctl 才能按照我想要的方式进行配置,并且在我们的信标丰富的环境中,这种输入会被 bluetoothctl 日志记录活动迅速掩盖。所以我整理了一个使用heredoc的bash脚本,并期望将命令提供给bluetoothctl,sed/grep/perl按摩输出:

$ cat beacon-scan.sh 
#!/bin/bash

# beacon-scan.sh
# Displays beacons including duplicates in real time.
# Uses expect to automate interaction with bluetoothctl.
# Uses sed to remove bluetoothctl colorization escape characters.
# Uses grep to filter out beacon manufacturer data logging.
# Uses perl to prefix each beacon with a timestamp.

if [ "$(id -u)" != "0" ]; then
    echo "ERROR: must run as root"
    exit 1
fi

(cat <<'END' | /usr/bin/expect

    set prompt "#"
    set timeout -1

    spawn bluetoothctl

    expect -re $prompt
    send "scan off\r"

    expect -re $prompt
    send "remove *\r"

    expect -re $prompt
    send "set-scan-filter-clear\r"

    expect -re $prompt
    send "set-scan-filter-transport le\r"

    expect -re $prompt
    send "scan on\r"

    trap {
        expect -re $prompt
        send "scan off\r"

        expect -re $prompt
        send "remove *\r"

        expect -re $prompt
        send "quit\r"
    } SIGINT

    expect eof

END
) | sed --unbuffered --quiet --expression 's/^.*Device //p' \
  | grep --line-buffered -v ManufacturerData \
  | perl -nle 'print scalar(localtime), " ", $_'

有效:

$ sudo ./beacon-scan.sh 
Wed Aug 22 19:34:07 2018 0F:64:64:EE:E7:C4 RSSI: -59
Wed Aug 22 19:34:07 2018 03:46:00:1D:E9:91 03-46-00-1D-E9-91
Wed Aug 22 19:34:07 2018 4E:20:6B:C7:68:D0 RSSI: -55
Wed Aug 22 19:34:07 2018 76:F1:1A:B9:ED:28 RSSI: -57
Wed Aug 22 19:34:07 2018 32:5D:8C:6A:72:C2 32-5D-8C-6A-72-C2
^C

bluetoothctl 现在报告重复的信标,类似于使用重复标志运行 hcitool lescan 时。

我会说如果 bluetoothctl 可以从命令行配置,而不需要交互式配置或求助于更复杂的脚本,那么它会更容易使用。

感谢弗洛里安的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2014-03-30
    • 2017-04-17
    • 1970-01-01
    • 2021-03-26
    • 2014-09-11
    • 2015-01-08
    相关资源
    最近更新 更多