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.
Make sure you are running the latest firmware image for the device. Be 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 but for now use the latest testing build.
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. This is a little built-in script for wireless-capable BeagleBone models that discovers Bluetooth hardware and sets it up for use.
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.
Though if you wanted to be completely proper, add a 200Ω or so resistor in series with the LED to limit the current draw.
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. 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 above 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 every few seconds. Try walking away from the BeagleBone and walking back to see what the detection range is for your specific device.
Here's what a typical demo session looks like in terms of debug output:
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.
The script above is just meant as a toy example. I'll leave the reader with some ideas on how it can be expanded for use in a more serious system.