如何使用像 hcitool lescan 这样的蓝牙控制来报告重复的邻近信标 [英] How to use bluetoothctl like hcitool lescan to report repeated proximity beacons

查看:10
本文介绍了如何使用像 hcitool lescan 这样的蓝牙控制来报告重复的邻近信标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can use hcitool lescan with the --duplicates flag to capture periodic LE Advertising Reports (proximity beacons) from two nearby BLE devices:

$ 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>

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

Here is that scan again, with timestamps added to show the periodicity:

$ 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.

At the Embedded Linux Conference and IoT Summit last week (April 2016), a presenter who works on the BlueZ stack said to no longer use hcitool, and to use bluetoothctl instead.

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

I tried that today, but it only shows the first LE Advertising Report for a device (like hcitool does without the --duplicates option):

$ 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 选项所做的那样?

How do you use bluetoothctl to capture repeated LE Advertising Reports from the same device, like hcitool does with the --duplicates option?

推荐答案

感谢 Florian 的建议.

Thanks Florian for your suggestion.

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

Since my original post, I have migrated to an Ubuntu 16.04.4 embedded Linux distribution which includes bluez 5.42. Unfortunately, bluetoothctl with this version does not recognize 'menu scan' or 'clear':

[bluetooth]# menu scan
Invalid command

[bluetooth]# clear
Invalid command

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

However, encouraged by your mention of 'default scan filter active that blocks most advertisements', I experimented with the commands available in my version of bluetoothctl as seen in the --help output, and got something working:

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 按摩输出:

It takes a bit of typing in bluetoothctl to get it configured the way I want, and that typing is obscured by bluetoothctl logging activity rapidly in our beacon rich environment. So I put together a bash script that uses a heredoc and expect to feed the commands to bluetoothctl and sed/grep/perl massages the output:

$ 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
"

    expect -re $prompt
    send "remove *
"

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

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

    expect -re $prompt
    send "scan on
"

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

        expect -re $prompt
        send "remove *
"

        expect -re $prompt
        send "quit
"
    } 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 时.

Beacons that repeat are now reported by bluetoothctl, similar to when hcitool lescan is run with the duplicates flag.

我会说,如果可以从命令行配置 bluetoothctl,而无需交互配置或使用更复杂的脚本,它会更易于使用.

I will say that bluetoothctl would be easier to use if it could be configured from the command line without needing to configure it interactively or having to resort to more complex scripting.

感谢弗洛里安的帮助.

这篇关于如何使用像 hcitool lescan 这样的蓝牙控制来报告重复的邻近信标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆