23

I have a linux computer broadcasting a BLE advertisement using the following commands:

 sudo hciconfig hci0 up
 sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00
 sudo hciconfig hci0 leadv 3

This works well but the computer only broadcasts its bluetooth advertisement once a second. I would like to increase this frequency to 10 times per second or more. Is there a way to increase advertising frequency in BlueZ? Or is once per second the standard and unchangeable? I'm happy to do this with C APIs if not possible with command line tools.

jjnebeker
  • 1,238
  • 1
  • 9
  • 12

1 Answers1

51

I think I figured it out.

Instead of:

sudo hciconfig hci0 up
sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00
sudo hciconfig hci0 leadv 3

Do this:

sudo hciconfig hci0 up
sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00 00 00 00 00 00 00 00 00 00 00 00 00
sudo hcitool -i hci0 cmd 0x08 0x0006 A0 00 A0 00 03 00 00 00 00 00 00 00 00 07 00
sudo hcitool -i hci0 cmd 0x08 0x000a 01

The second hcitool command (0x08 0x0006) is "LE Set Advertising Parameters. The first two bytes A0 00 are the "min interval". The second two bytes A0 00 are the "max interval". In this example, it sets the time between advertisements to 100ms. The granularity of this setting is 0.625ms, so setting the interval to 01 00 sets the advertisement to go every 0.625ms. Setting it to A0 00 sets the advertisement to go every 0xA0*0.625ms = 100ms. Setting it to 40 06 sets the advertisement to go every 0x0640*0.625ms = 1000ms. The fifth byte, 03, sets the advertising mode to non-connectable. With a non-connectable advertisement, the fastest you can advertise is 100ms, with a connectable advertisment (0x00) you can advertise much faster.

The third hcitool command (0x08 0x000a) is "LE Set Advertise Enable". It is necessary to issue this command with hcitool instead of hciconfig, because "hciconfig hci0 leadv 3" will automatically set the advertising rate to the slower default of 1280ms.

I figured this out by running hcidump at the same time as running the original commands you posted in the question. This shows you a bunch of raw hcitool commands (nicely annotated for what they do) that get executed by bluez. I just happened to notice from the hcidump output that "hciconfig hci0 leadv 3" issues its a slower set advertising interval command.

Note that all of this is based on the IOGear GBU521, so this may not work with other Bluetooth LE chipsets.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • This was incredibly helpful. Do you have a link to the docs you found this in? – schodge Feb 18 '15 at 19:24
  • How are the hex commands determined? Can you please point us to the documentation? Thanks. – neowulf33 Jun 11 '15 at 02:56
  • I determed the hex commands "by running hcidump at the same time as running the original commands... posted in the question. This shows you a bunch of raw hcitool commands (nicely annotated for what they do)" – davidgyoung Jun 11 '15 at 03:14
  • 1
    Note that your 0x0008 command should have a twelve 00 removed at the end, to be like: `sudo hcitool -i hci0 cmd 0x08 0x0008 1e 02 01 1a 1a ff 4c 00 02 15 e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 00 00 00 00 c5 00`, so it conforms to specs (0x1e being number of significant octets, and following data is 31 octets of Advertising Data) – domen Sep 09 '15 at 14:18
  • 6
    @neowulf33 hex commands are documented in Bluetooth specification 4.2 (Core_v4.2.pdf). It's freely available from https://www.bluetooth.org/en-us/specification/adopted-specifications – domen Sep 09 '15 at 14:19
  • 3
    Commands are described in "Part E - host controller interface functional specification". Chapters describing commands generally follow the form of 7.ogf.x. – domen Sep 09 '15 at 14:21
  • Just to let the readers know, I have used a usb dongle provided by Plugable as well as a CSR v4.0 in both raspbian and kali linux however I wasn't able to achieve more than 1Hz of advertising even if the correct parameters were used. – dr.doom Jun 13 '16 at 13:57
  • 2
    This all comes from the Bluetooth Core spec, volume 2 part E section 7.8.5, which says `The Advertising_Interval_Min and Advertising_Interval_Max should not be the same value to enable the Controller to determine the best advertising interval given other activities.` You might want to separate those values a bit. – nmichaels Aug 22 '17 at 19:54
  • 1
    In Bluetooth specification 5.0 (Core_v5.0.pdf), commands documented at "Vol 2, Part E - Host Controller Interface Functional Specification" – RootCode Mar 13 '18 at 10:48
  • 1
    In 2020 - this still works. OpenWRT 18.06, Kernel 4.9, Bluez 5.49. Thank you. – kyb May 25 '20 at 17:24