How to Setup a Raspberry Pi iBeacon

In this tutorial, we will show you how you can set up your Raspberry Pi to operate as an iBeacon.

Raspberry Pi iBeacon

To make this tutorial work you will need a Bluetooth adapter that supports being put into advertising mode.

iBeacon is a protocol that was developed by Apple and introduced back in 2013 for broadcasting data over the Bluetooth protocol. It also provides a way for devices to calculate their distance from the beacon.

There is a variety of different reasons why you might want an iBeacon setup. One example is to advertise your store over the iBeacon protocol. Another is to keep track of your distance from certain items such as their car keys and much more.

There are many projects for the Raspberry Pi where this protocol will come in handy. If you have an example that you would love to share then be sure to drop us a comment at the bottom of this page.

In this tutorial, we will show you how to broadcast the data from your Raspberry Pi as well as explaining the packet format that the iBeacon protocol expects.

Equipment List

Below are all the pieces of equipment that you will need for this Raspberry Pi ibeacon tutorial.

Recommended

Optional

Setting up the iBeacon

1. Before we go ahead and reconfigure our Raspberry Pi’s Bluetooth to act as an iBeacon, we should first update all our currently installed packages.

We can update all the software running on our Raspberry Pi by running the following command.

sudo apt update
sudo apt upgrade

2. When you have finished updating your Raspberry Pi’s software let’s power up our Raspberry Pi’s Bluetooth device. This Bluetooth device should be sitting on the hci0 interface.

For us to power up our Bluetooth device we will need to run the following command on the Pi.

sudo hciconfig hci0 up

3. Now that we have turned on the Bluetooth module we need to change its operating mode to “Low Energy Advertising“, and set that mode so that it is in “Nonconnectable undirected advertising mode“.

This process is pretty simple and involves using the hciconfig tool again. Enter the following command into your Raspberry Pi to change the mode of the Bluetooth device.

sudo hciconfig hci0 leadv 3

4. Once we have changed our Raspberry Pi’s Bluetooth mode into “Low Energy Advertising” we can now proceed to set the Pi’s Bluetooth to broadcast the iBeacon payload data.

To test that the iBeacon is working as it should on the Raspberry Pi proceed to enter the following command into your terminal.

We will explain the packet data that we are broadcasting later in this guide, but for now, we will make sure that the broadcast is working.

sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 06 1A FF 4C 00 02 15 C7 C1 A1 BF BB 00 4C AD 87 04 9F 2D 29 17 DE D2 00 00 00 00 C8 00

5. Now on your device install a “beacon scanner” application. On Android, we utilized the Beacon Scanner app by Nicolas Bridoux.

Using this application, we can run a scan to find the iBeacon that we just set up. Once you see your Raspberry Pi iBeacon, you can move onto the next step.

iBeacon Scanner Screenshot

Now that we have verified that we have configured our iBeacon on the Raspberry Pi correctly let’s go ahead and take a more in-depth look into the payload data.

Further Tinkering

1. The vast majority of the payload data should not be modified as Apple’s iBeacon protocol requires these. The two blocks of data that you can change is the UUID the Major, Minor bytes and the signal power.

Our table below will show you what each bit we send through to our Bluetooth device is.

Byte ValueDescription
0x08Defines the OGF as the Bluetooth Command Group 0x08
0x0008Defines the OCF so that we set the bluetooth advertising data
1FDefines the data length of our entire payload
02Data length of the next section, 2 bytes
01Data Type, defines that this section is flag data
06Defines the flag needed for Low Energy Advertising mode
1AData length of the final section of the payload, 26 bytes
FFData Type, defines that this section is manufacturer specific data
4CDefines the manufacturer ID, in this case apple (0x4c00)
00Second part of the manufacturer ID
02Defines the sub type, which in our case is an iBeacon
16Defines that the following data is “service data” for eddystone
15Data length of the subtype payload, which should be 21 bytes
C7Proximity UUID, 1st Byte
C1Proximity UUID, 2nd Byte
A1Proximity UUID, 3rd Byte
BFProximity UUID, 4th Byte
BBProximity UUID, 5th Byte
00Proximity UUID, 6th Byte
4CProximity UUID, 7th Byte
ADProximity UUID, 8th Byte
87Proximity UUID, 9th Byte
04Proximity UUID, 10th Byte
9FProximity UUID, 11th Byte
2DProximity UUID, 12th Byte
29Proximity UUID, 13th Byte
17Proximity UUID, 14th Byte
DEProximity UUID, 15th Byte
D2Proximity UUID, 16th Byte
00Major 1st byte – Distinguishes devices using same UUID
00Major 2nd byte – Distinguishes devices using same UUID
00Minor 1st byte – Distinguishes devices using same UUID
00Major 2nd byte – Distinguishes devices using same UUID
C8Signal Power – Used to optimize distance calculations.

2. We can now show you how to utilize a new UUID for your Pi iBeacon, to begin we will need to create a new one.

To quickly generate a new UUID we will be utilizing a website called Online UUID Generator.

There are much better ways of generating a UUID, but for this tutorial, we will aim for the most straightforward process.

3. Once you have a UUID, remove the hyphens from it, and add a space after every two letters. Below we have included an example of what we mean.

This modified UUID will have the same amount of bytes to replace the one we utilized in the command earlier in the tutorial.

From:

98374d0a-fa8f-43ab-968b-88eaf83c6e4c

To:

98 37 4d 0a fa 8f 43 ab 96 8b 88 ea f8 3c 6e 4c

4. Now to replace the UUID in the command, look for the “15” part of the command that designates the payload length.

From here you need to swap out the next 16 bytes, we have included an example below. This example shows you what a modified version of the command should look like, including bolding the section that was replaced.

sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 06 1A FF 4C 00 02 15 98 37 4D 0A FA 8F 43 AB 96 8B 88 EA F8 3C 6E 4C 00 00 00 00 C8 00

Conclusion

Hopefully, at this point, you now have your Raspberry Pi iBeacon up and running. You should also understand what sort of data your Bluetooth device will be sending out. As well as knowing how to replace the UUID for your very own generated one.

If you have any feedback regarding this tutorial, then please don’t hesitate to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *