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

 

 

Creating Battery Level Indicator in Qt/QML using Icon Font


The purpose of this post is to create a battery charge level indicator in Qt/QML. For the buttery icon we’ll use Font Awesome, which is a free icon font, so that we don’t need to design the icon from scratch.

Here is the list of contents of this post.

Contents
– Steps
1. Download icon font and add to your project
2. Create “BatteryIcon” component
3. Use “BatteryIcon” component
– Summary
– Reference

 

Steps
1. Download icon font and add to your project

1-1. Download Font Awesome from here and extract the zip file.

1-2. Copy .ttf file to your Qt Creator project folder.

1-3. Then, right-click on “qml.qrc” and select “Add Existing Files…” in the left side bar.

1-4. Select .ttf file to add.

 

2. Create “BatteryIcon” component

2-1. In your Qt Creator project, click on “Resources” in the left side bar.

2-2. Then, right-click on “qml.qrc” and select “Add New…”

2-3. Select “Qt” and “QML File (Qt Quick2)”.

2-4. Input “BatteryIcon” as the file name and click on “Finish”.

2-5. Copy below lines to the file.

— BatteryIcon.qml —

import QtQuick 2.0

Item {
    property string iconColor
    property int iconSize: 32 // in pixcel
    property int level // 0-4 levels

    function getUnicode()
    {
        if (level == 0)
            return "\uf244" // fa-battery-empty
        else if (level == 1)
            return "\uf243" // fa-battery-quarter
        else if (level == 2)
            return "\uf242" // fa-battery-half
        else if (level == 3)
            return "\uf241" // fa-battery-three-quarters
        else if (level == 4)
            return "\uf240" // fa-battery-full
        else
            return "\uf244" // fa-battery-empty
    }

    Text {
        font.family: fontAwesome.name
        color: iconColor
        font.pixelSize: iconSize
        text: getUnicode()
    }

    FontLoader { id: fontAwesome; source: "qrc:/fontawesome-webfont.ttf" }
}

2-6. Click on “qml.qrc” and “/” in the left side bar.

2-7. Right-click on the .ttf file and select “Copy URL …”.

2-8. Update URL on Line 31 with the URL copied at Step 2-7.

 

3. Use “BatteryIcon” component

3-1. Use BatteryIcon component like below example. It has three properties, icon color, icon size (in pixel) and battery level from 0 to 4 (0:empty, 1:quarter, 2:half, 3:three quarters and 4:full).

BatteryIcon{
    iconSize: 48
    iconColor: "blue"
    level: 2
}

If everything is fine, it will show like below battery icon.

 

Summary

I needed some icons for a project but was hesitant to create because I thought it’s gonna take some time. Then I found this article. It was really helpful for me at the situation, and Font Awesome is, yes, awesome! Using icon fonts for QML is makes sense. It requires less work, and it works perfectly.

 

Reference

– How to make a quick custom Qt/QML checkbox using icon fonts
https://medium.com/@eduard.metzger/how-to-make-a-quick-custom-qml-checkbox-using-icon-fonts-b2ffbd651144

– Font Awesome — The iconic font and CSS toolkit
http://fontawesome.io/icons/

 

 

Creating Bluetooth Beacon with Raspberry Pi by using Bluetooth Developer Studio


This post shows how to make a Raspberry Pi a Bluetooth Beacon by using Bluetooth Developer Studio and its Beacon plugin. Bluetooth Developer Studio is a free application development tool from Bluetooth SIG and with Beacon plugin, it generates a script file which makes a Raspberry Pi a Bluetooth beacon.

The steps below basically follow a tutorial (Turning your Raspberry Pi into a Beacon using Bluetooth Developer Studio v1.0) which is included in Beacon plugin for Bluetooth Developer Studio but are simplified for my learning purpose. Please note, this is not very practical but rather kind of “getting started with” Bluetooth Developer Studio and Bluetooth Beacon.

Here are the contents of this post.

 

Prerequisites

  • Raspberry Pi
    Assuming your Raspberry Pi is up and running. If it’s not already, please set it up first. Raspberry Pi 3 B+ or Raspberry Pi Zero W is recommended since they have a built-in Bluetooth chip. If you want to use other model which doesn’t have Bluetooth chip, you need a Bluetooth Low Energy USB dongle such as this.
  • Windows PC (Windows 7 or higher)
    Bluetooth Developer Studio works on only Windows 7 and higher.

 

Steps
1. Install Bluetooth Developer Studio and Beacon plugin
1-1. Download Bluetooth Developer Studio from here. As of 9/2/2017, the latest version is 1.1. It requires registration to download.

1-2. Install Bluetooth Developer Studio by following the Setup wizard.

1-3. Download Beacon plugin from here.

1-4. Unzip the downloaded file and go to \beacon-1\tutorial\RaspberryPi\starter-files.

1-5. Then, copy “beacon-plugin” folder into Bluetooth Developer Studio’s plugins folder.
(e.g. “C:\Program Files (x86)\Bluetooth SIG\Bluetooth Developer Studio\Plugins”)

1-6. Go into the newly copied folder (i.e. “beacon-plugin”) and open “plugin.js” with a text editor.

1-7. Search the lines below and change “true” to “false” on the if statement.

// set the true / false value on the following line to true to read in the data from the service and false to read in from GAP settings
if (false)

* Note: Change the permissions to modify the file if needed.

 

2. Create Beacon Profile
2-1. Launch Bluetooth Developer Studio.

2-2. Create a new project from “FILE” > “NEW”

2-3. Enter “Beacon” in the “NAME” field of “NEW PROJECT” window.

2-4. Enter “org.bds.beacon” in the “NAMESPACE” field, then click “OK” button.

2-5. Click on “New Profile”, change the name to “Beacon”, then click “SAVE”.

2-6. Click on the “Custom Service” button.

2-7. Click on “Beacon new service” and change it to “Beacon”, then, “SAVE”.

2-8. Click on “GAP” button

2-9. Enter beacon data as below and click on “DONE”.
TX Power : “-127″
Manufacturer Specific Data” : “ffeeddccbbaa99887766554433221100facefeed”

2-10. From the menu bar, select “FILE” > “SAVE” to save the profile.

 

3. Generate Beacon script

3-1. From the top menu, select “TOOLS” > “GENERATE CODE”.

3-2. Select “Beacon” plugin and click on “GENERATE”.

 

3-3. After “Code generated successfully”, check on “Open output location when finished”, then click on “FINISH”.

The folder which contains the generated script file will be opened automatically.

 

4. Execute Beacon script on Raspberry Pi

4-1. Copy the generated “Beacon.sh” script to Raspberry Pi’s home directory. (e.g. via USB drive, scp)

4-2. Open terminal on Raspberry Pi and add execute access to the script.

sudo chmod +x ~/Beacon.sh

4-3. Run the script.

. ~/Beacon.sh

If it worked you should be able to see output such as:

$ . ~/Beacon.sh
hci0:	Type: BR/EDR  Bus: UART
	BD Address: xx:xx:xx:xx:xx:xx  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:7550 acl:0 sco:0 events:716 errors:0
	TX bytes:22202 acl:0 sco:0 commands:716 errors:0

< HCI Command: ogf 0x08, ocf 0x0008, plen 32
  1F 02 01 1A 1B FF 18 01 BE AC FF EE DD CC BB AA 99 88 77 66 
  55 44 33 22 11 00 FA CE FE ED 81 01 
> HCI Event: 0x0e plen 4
  01 08 20 00 

If you get an error like,

-bash: ./Beacon.sh: /bin/bash^M: bad interpreter: No such file or directory

and/or,

Beacon.sh: line 2: $'\r': command not found

that is because the script file has Windows style line endings and bash doesn’t understand it. You can fix the error by changing the line endings from Windows style to Unix style by the following command.

sed -i 's/\r$//' Beacon.sh

 

5. Verify
You can check if it’s really broadcasting the beacon data by using a smartphone app such as “BLE Scanner” on Android.