Running BLE GATT Server Example on Raspbian Stretch


The purpose of this post is to run an example code of Bluetooth Low Energy GATT server from BlueZ source code on Raspberry Pi.

Here are the contents of this post.

Contents
– Assumptions
– Steps
1. Download Example Code
2. Execute Example Code
3. Install Requisite
4. Start Advertising BLE
5. Verify
– Reference

 

Assumptions
Here are some assumptions before start. Please refer this in case it’s not ready.

 

Steps
1. Download Example Code
1-1. Download BlueZ 5.43.

wget www.kernel.org/pub/linux/bluetooth/bluez-5.43.tar.xz

1-2. Extract the file.

tar xvf bluez-5.43.tar.xz

The GATT server example code “example-gatt-server” is in “test” directory.

$ ls bluez-5.43/test/example-gatt-server 
bluez-5.43/test/example-gatt-server

 

2. Install Requisite
2-1. Update package list.

sudo apt-get update

2-2. Install python package manager.

sudo apt-get install python3-pip -y

2-3. Install dbus.

sudo pip3 install pydbus

 

3. Execute Example Code
3-1. Execute BLE GATT server example code.

./bluez-5.43/test/example-gatt-server

3-2. Check the output. If everything is fine, it should be like this:

$ ./bluez/bluez-5.43/test/example-gatt-server 
Registering GATT application...
GetManagedObjects
GATT application registered
Battery Level drained: 98
Battery Level drained: 96

Now, the GATT server is running. Since the example code implements “Fake Battery service that emulates a draining battery”, it outputs “Battery Level drained” message on the console every 5 seconds.

 

4. Start Advertising BLE
The next step is to start advertising in order to be detected by other devices. To make the steps easier, I’ll use hciconfig command. If you want to use dbus interface instead of hciconfig, please refer this post to run BLE advertising example code (i.e. example-advertisement).

4-1. Open other terminal and type below to start advertising.

sudo hciconfig hci0 leadv 0

Type below in case you need to stop the advertisement,

sudo hciconfig hci0 noleadv

 

5. Verify
To verify, you can use smartphone apps. The below image was captured with “BLE Scanner” on iPhone.

 

Reference
– example-gatt-server\test – bluez.git – Bluetooth protocol stack for Linux
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/example-gatt-server

 

 


Update (March 5, 2019):
If you want to make a GATT server based on BlueZ’s example code, please check this post out.


 

 

Disabling Screen Sleep on Raspberry Pi

By default the screen goes to sleep after some minutes of inactivity. It can be disabled by editing configuration file.
Here is the steps.

1. Open /etc/lightdm/lightdm.conf file.

sudo nano /etc/lightdm/lightdm.conf

2. Look for the line starts “xserver-command” under “[Seat:*]” section and modify as below:

xserver-command=X -s 0 -dpms

3. Reboot the system.

sudo reboot

 

 

 

Running BLE Advertising Example Code on Raspbian Stretch


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.

 

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. Install Requisite

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.

 

Troubleshoot

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.

 

 

Enabling Clipboard Sharing and Shared Folder on VirtualBox (Ubuntu Linux Guest)


This post shows how to enable clipboard sharing and file sharing via shared folder between VirtualBox guest OS and host OS. In this post, I use Windows 10 as the host and Ubuntu Linux as the guest.

 

Assumptions
To enable those functions, “VirtualBox Guest Additions” needs to be installed on the guest OS. Please see here (step 4, 22-24) for the installation.

 

1. Enabling Clipboard Sharing
1-1. Launch”Oracle VM VirtualBox”.

1-2. Select your Ubuntu guest and press ctrl + s to open “Settings” window.

1-3. Select “General” and “Advanced” tab.

1-4. Select “Bidirectional” for both “Shared Clipboard” and “Drag’n’Drop”.
(* For some reason, dragging and dropping files to Linux guest is not working in my environment.)

 

 

2. Enabling Shared Folder (Automatic Mounting)
2-1. On “Settings” window, select “Shared Folders”.

2-2. Click on “Adds New Shared Folder” icon.

2-3. Set the path and name for the folder you want to share.

2-4. Check “Auto-mount” and “Make Permanent” options, then press OK.

2-5. Start the guest OS.

2-6. Add your user to the vboxfs group.

sudo usermod -a -G vboxsf $USER

2-7. Reboot the guest OS.

sudo reboot

2-8. After restart, the shared folder should be mounted to “/media/sf_<shared_folder_name>”. In this example, it’s “/media/sf_share”. So, now you can access the shared folder like:

ls /media/sf_share

 

 

Reference
– How To Install Ubuntu Linux On Windows 10 In 24 Steps
https://www.lifewire.com/install-ubuntu-linux-windows-10-steps-2202108

– Ubuntu Documentation: VirtualBox/SharedFolders
https://help.ubuntu.com/community/VirtualBox/SharedFolders

– Automatic mounting — Oracle VM VirtualBox User Manual
https://www.virtualbox.org/manual/ch04.html#sf_mount_auto

 

 

Enabling Bluetooth in VirtualBox


VirtualBox guest OS doesn’t recognize Bluetooth adapter by default. Here is how to enable the built in Bluetooth adapter for Ubuntu guest on Windows host. OS and the version I used are below.

Host: Windows 10
Guest: Ubuntu 16.04

I tested the steps with Microsoft Surface Pro 4 and Dell Precision 3510.

Here is the list of contents of this post.

Contents
– Assumptions
– Steps
1. Disable Bluetooth Adapter on Windows
2. Launch Ubuntu Linux
3. Enable Bluetooth Adapter on Windows
4. Enable Bluetooth Adapter on Ubuntu (Guest)
5. Verify
– Reference

 

Assumptions
There are some prerequisites before starting the steps.

  • Windows PC with Bluetooth Adapter
  • Ubuntu installed as VirtualBox guest OS and running on Windows PC
  • VirtualBox Guest Additions installed on Ubuntu guest OS

 

Steps
1. Disable Bluetooth Adapter on Windows
1-1. Launch “Device Manager” (right-click on Windows icon at left bottom corner, then select “Device Manager”)

1-2. Disable Bluetooth adapter (right-click on your Bluetooth device and select “Disable device”)

 

2. Launch Ubuntu Linux
2-1. Launch “Oracle VM VirtualBox”.

2-2. Select your Ubuntu guest and click on “Start” icon on “Oracle VM VirtualBox Manager” window.

 

3. Enable Bluetooth Adapter on Windows
3-1. Go back to Device Manager, enable the Bluetooth adapter. (right-click on the Bluetooth device and select “Enable device”)

 

4. Enable Bluetooth Adapter on Ubuntu (Guest)
4-1. In “Oracle VM VirtualBox” window, select “Devices” > “USB” from the top menu bar.

4-2. Then, select Bluetooth adapter to enable.

 

5. Verify
5-1. Check if Bluetooth is enabled on Ubuntu by typing the command below.

hciconfig -a

If everything is fine, you should be able to see something like below. Be sure it says “UP RUNNING” (Line 4 in the example below).

$ hciconfig -a
hci0:	Type: BR/EDR  Bus: USB
	BD Address: xx:xx:xx:xx:xx:xx  ACL MTU: 1021:7  SCO MTU: 240:3
	UP RUNNING 
	RX bytes:1039 acl:0 sco:0 events:54 errors:0
	TX bytes:2435 acl:0 sco:0 commands:54 errors:0
	Features: 0xff 0xfe 0x8f 0xfe 0xdb 0xff 0x7b 0x87
	Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
	Link policy: RSWITCH HOLD SNIFF 
	Link mode: SLAVE ACCEPT 
	Name: 'vm'
	Class: 0x0c0000
	Service Classes: Rendering, Capturing
	Device Class: Miscellaneous, 
	HCI Version: 4.0 (0x6)  Revision: 0x8300
	LMP Version: 4.0 (0x6)  Subversion: 0x9172
	Manufacturer: Marvell Technology Group Ltd. (72)

 

 

Reference
– How To Install Ubuntu Linux On Windows 10 In 24 Steps
https://www.lifewire.com/install-ubuntu-linux-windows-10-steps-2202108