Getting BLE to behave on the Pi

Finding the Peripheral

Simply finding and getting the BLE connection isn't always as simple as it first appears. In this section I'll look at how to verify that you are actually advertising something and what tools are available to work out what is really going on. Here is where things potentially get tricky and, judging from forums across the web, where many have come unstuck.

BLE browsing programs such as LightBlue for iOS [7] or Mac [8] allow you to search for nearby advertising devices, interrogate their characteristics and write to them. Prototyping and trying out ideas becomes easier because you can concentrate on the Raspberry Pi side of things and get that working before moving on to the app side.

Other browsing programs are available for other platforms, so look for something that allows you to browse through the BLE devices nearby and to interrogate their services and characteristics. In LightBlue, if you click scan, a few seconds later a list of the devices near you pop up, hopefully including the one you are running on your Rasp Pi.

It's instructive to setup BLE advertising and scanning on a phone and desktop so you know what to expect. Assuming you have a desktop handy, try this: Install LightBlue on both your desktop and phone. Open LightBlue on your phone and set up a fake peripheral (Figure 1). With the peripheral advertising, open LightBlue on your desktop and scan. The peripheral will pop up, and you will be able to drill down into its services and characteristics (Figure 2). Once this is running you'll see this extra peripheral appear in the list, allowing you to see what's being advertised and therefore what any other devices scanning for this should expect.

Figure 1: Install LightBlue both to scan for your Raspberry Pi peripherals and to simulate peripherals on the phone.
Figure 2: The LightBlue desktop app allows you to set up BLE scenarios and learn what to expect on the central and peripheral sides.

This trick is very useful. A lot of the process of learning about BLE, is discovering what each side of the BLE connection should expect, and having a tool to scan for arbitrary devices is hugely useful.

Where Now?

BLE is a nice way to connect devices for automating your home, without the need to connect to WiFi – or to connect devices where WiFi or Internet connection might not be available. However, getting BLE running on a Raspberry Pi isn't always simple. Knowing the main concepts and debugging tools will save you hours (see the "BLE Concepts" and "Useful Tools" boxouts).

BLE Concepts

Although the following list isn't exhaustive, you should know the following terms before navigating the BLE options and to understand responses on forums.

  • Central and Peripheral. The central is the device that scans for the peripherals, which should be advertising. For example, the central might be your phone and the peripheral your Fitbit wristband.
  • Scanning. The central scans for peripherals but might only scan for peripherals advertising a certain service. You need to make sure the service being advertised and the service being scanned for match up.
  • Services. Services are the way devices describe themselves and each service is a collection of characteristics, which are roughly analogous to properties of a class or fields in a database table.

Useful Tools

What happens when BLE communication doesn't work? Where are the logs? What should you try?

Know your debugging tools. The first thing to check is compatibility of the BLE dongle. You'll either get errors about not being able to write to the device or just get nothing back from if it's not BLE compatible. I encourage you to research the device before buying.

On the Raspberry Pi you can run the hcidump tool installed earlier. This monitors the BLE status and reports on all advertising and each connection made. To use it, run

hcidump -X

in one terminal on the Raspberry Pi. In another terminal, start up your BLE advertising script, such as

sudo node ble-peripheral.js

which dumps the activity.

This tool is useful when a central isn't connecting to the peripheral and you can't work out at what point the failure is occurring. The output of hcidump shows you how far in the advertising, interrogating, and pairing process you got and will help find out at what point things break. In such cases, this log might report what point you've reached.

The rfkill command is worth checking out because sometimes the BLE capability will be disabled on the Raspberry Pi, blocking all activity. To use this, run rfkill, which shows which are blocked:

# rfkill list
0: hci0: Bluetooth
     Soft blocked: no
     Hard blocked: no
1: phy0: Wireless LAN
     Soft blocked: no
     Hard blocked: no

Although specifically for bleno, adding the following options to the command line provides more output, which is also helpful. Remember you're looking for the BLE to start advertising and, if it does, for a connection to be attempted:

sudo DEBUG=hci-blue,l2cap-ble      node test2.js

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content