Use BlueZ Stack As A Peripheral (Advertiser)

Ceryni picture Ceryni · Apr 22, 2013 · Viewed 48.6k times · Source
  • Goal: Use BlueZ and an Bluetooth 4LE dongle to create a peripheral that advertises the bluetooth equivalent of "Hello World".

  • Where I'm At: I've currently got the BlueZ stack setup and downloaded, I can use the hci tool to recognize and see the Bluetooth dongle. I've tinkered with hciconfig leadv but I'm just not quite getting it/understanding what's going on.

  • What Help I Think I Need: I need to get it to the next step. If anyone can either point me towards a good resource, walk me through this, or anything, It would be much appreciated. If I need to do additional leg(search)work I can but I've scoured Google and SO with as many different derivatives of this question as I can think of.

*I tagged this as CoreBluetooth as well in hopes that maybe an iOS dev has tinkered with this at some point.

EDIT: In response to a comment, It seems prudent to state what my end goal is. I'd ultimately like to advertise via the dongle the simplest of simple messages/signals and pick that up on an iOS device (CoreBluetooth). I've been able to get the iOS side of things working well (tons of documentation compared to the Linux side of things) but that hard part for me is getting this adapter setup as a peripheral. The BlueZ stack is a terrible enigma for me.

EDIT: After more digging, I eventually stumbled upon this post: Raspberry Pi Bluetooth 4.0 Connection. This has led me toward the topic of a GATT server, I'll continue pursuing this topic.

EDIT: Alright so my quest for learning goes on. Over the past couple of days I've dived deeper into the hci*, sdptool, and gatttool tools. I've gotten to the point of being able to set the adapter to advertise, "hciconfig hci0 leadv". At this point, I can successfully "see" the adapter, but I cannot actually read anything off of it. I'm not even seeing the friendly name. I'll keep trucking on but as always, any help/suggestions are more than welcome.

EDIT: Relevant Link, solid overview of Bluetooth LE pertaining to iOS. https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth/Introduction.html#//apple_ref/doc/uid/TP40013257-CH1-SW1

LAST EDIT: Hey all, this link covers how to create an iBeacon using a Raspberry Pi + BlueZ... http://www.wadewegner.com/2014/05/create-an-ibeacon-transmitter-with-the-raspberry-pi/

Answer

davidgyoung picture davidgyoung · Sep 27, 2013

With your Bluetooth dongle plugged in, running the following command will tell you the device name and give its state:

$ hciconfig

The output should look something like this:

hci0:    Type: BR/EDR  Bus: USB
     BD Address: 00:01:02:aa:bb:cc  ACL MTU: 1021:8  SCO MTU: 64:1
     DOWN
     RX bytes:1000 acl:0 sco:0 events:47 errors:0
     TX bytes:1072 acl:0 sco:0 commands:47 errors:0

This indicates the device is called hci0 is in a down state. Issue the following command to bring it up:

$ sudo hciconfig hci0 up

Now it should look like:

$ hciconfig
hci0:   Type: BR/EDR  Bus: USB
     BD Address: 00:01:02:aa:bb:cc  ACL MTU: 1021:8  SCO MTU: 64:1
     UP RUNNING
     RX bytes:1000 acl:0 sco:0 events:47 errors:0
     TX bytes:1072 acl:0 sco:0 commands:47 errors:0

Next, execute the following example command to configure the advertising data to be sent.

$ 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

You can change the hex bytes (starting with 1e) to send different byte sequences for your advertisement. One that literally sends the ASCII codes for "HELLO WORLD" would use: 48 45 4c 4c 4f 57 4f 52 4c 44 (EDIT: But you will also have to prefix this message with a valid header, see here.)

Now, use the following command to activate advertising on the dongle, this will start sending "Helo World" packets.

$ sudo hciconfig hci0 leadv 0

EDIT: the above command makes the advertised service connectable. If you don't want to allow connections, change it to $ sudo hciconfig hci0 leadv 3

You can also disable advertising using the following command:

$ sudo hciconfig hci0 noleadv