使用HCITool播发Bluetooth LE服务 [英] Advertise Bluetooth LE Service using HCITool

查看:602
本文介绍了使用HCITool播发Bluetooth LE服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Linux计算机上创建一个低功耗蓝牙外围设备(目标是通过蓝牙从iPhone发送数据).我目前正在使用工具hciconfighcitoolhcidump.

I'm experimenting with creating a Bluetooth Low Energy Peripheral on my Linux computer (The goal is to send data over Bluetooth From an iPhone). Im currently using the Tools hciconfig, hcitool and hcidump.

我当前的实验是播发带有特定UUID的服务,iOS CoreBluetooth库将使用该服务. (注意:我不是要创建iBeacon).

My current experiment is to advertise a Service with a Specific UUID, that the iOS CoreBluetooth Library will pick up. (Note: I'm not trying to create an iBeacon).

现在,它实际上就像困扰着我的一个命令一样简单.

Right now, it's actually as simple as One Single Command that is bugging me.

hcitool -i hci0 cmd 0x08 0x0008 15 02 01 1a 11 07 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50

我认为它应该做的是以下事情:

What I think it should do is the following:

  • 0x08:将组设置为BLE
  • 0x0008:将命令设置为HCI_LE_Set_Advertising_Data
  • 0x15:将标头中的重要字节的长度设置为21(标志包为3字节,服务结构为18字节)
  • 0x02:将标志结构的长度设置为2个字节
  • 0x01:将结构类型设置为AD标志
  • 0x1a:标志值:

  • 0x15: Setting the Length of the Significant Bytes in the Header to 21. (3 Byte for the Flag packet, 18 Byte for the Service Structure)
  • 0x02: Setting the Length of the Flags structure to 2 Bytes
  • 0x01: Setting the structure Type to AD Flags
  • 0x1a: Flag Value:

bit 0 (OFF) LE Limited Discoverable Mode
bit 1 (ON) LE General Discoverable Mode
bit 2 (OFF) BR/EDR Not Supported
bit 3 (ON) Simultaneous LE and BR/EDR to Same Device Capable (controller)
bit 4 (ON) Simultaneous LE and BR/EDR to Same Device Capable (Host)

(标志结束)

  • 0x11将服务结构的长度设置为17个字节
  • 0x07将结构类型设置为128位完整服务UUID列表
  • 0x41 ... 0x50将测试服务的UUID设置为ABCDEFGHIJKLMNOP

据我在hcidump上看到的那样,它已正确执行,并按照我想要的方式显示.但是它被错误拒绝了:

As far as I can see with hcidump, it's executed properly and looks the way I wanted to. But it's rejected with Error:

LE Set Advertising Data (0x08|0x0008) ncmd 1
status 0x12
Error: Invalid HCI Command Parameters

我花了整整一天的时间来纠正它.熟练的人看到我做错了吗?这是宣传服务的正确方法吗?

And I have spent a whole day trying to get it right. Does someone skilled see what I have done wrong? And is this the correct way to advertise a Service?

(感兴趣的读者的背景:我已经成功使用NodeJs中的Bleno库完成了我想做的事情.但是,这并不适合我们系统中的大局.直接使用HCITool进行广告宣传只是为了进行实验和稍后将用Python编写)

(Context for the Interested reader: I have successfully accomplished what I want to do using the Bleno Library in NodeJs. However, this will not fit into the bigger picture in our System. Using HCITool directly for advertising is just for experimentation and will be written in Python later)

推荐答案

HCI_LE_Set_Advertising_Data有效负载的长度应恰好为32个字节.尝试将命令填充零以达到32个字节:

The length of the the HCI_LE_Set_Advertising_Data payload should be exactly 32 bytes. Try zero padding the command to reach 32 bytes:

hcitool -i hci0 cmd 0x08 0x0008 15 02 01 1a 11 07 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 00 00 00 00 00 00 00 00 00 00

您可以使用hcidump --raw.获得更多见解 比较原始命令的输出:

You can gain some more insight using hcidump --raw. Compare the output of the original command:

$hcidump --raw
HCI sniffer - Bluetooth packet analyzer ver 5.30
device: hci0 snap_len: 1500 filter: 0xffffffffffffffff
< 01 08 20 16 15 02 01 1A 11 07 41 42 43 44 45 46 47 48 49 4A 
  4B 4C 4D 4E 4F 50 
> 04 0E 04 01 08 2

用零填充一个:

HCI sniffer - Bluetooth packet analyzer ver 5.30
device: hci0 snap_len: 1500 filter: 0xffffffffffffffff
< 01 08 20 20 15 02 01 1A 11 07 41 42 43 44 45 46 47 48 49 4A 
  4B 4C 4D 4E 4F 50 00 00 00 00 00 00 00 00 00 00 
> 04 0E 04 01 08 20 00 

获得更多见解的另一种方法是运行hciconfig hci0 leadv并使用hcidump --raw检查hciconfig发送的SET_ADVERTISING_PARAMETERS命令的有效负载.

Another way to gain more insight is to run hciconfig hci0 leadv and use hcidump --raw to examine the payload of the SET_ADVERTISING_PARAMETERS command send by hciconfig.

顺便说一句,我注意到有时非零填充命令也可以使用,这可能取决于您使用的bluez版本.

By the way, I've noticed that sometimes a non zero padded command also works, it might depend on the bluez version you are using.

这篇关于使用HCITool播发Bluetooth LE服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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