Advertise Bluetooth LE Service using HCITool

2019-06-24 01:15发布

问题:

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.

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: Setting Group to BLE
  • 0x0008: Setting Command to HCI_LE_Set_Advertising_Data

  • 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)
    

(End of Flag)

  • 0x11 Setting the Length of Service Structure to 17 Bytes
  • 0x07 Setting the Structure Type to 128 Bit Complete Service UUID List
  • 0x41 ... 0x50 Setting the UUID of the Test Service to ABCDEFGHIJKLMNOP

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?

(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)

回答1:

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

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

With the zero padded one:

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 

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.

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.