Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Chris Earley
Published © CC BY-SA

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!

BeginnerProtip1 hour38,989
Simple Bluetooth Device Detection

Things used in this project

Hardware components

SeeedStudio BeagleBone Green Wireless
BeagleBoard.org SeeedStudio BeagleBone Green Wireless
×1
LED (generic)
LED (generic)
×1
A bluetooth device that can be made discoverable
×1

Story

Read more

Schematics

LED Placement

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.

Code

Simple Python Bluetooth Detection Script

Python
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.
import bluetooth, time
import Adafruit_BBIO.GPIO as GPIO

search_time = 10
led_pin = "P8_7"
# You can hardcode the desired device ID here as a string to skip the discovery stage
addr = None

print("Welcome to the Bluetooth Detection Demo! \nMake sure your desired Bluetooth-capable device is turned on and discoverable.")

if addr == None:
    try:
        input("When you are ready to begin, press the Enter key to continue...")
    except SyntaxError:
        pass

    print("Searching for devices...")

    nearby_devices = bluetooth.discover_devices(duration=search_time, flush_cache=True, lookup_names=True)

    if len(nearby_devices) > 0:
        print("Found %d devices!" % len(nearby_devices))
    else:
        print("No devices found! Please check your Bluetooth device and restart the demo!")
        exit(0)

    i = 0 # Just an incrementer for labeling the list entries
    # Print out a list of all the discovered Bluetooth Devices
    for addr, name in nearby_devices:
        print("%s. %s - %s" % (i, addr, name))
        i =+ 1

    device_num = input("Please specify the number of the device you want to track: ")

    # extract out the useful info on the desired device for use later
    addr, name = nearby_devices[device_num][0], nearby_devices[device_num][1]

print("The script will now scan for the device %s." % (addr))
print("Feel free to move near and far away from the BeagleBone to see the state change on the LED.\nUse Ctrl+c to exit...")

GPIO.setup(led_pin, GPIO.OUT)
while True:
    # Try to gather information from the desired device.
    # We're using two different metrics (readable name and data services)
    # to reduce false negatives.
    state = bluetooth.lookup_name(addr, timeout=20)
    services = bluetooth.find_service(address=addr)
    # Flip the LED pin on or off depending on whether the device is nearby
    if state == None and services == []:
        print("No device detected in range...")
        GPIO.output(led_pin, GPIO.LOW)
    else:
        print("Device detected!")
        GPIO.output(led_pin, GPIO.HIGH)
    # Arbitrary wait time
    time.sleep(1)

Credits

Chris Earley

Chris Earley

2 projects • 17 followers
I'm just a life-long learner with questions to ask and an itch to make.
Thanks to Seeed Studios.

Comments