TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

What is it?


Use a USB Bluetooth dongle to set up Bluetooth capabilities on the Raspberry Pi, scan for devices, send files or messages and check who is in the building.  Not all dongles work and this is the first hurdle, check that you have a compatible one at this site. I tried the setup and it originally didn't work, may be that my power supply was not high enough to power the keyboard, mouse, and the Bluetooth.  In the end I flashed a fresh copy of the OS 'Wheezy' onto a new SD card and also used a remote desktop connection to the Pi.  Success!

1. Getting Set up


Load the LX Terminal:
sudo apt-get update
sudo apt-get upgrade

Then install the modules for the hardware and Python Libraries:
sudo apt-get install bluez
sudo apt-get install python-bluez

At this point I would insert your Bluetooth Dongle and restart the Raspberry Pi with sudo reboot.  When reloaded you can then check that the Dongle has been recognised.  You can use lsusb  to see if it is plugged into the USB and hcitool dev to identify the dongle and return its address.

2. Finding Devices


Turn on the Bluetooth on another device, or phone, laptop, iPad etc and ensure that it is in discovery mode.

This simple Python code will look for available Bluetooth devices and return the address of the device.  This can be used later to send communication and create some useful hacks. 

import bluetooth
print("Searching for device...")
nearby_devices = bluetooth.discover_devices(lookup_names = True)
print("found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
    print("  %s - %s" % (addr, name))

Bluetooth Coding



3. Discover a List of Bluetooth Services


Once you have found the MAC address of the device you can then scan the device to see what Bluetooth services are available, such as Audio Gateway, SMS access or Phone book access.  This is a temperamental process and I found different devices had various properties and would not work in sleep mode or needed to connect first.  The program does return a list of protocols, the name of the service, the service class and most importantly the port number that the service is available on.  
#!/usr/bin/env python
import bluetooth
from bluetooth import *

device_address = "68:58:78:3A:BB:7F" # enter address of device

#finds a list of MAC addresses for devices
devices = bluetooth.discover_devices()
for mac in devices:
    print mac

#find services on the phone
services = find_service(address=device_address)
for i in services:
    print i, '\n'
  • PyBluez Website
  • GITHUB link
Copyright 2020 TeCoEd @dan_aldred