In this Raspberry Pi iBeacon tutorial, we will be showing you how you can set up your Pi to operate as an 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.
Below are all the pieces of equipment that you will need for this Raspberry Pi ibeacon tutorial.
Raspberry Pi 2 or 3
Micro SD Card
Bluetooth Adapter that supports advertising mode
Raspberry Pi Case
Ethernet Network Connection or Wifi dongle (The Pi 3 has WiFi inbuilt)
Setting up our Eddystone beacon
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-get update sudo apt-get 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 hci0interface.
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.
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.
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.
|0x08||Defines the OGF as the Bluetooth Command Group 0x08|
|0x0008||Defines the OCF so that we set the bluetooth advertising data|
|1F||Defines the data length of our entire payload|
|02||Data length of the next section, 2 bytes|
|01||Data Type, defines that this section is flag data|
|06||Defines the flag needed for Low Energy Advertising mode|
|1A||Data length of the final section of the payload, 26 bytes|
|FF||Data Type, defines that this section is manufacturer specific data|
|4C||Defines the manufacturer ID, in this case apple (0x4c00)|
|00||Second part of the manufacturer ID|
|02||Defines the sub type, which in our case is an iBeacon|
|16||Defines that the following data is “service data” for eddystone|
|15||Data length of the subtype payload, which should be 21 bytes|
|C7||Proximity UUID, 1st Byte|
|C1||Proximity UUID, 2nd Byte|
|A1||Proximity UUID, 3rd Byte|
|BF||Proximity UUID, 4th Byte|
|BB||Proximity UUID, 5th Byte|
|00||Proximity UUID, 6th Byte|
|4C||Proximity UUID, 7th Byte|
|AD||Proximity UUID, 8th Byte|
|87||Proximity UUID, 9th Byte|
|04||Proximity UUID, 10th Byte|
|9F||Proximity UUID, 11th Byte|
|2D||Proximity UUID, 12th Byte|
|29||Proximity UUID, 13th Byte|
|17||Proximity UUID, 14th Byte|
|DE||Proximity UUID, 15th Byte|
|D2||Proximity UUID, 16th Byte|
|00||Major 1st byte – Distinguishes devices using same UUID|
|00||Major 2nd byte – Distinguishes devices using same UUID|
|00||Minor 1st byte – Distinguishes devices using same UUID|
|00||Major 2nd byte – Distinguishes devices using same UUID|
|C8||Signal 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.
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
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.