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

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

问题描述

我可以将hcitool lescan与--duplicates标志一起使用,以从附近的两个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会议和IoT峰会上,使用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?

推荐答案

感谢弗洛里安的建议.

自从我发表原始文章以来,我已迁移到包含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

但是,由于您提到阻止大多数广告的默认默认扫描过滤器处于活动状态",我对此进行了尝试,在--help输出中看到了我的bluetoothctl版本中可用的命令,并且可以正常工作:

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\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运行时的信标.

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这样的bluetoothctl报告重复的邻近信标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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