简体   繁体   English

BlueZ:从命令行发送LE定向广告

[英]BlueZ: sending LE directed adverts from the command line

I am trying to send out directed adverts from the command line but I can't figure out how to do it. 我试图从命令行发送定向广告,但我无法弄清楚如何做到这一点。 I've checked hciconfig , hcitool , btmgmt , and bluetoothctl but none of them seem to enable directed adverts to a specific BLE device. 我检查hciconfig,hcitool,btmgmtbluetoothctl但他们都不让广告定向到一个特定的BLE装置。 When using hciconfig to check the type of adverts I can send, I see the following:- 当使用hciconfig检查我可以发送的广告类型时,我看到以下内容: -

leadv      [type]   Enable LE advertising
                0 - Connectable undirected advertising (default)
                3 - Non connectable undirected advertising

Is there any way to send directed adverts from the command line? 有没有办法从命令行发送定向广告?

I am using BlueZ ver 5.48 running on Linux v4.15.0-36-generic with a Bluetooth 4.0 dongle attached. 我使用的是运行在Linux v4.15.0-36-generic上的BlueZ ver 5.48,附带蓝牙4.0加密狗。

You can do this at least with raw HCI commands, which you can enter with hcitool. 您至少可以使用原始HCI命令执行此操作,您可以使用hcitool输入这些命令。 The key HCI command you need is "LE Set Advertising Parameters Command" (ogf=0x08, ocf=0x0006). 您需要的关键HCI命令是“LE Set Advertising Parameters Command”(ogf = 0x08,ocf = 0x0006)。 Here is an example of setting type 2 advertisement to peer address 66:55:44:33:22:11. 以下是将类型2广告设置为对等地址66:55:44:33:22:11的示例。

sudo hcitool -i hci0 cmd 0x08 0x0006 A0 00 A0 00 02 01 00 11 22 33 44 55 66 07 00

The first A0 00 is minimum advertisement interval (0x00A0 x 0.625ms = 100ms) and second is the maximum advertisement interval (you might actually want to separate them a bit). 第一个A0 00是最小广告间隔(0x00A0 x 0.625ms = 100ms),第二个是最大广告间隔(您可能实际上想要将它们分开一点)。 Then follows the type (02), own address type (01 for random), remote address type (00 for public), peer address, channel map (07 meaning advertising on channels 37, 38 and 39) and filter policy (00 meaning allow all). 然后是类型(02),自己的地址类型(01为随机),远程地址类型(00为公共),对等地址,频道映射(07表示在频道37,38和39上做广告)和过滤策略(00表示允许所有)。 More details can be read eg from Bluetooth Specification, Version 5.0, Vol 2, Part E, Chapt 7.8 (downloadable from https://www.bluetooth.com/specifications/bluetooth-core-specification ). 更多细节可以从例如Bluetooth Specification,Version 5.0,Vol 2,Part E,Chapt 7.8(可从https://www.bluetooth.com/specifications/bluetooth-core-specification下载)中读取。

Note that advertisement must not be active when using this command, otherwise it will fail. 请注意,使用此命令时,广告不得处于活动状态,否则将失败。

Also note that some of the tools you mention in your question might impose their own defaults to these parameters, eg change the advertisement interval to something larger. 另请注意,您在问题中提到的某些工具可能会将这些工具的默认值强加给这些参数,例如将广告间隔更改为更大的值。 For this reason it is safest to enable the advertisement with raw HCI command as well, which is simply: 出于这个原因,使用原始HCI命令启用广告是最安全的,这很简单:

sudo hcitool -i hci0 cmd 0x08 0x000a 01

For the sake of completeness, here's the set of commands to start directed advertisement with vendor specific payload (Apple in this case, which you obviously should not use without permission). 为了完整起见,这里是一组命令,用于启动具有供应商特定负载的定向广告(在这种情况下,Apple,显然不应该未经许可使用)。 Advertisement payload is set with 0x08 0x0008. 广告有效载荷设置为0x08 0x0008。

sudo hciconfig hci0 up
sudo hcitool -i hci0 cmd 0x08 0x0008 0b 09 ff 4c 00 30 31 32 33 34 35 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
sudo hcitool -i hci0 cmd 0x08 0x0006 A0 00 A0 00 02 00 01 11 22 33 44 55 66 07 00
sudo hcitool -i hci0 cmd 0x08 0x000a 01

You can monitor the HCI interface with btmon (comes with BlueZ) while issuing these or any other commands. 您可以在发出这些或任何其他命令时使用btmon(BlueZ附带)监视HCI接口。 It parses the packets nicely, so you can easily see how editing your raw command changes the meaning. 它可以很好地解析数据包,因此您可以轻松地查看编辑原始命令如何更改其含义。 It also highlights any broken HCI commands it notices. 它还突出显示它注意到的任何损坏的HCI命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM