Update (Feb 26, 2018):
If your BlueZ version is 5.48 or higher, you can skip Step 1 since the Advertising Manager is marked as stable In BlueZ 5.48. (To update BlueZ, please see this post. )
The purpose of this post is to run an example code of Bluetooth Low Energy Advertisement from BlueZ source code (i.e. “example-advertisement“) on Raspberry Pi running Raspbian Stretch.
Here are the contents of this post.
Contents
– Assumptions
– Steps
1. Enable Experimental Flag for BlueZ
2. Download Example Code
3. Execute Example Code
4. Verify
– Troubleshoot
– Summary
Assumptions
Here are some assumptions before start. Please refer this in case it’s not ready.
- Raspbian Stretch (2017-09-07) running on Raspberry Pi 3 B+ or Raspberry Pi Zero W.
- Internet access
Steps
1. Enable Experimental Flag for BlueZ
The example code uses LEAdvertisingManager1 interface which is still experimental in BlueZ 5.43 (pre-installed version in Raspbian Stretch (2017-09-07)). So, the experimental flag for BlueZ needs to be enabled.
1-1. Open the configuration file for bluetooth service.
sudo nano /lib/systemd/system/bluetooth.service
1-2. Find a line starting with “ExecStart” and add “–experimental” at the end of the line (Line 9 in the example below). Then save and close the file.
[Unit] Description=Bluetooth service Documentation=man:bluetoothd(8) ConditionPathIsDirectory=/sys/class/bluetooth [Service] Type=dbus BusName=org.bluez ExecStart=/usr/libexec/bluetooth/bluetoothd --experimental NotifyAccess=main #WatchdogSec=10 #Restart=on-failure CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE LimitNPROC=1 ProtectHome=true ProtectSystem=full [Install] WantedBy=bluetooth.target Alias=dbus-org.bluez.service
1-3. Reboot the system to enable the change.
sudo reboot
2. Download Example Code
2-1. Download BlueZ 5.43.
wget www.kernel.org/pub/linux/bluetooth/bluez-5.43.tar.xz
2-2. Extract the file.
tar xvf bluez-5.43.tar.xz
The advertising example code “example-advertisement” is in “test” directory.
$ ls -l bluez-5.43/test/example-advertisement -rwxr-xr-x 1 pi pi 5774 Sep 26 2016 bluez-5.43/test/example-advertisement
3-1. Update package list
sudo apt-get update
3-2. Install dbus and gobject
sudo apt-get install python-dbus python-gobject -y
4. Execute Example Code
4-1. Execute BLE advertising example code.
python ./bluez-5.43/test/example-advertisement
4-2. Check the output. If everything is fine, it should be like this:
$ ./bluez-5.43/test/example-advertisement GetAll returning props Advertisement registered
In case you get the error below, see Troubleshoot section.
$ python ./bluez-5.43/test/example-advertisement LEAdvertisingManager1 interface not found
5. Verify
You can use smartphone apps to verify if it’s really advertising. The image below is a screen capture of “LightBlue” app running on iPhone.
“stretch0907” is my Raspberry Pi’s host name. Advertisement data can be configured by modifying the example code.
LEAdvertisingManager1 interface not found
The error above occurs most likely because the experimental flag for bluez is not enabled. You can check it using “ps” command by typing:
ps aux|grep blue
If it’s enabled, you can see the flag (i.e. “–experimental”) after the process path like this.
root 498 0.3 0.4 7268 3856 ? Ss 13:43 0:00 /usr/lib/bluetooth/bluetoothd --experimental
In case the flag is not there (like below), please go back to step 1 and enable the flag.
root 540 0.0 0.3 7268 3704 ? Ss 11:52 0:00 /usr/lib/bluetooth/bluetoothd
Summary
Now, Raspberry Pi is acting as BLE advertiser by running the BlueZ example code. Also, it can be easily a BLE beacon such as Apple’s iBeacon by modifying the code. Please refer this post to create iBeacon with BlueZ.
I am unable to change the Raspberry Pi Local Name When using my Raspberry pi act as Peripheral
Hi Akhzar,
Can you tell me how you are trying to change the local name and what do you mean by “unable to change”? Are you getting any error?
Is it possible to disable the Classic Mode and advertise only for LE?
this method occured ERROR:dbus.proxies:Introspect error in Buster