Abstract: In this short howto we will install BluePy (an API to allow access to Bluetooth Low Energy devices from Python) on Raspberry Pi. We will use it in some python scripts which use BLE (Bluetooth Low Energy) devices.
1.) Setup a Raspberry Pi as explained here
2.) Login as pi user
3.) Install the required phyton software via
sudo apt-get install python-pip libglib2.0-dev
To find out which phyton software you are running you can use “python –version”
4.) We will now install BluePy directly from the Phyton Package Index via PIP:
sudo pip install bluepy
This might take some time but once installed we will find the needed files in:
Working dir is /tmp/pip-build-b_rUBd/bluepy
Installing blescan script to /usr/local/bin
Installing sensortag script to /usr/local/bin
The main package is installed in /usr/local/lib/pythonVERSION/dist-packages/. In the example here (default Debian Jessie) its: /usr/local/lib/python2.7/dist-packages/bluepy
5.) To check BluePy now, we need the MAC from our BLE device. If we know that (sometimes its written on the package from the BLE device or on the device itself). If not we can use lescan to scan for BLE devices. This can be done via:
sudo hcitool lescan
It will output somehing like:
7C:2F:80:AD:08:66 Gigaset G-tag
Which shows currently my Gigaset G-Tag and my Adidas Speed Cell. The numbers in front of the devices is the MAC address which we need. Once the device came up we can press ctrl + c to stop the scan, otherwiese it will run forever (keep noted that lescan is not part of BluePy).
6.) Once we have the MAC we can switch to “/usr/local/lib/python2.7/dist-packages/bluepy” and run:
python btle.py 7C:2F:80:AD:08:6
if working it will show us some useful info´s, here is a short abbreviation from the output (might take some time to show something):
Service <uuid=Battery Service handleStart=25 handleEnd=28> :
Characteristic <Battery Level>, hnd=0x1a, supports NOTIFY READ
Service <uuid=Device Information handleStart=16 handleEnd=24> :
Characteristic <Manufacturer Name String>, hnd=0x11, supports READ
Characteristic <Model Number String>, hnd=0x13, supports READ
Characteristic <Firmware Revision String>, hnd=0x15, supports READ
Characteristic <Software Revision String>, hnd=0x17, supports READ
If yours look similar then you can be sure its working. Keep noted that if you play with different BLE devices, the options you will see might be different.
7.) To run another test you could enter
which will then output something like
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And then you can enter:
from bluepy.btle import UUID, Peripheral
Which should work without error
Keep noted that LE scan mußt run as root as mentioned here
P.S. Thanks to Ian Harvey and others who report issues and code for spending time in writing and improving BluePy which helps a lot to build scripts for BLE devices.