Bluetooth 如何使用蓝牙CTL(如hcitool lescan)报告重复的接近信标

Bluetooth 如何使用蓝牙CTL(如hcitool lescan)报告重复的接近信标,bluetooth,linux-kernel,bluetooth-lowenergy,embedded-linux,Bluetooth,Linux Kernel,Bluetooth Lowenergy,Embedded Linux,我可以使用hcitool lescan和--duplicates标志从附近的两个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 &

我可以使用hcitool lescan和--duplicates标志从附近的两个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>

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

我刚刚发现以下内容适用(Ubuntu 18.04.1,bluez 5.48):


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

感谢Florian的建议

从我最初的帖子开始,我已经迁移到了Ubuntu 16.04.4嵌入式Linux发行版,其中包括Bluez5.42。不幸的是,此版本的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日志记录活动会很快使键入变得模糊。因此,我编写了一个bash脚本,它使用一个herdeoc,并希望将命令提供给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), " ", $_'
bluetoothctl现在报告重复的信标,类似于使用duplicates标志运行hcitool lescan时

我要说的是,如果可以从命令行配置bluetoothctl,而不需要以交互方式进行配置,也不需要使用更复杂的脚本,那么bluetoothctl将更易于使用


感谢Florian的帮助。

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

[bluetooth]# clear
Invalid command
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
$ 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