Simple Bluetooth Device Detection

Want to learn how to trigger events when a known Bluetooth device is nearby using the BBGW? Here's a python example to get you started!

Categories: Beginner

Introduction

In this short tutorial, you will learn how to setup the BeagleBone Green Wireless (BBGW) for Bluetooth use and create a simple Python script that will trigger an LED to light up when a known Bluetooth-capable device (such as your phone or fitness band) comes within sensing range.

This demo should be used as a simple starting place for creating systems that require the ability to track or sense known users by their handheld Bluetooth devices.

Possible applications include:

  • Work Tracking (How many hours do you spend at your desk?)
  • Triggering Smart Home Hardware (e.g. Turn off lights when you leave your house.)
  • Simple Device-aware Games (Reward people for returning to your business/hackerspace with points similar to FourSquare or Jiepang)

Requirements

  • Some Python programming experience.
  • Some Linux command line experience.

Device Setup

Make sure you are running the
latest firmware image for the device. B
e sure to use only the images named
bone-debian-8.4-seeed-iot-armhf-2016-MM-DD-4gb.img.xz
from the
seeed-iot
directories. As the platform matures the most up-to-date image will be moved to the
official wiki page bu
t for now use the latest testing build.

For more information about updating the BeagleBone Green firmware or flashing the eMMC to free up a microSD card, check out the official page on the matter
here.

In order to proceed, you will require net access so be sure to read section 3.3 of the BBGW System Reference Manual available on the BBGW’s internal memory concerning WiFi setup. (Hint: Look for the PDF document located at [BBGW Device Root]\Docs\Hardware)

Prerequisites

Before starting any system setup, it is always a good idea to have the latest stable software installed. Access a command line interface on the device either over ssh or via a terminal tab in the BBGW’s Cloud9 IDE and run:

sudo apt-get update && sudo apt-get upgrade

Now it is time to install the Bluetooth library prerequisites required by
pybluez.

sudo apt-get install build-essential python-dev libbluetooth-dev

Finally use the Python package manager to install our Bluetooth framework.

sudo pip install pybluez

With all the libraries and dependencies installed it is now time to finally start up our Bluetooth chipset on the BBGW. This is a little built-in script for wireless-capable BeagleBone models that discovers Bluetooth hardware and sets it up for use.

sudo bb-wl18xx-bluetooth

Hardware Setup

To keep this demo as simple as possible, I’ve elected to just have the script toggle a single LED connected to pin P8_7. Since the current draw of most LEDs is pretty small, we can directly drive it from a GPIO pin on the BeagleBone without issue. If you want to be completely proper, add a 200Ω or so resistor in series with the LED to limit the current draw.

Head down to the
schematic section
for an image of how to insert the LED.

Time to Code

In general, what we want is a simple loop that requests information from a known Bluetooth device ID and, depending on what it hears back, changes the state of a digital output pin accordingly. If the script receives the requested information, we set a pin connected to an LED to HIGH, otherwise set it LOW since the device must now be out of range.

The neat thing about polling for public Bluetooth information,
we no longer have to worry about pairing with the desired device!
This greatly reduces the complexity of the system down to something that can be implemented in just a few lines of code.

To make the script a bit easier to use, I’ve added some helpful user prompts and a device discovery stage to make identifying your Bluetooth device easier.

Just open up your editor of choice and paste the
script down in the Code section
into a new file called
detect_bluetooth.py
and save. To see how it works, either run it in cloud9 or just use:

sudo python detect_bluetooth.py

After following the onscreen instructions, the script will then proceed to ping your device for information every few seconds. Try walking away from the BeagleBone and walking back to see what the detection range is for your specific device. The LED, despite being tradition for intro tutorials, makes it pretty easy to tell what the current detection state is from a distance.

Here’s what a typical demo session looks like in terms of debug output:

A typical usecase for this detection method with the user traveling in and out of the detection range.

For my phone, I had to get around 12 feet away before the BeagleBone lost connection and when coming back into range, there were a few false negatives.

Where to go from here

The script above is just meant as a quick working example of a detection method that doesn’t require device pairing. I’ll leave the reader with some ideas on how it can be expanded for use in a more serious system.

  • Allow for tracking multiple IDs
  • Add a sampling threshold to filter out the occasional false negative. (e.g. only change output state upon 5 consecutive samples)
  • Use the more
    advanced features of
    the pybluez library to get signal strength (RSSI) information to measure device distance.
  • Save selected Bluetooth IDs to disk (Using
    Pickle) f
    or loading upon subsequent tests (Good for making this into a daemonized service)

Comments are not currently available for this post.