Difference between revisions of "RPi Bluetooth LE"

From eLinux.org
Jump to: navigation, search
m (Simple bluepy programming)
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
= Bluetooth LE on the Raspberry Pi =
 
= Bluetooth LE on the Raspberry Pi =
  
At the time of writing Bluetooth Low Energy (BLE) is not well supported by the standard Raspberry Pi distributions. Some work is required to get it working, as described below.
+
The [https://www.raspberrypi.org/products/raspberry-pi-3-model-b/ Raspberry Pi 3] has built-in support for Bluetooth Low Energy (BLE). BLE is the technology behind many fitness trackers and smartwatches, and allows small, low-power devices to transmit and receive information from a central computer or smartphone.
 +
 
 +
This tutorial gets you started with Bluetooth Low Energy using a [http://www.ti.com/tool/cc2650stk Texas Instruments SensorTag] - these are small, robust, relatively cheap and do interesting things out of the box. You can buy one [http://www.ti.com/tool/cc2650stk#buy directly from TI], or in the UK they are available from [http://uk.rs-online.com/web/p/radio-frequency-development-kits/8735892/ RS] and [http://uk.farnell.com/texas-instruments/cc2650stk/evaluation-mod-simplelink-sensor/dp/2470181 Farnell].
  
 
== Pre-requisites ==
 
== Pre-requisites ==
  
=== Software ===
+
=== Compatible Hardware ===
 +
This page was written assuming the following hardware:
 +
* Raspberry Pi 3 Model B
 +
* Raspbian Jessie (April 2017), although later versions will probably work.
 +
* TI CC2650 SensorTag, firmware 1.30 (May 2016) or later
 +
 
 +
We'll also assume you are familiar with the basics of typing Bash commands using the [https://www.raspberrypi.org/documentation/usage/terminal/ command line].
 +
 
 +
Older models of the Pi should work with an external USB adapter. The author has used these two successfully:
 +
* Plugable USB-BT4LE adapter: http://plugable.com/products/usb-bt4le (uses BCM20702 chipset)
 +
* CSR chipset adapter http://www.amazon.co.uk/Version-Bluetooth-Adapter-Compatible-Windows/dp/B00A0CBOTE
 +
 
 +
 
 +
=== Basic installation checks ===
 +
 
 +
The Pi 3's built-in Bluetooth adapter is called ''hci0''. You can check it is operating correctly with the command:
 +
hciconfig
 +
 
 +
This should show something like this:
 +
hci0: Type: BR/EDR  Bus: UART
 +
BD Address: B8:27:EB:23:E2:A4  ACL MTU: 1021:8  SCO MTU: 64:1
 +
UP RUNNING
 +
RX bytes:1987 acl:0 sco:0 events:91 errors:0
 +
TX bytes:1647 acl:0 sco:0 commands:57 errors:0
 +
 
 +
If for some reason it is shown as <tt>DOWN</tt> you can re-enable it with:
 +
sudo hciconfig hci0 up
 +
 
 +
== Using the SensorTag ==
 +
=== Scanning using <tt>hcitool</tt> ===
 +
 
 +
[[File:Sensortag-buttons.jpg|frameless|SensorTag with outer case removed]]
 +
 
 +
If you've not already done so, take a look at TI's [http://www.ti.com/ww/en/wireless_connectivity/sensortag/gettingStarted.html Getting Started Guide] which explains how to set up the SensorTag.
 +
 
 +
To wake it up, press the button on the left-hand side (see illustration) and the green LED should flash, once per second.
 +
 
 +
On the Pi, enter the command:
 +
  sudo hcitool lescan
 +
 
 +
This will start a scan for any Bluetooth LE devices in range which are currently 'advertising'. You should shortly see something like this:
 +
  pi@raspberrypi:~ $ sudo hcitool lescan
 +
  LE Scan ...
 +
  B0:B4:48:ED:44:C3 (unknown)
 +
  B0:B4:48:ED:44:C3 CC2650 SensorTag
 +
 
 +
Type Control-C to stop <tt>lescan</tt>. The 12 hexadecimal digits (<tt>B0:B4:48:ED:44:C3</tt> in the example) are your tag's MAC address. You will need to know this when making Bluetooth LE connections to it.
 +
 
 +
=== Connecting with <tt>gatttool</tt> ===
  
This page has been tested using the Raspbian ''Wheezy'' distribution, in particular the image at:
+
The <tt>gatttool</tt> utility can make connections to Bluetooth LE devices. To try this, make sure the SensorTag's LED is flashing then run:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2014-01-09/2014-01-07-wheezy-raspbian.zip
+
  gatttool -I -b <i>MAC-address</i>
  
For reference, <tt>uname -a</tt> on the test system returned:
+
where ''MAC-address'' is the value reported by <tt>hcitool</tt> (see above). <tt>gatttool</tt> will give you a prompt, ending <tt>[LE]&gt;</tt>. Type <tt>connect</tt> to make a connection to the SensorTag. This is shown below:
Linux raspberrypi 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l GNU/Linux 
 
  
=== Hardware ===
+
  pi@raspberrypi:~ $ gatttool -I -b B0:B4:48:ED:44:C3
 +
  [B0:B4:48:ED:44:C3][LE]> connect
 +
  Attempting to connect to B0:B4:48:ED:44:C3
 +
  Connection successful
 +
  [B0:B4:48:ED:44:C3][LE]>
  
You will need a Bluetooth 4.0 compatible USB adapter to use with the Pi. The following devices have been tested:
+
You can then type a number of commands. The <tt>primary</tt> command lists the available 'Services', which are groups containing 'Characteristics' - these are data items which can be read or written to the device. So, to read the device name you could use the <tt>char-read-uuid</tt> command, giving it the ID of the Bluetooth [https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.device_name.xml Device Name characteristic]:
* Plugable USB-BT4LE adapter: http://plugable.com/products/usb-bt4le (uses BCM20702 chipset)
+
 
* CSR chipset adapter http://www.amazon.co.uk/Version-Bluetooth-Adapter-Compatible-Windows/dp/B00A0CBOTE
+
  [B0:B4:48:ED:44:C3][LE]> char-read-uuid 00002a00-0000-1000-8000-00805f9b34fb
 +
  handle: 0x0003 value: 53 65 6e 73 6f 72 54 61 67 20 32 2e 30
 +
 
 +
(These are the ASCII values for ''SensorTag 2.0''.
 +
 
 +
The Sensortag User Guide at http://processors.wiki.ti.com/index.php/CC2650_SensorTag_User's_Guide lists all the available services and characteristics. For example, to turn on the optical sensor, we need to discover the 'handle' for the configuration characteristic (with ID ''f000aa72-0451-4000-b000-000000000000''), then write an 0x01 byte to it:
 +
 
 +
  [B0:B4:48:ED:44:C3][LE]> characteristics 1 ffff f000aa72-0451-4000-b000-000000000000
 +
  handle: 0x0046, char properties: 0x0a, char value handle: 0x0047, uuid: f000aa72-0451-4000-b000-000000000000
 +
  [B0:B4:48:ED:44:C3][LE]> char-write-req 47 01
 +
  Characteristic value was written successfully
 +
 
 +
In the example above the handle value was 0x47 (it may change with firmware versions). One the sensor is enabled, the light level can then be read from characteristic ''f000aa71-0451-4000-b000-000000000000'':
 +
 
 +
  [B0:B4:48:BF:C9:83][LE]> char-read-uuid f000aa71-0451-4000-b000-000000000000
 +
  handle: 0x0044 value: bc 0a
 +
 
 +
In this example the light level bytes are ''bc 0a'', and will change with the amount of light falling on the CC2650 device.
 +
 
 +
 
 +
== Using Bluetooth LE with Python ==
 +
 
 +
=== Installing <tt>bluepy</tt> ===
 +
Using <tt>gatttool</tt> is very laborious for doing any useful work, so it's a good idea to use a programming language. The ''bluepy'' package is one way to use Bluetooth LE commands from Python - it can be installed from https://pypi.python.org/pypi using the <tt>pip</tt> command. If you don't already have <tt>pip</tt>, on the Pi or other Debian systems it is easily installed with:
 +
 
 +
  sudo apt-get install python-pip
 +
 
 +
Before installing ''bluepy'' you will also need some support libraries:
 +
 
 +
  sudo apt-get install libglib2.0-dev
  
== BlueZ installation ==
+
Then you can run:
  
By default, the Wheezy distribution comes without a Bluetooth stack. The <tt>bluez</tt> package is version 4.99, which has patchy support for Low Energy. You can build and install a more modern version as follows:
+
  sudo pip install bluepy
  
=== BlueZ 5.4 build instructions ===
+
If successful, it will show a message such as this:
  
Firstly, you'll need a download and install a number of development libraries. From a [[RPi_Beginners#Intro_to_the_CLI_.28Command_Line_Interface.29|command prompt]] run:
+
      Installing blescan script to /usr/local/bin
+
      Installing sensortag script to /usr/local/bin
sudo apt-get install libdbus-1-dev libglib2.0-dev libdbus-glib-1-dev
+
  Successfully installed bluepy
sudo apt-get install libusb-dev libudev-dev libreadline-dev
+
  Cleaning up...
  
Then, download the source:
+
=== <tt>bluepy</tt> command-line programs ===
mkdir -p work/bluepy
 
cd work/bluepy
 
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.4.tar.xz
 
xz -d bluez-5.4.tar.xz
 
tar xvf bluez-5.4
 
  
If successful, you'll now have a <tt>bluez-5.4</tt> source code directory on disk. To build it, do:
+
The <tt>blescan</tt> program performs a similar function to "<tt>hcitool lescan</tt>" but produces more information. You will need root privileges (using e.g. <tt>sudo</tt>) to run it. Typical output looks like this:
cd bluez-5.4
 
./configure --disable-systemd
 
make
 
  
This will take half an hour or so. When this is done, do:
+
  pi@raspberrypi:~ $ sudo blescan
sudo make install
+
  Scanning for devices...
 +
      Device (new): b0:b4:48:ed:44:c3 (public), -68 dBm
 +
    Flags: <05>
 +
    Incomplete 16b Services: <80aa>
 +
    Complete Local Name: 'CC2650 SensorTag'
 +
    Tx Power: <00>
 +
    0x12: <08002003>
 +
    Manufacturer: <0d00030000>
  
== First tests ==
+
You can alter its behaviour with various command-line options; <tt>blescan -h</tt> gives help text.
  
=== USB Checks ===
+
The <tt>sensortag</tt> program connects to various versions of SensorTag, and reads their sensors. <tt>sensortag -h</tt> gives a full list of options; a typical run looks like this:
  
To ensure the USB Bluetooth device is being seen, run
+
  pi@raspberrypi:~ $ sensortag -T -B B0:B4:48:ED:44:C3
lsusb
+
  Connecting to B0:B4:48:ED:44:C3
 +
  ('Temp: ', (26.84375, 21.3125))
 +
  ('Barometer: ', (27.15, 1017.46))
 +
  ('Temp: ', (26.875, 21.5625))
 +
  ('Barometer: ', (27.15, 1017.48))
 +
  ...
  
With the Plugable adapter shown above, the result will be similar to this:
+
Use Ctrl-C to stop the program.
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
 
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
 
Bus 001 Device 004: ID 0a5c:21e8 Broadcom Corp.  
 
  
The adapter is the one on <tt>Bus 001 Device 004</tt>. To show more information about the 'Broadcom' device, do:
+
=== Simple <tt>bluepy programming</tt> ===
sudo lsusb -v -d 0a5c:
 
  
=== Using hcitool ===
+
The following simple example shows how to connect to a device and display its services:
  
If this is working, run the <tt>hciconfig</tt> tool:
+
  from bluepy import btle
pi@raspberrypi ~ $ hciconfig
+
 
hci0: Type: BR/EDR  Bus: USB
+
  print "Connecting..."
BD Address: 00:02:72:14:27:0E  ACL MTU: 1021:8  SCO MTU: 64:1
+
  dev = btle.Peripheral("B0:B4:48:BF:C9:83")
DOWN
+
 
RX bytes:2715 acl:1 sco:0 events:146 errors:0
+
  print "Services..."
TX bytes:2500 acl:0 sco:0 commands:133 errors:0
+
  for svc in dev.services:
 +
      print str(svc)
  
If the bluetooth adapter (<tt>hci0</tt> in the above example) is down as <tt>DOWN</tt>, you will need to enable it with:
+
For a Sensortag, ensure the green LED is flashing before trying to connect. To connect to the SensorTag's "light level" service, and list the characteristics, you could add this:
sudo hciconfig hci0 up
 
  
=== Scanning for LE devices ===
+
  lightSensor = btle.UUID("f000aa70-0451-4000-b000-000000000000")
 +
 
 +
  lightService = dev.getServiceByUUID(lightSensor)
 +
  for ch in lightService.getCharacteristics():
 +
      print str(ch)
  
If you have any Bluetooth LE devices, you can start scanning for them with:
+
To initialize and read from the light sensor you might first add:
sudo hcitool lescan
 
  
For the [http://www.ti.com/sensortag TI SensorTag], pressing the button on the side will ensure the device is awake and broadcasting advertising packets. Other LE devices may need a similar action - please consult the relevant user manual.
+
  import time
 +
  import binascii
  
=== Testing connections ===
+
to the top of the program, then add the following lines:
  
The <tt>lescan</tt> command above should have displayed the MAC address (six hex digits, like ''00:1e:3f:22:4b:a7'') of any Bluetooth LE devices in range and available for connection. You can check a particular device by attempting to make (and then drop) a connection to it, using a command like:
+
  uuidConfig = btle.UUID("f000aa72-0451-4000-b000-000000000000")
  sudo hcitool lecc 00:1e:3f:22:4b:a7
+
  lightSensorConfig = lightService.getCharacteristics(uuidConfig)[0]
 +
  # Enable the sensor
 +
  lightSensorConfig.write(bytes("\x01"))
 +
 
 +
  time.sleep(1.0) # Allow sensor to stabilise
 +
 
 +
  uuidValue = btle.UUID("f000aa71-0451-4000-b000-000000000000")
 +
  lightSensorValue = lightService.getCharacteristics(uuidValue)[0]
 +
  # Read the sensor
 +
  val = lightSensorValue.read()
 +
  print "Light sensor raw value", binascii.b2a_hex(val)
  
(Obviously, replace 00:1e ... with a MAC address from your <tt>lescan</tt> output).
+
Once you have set <tt>lightSensorConfig</tt> and <tt>lightSensorValue</tt> (these are Bluepy <tt>Characteristic</tt> objects), you can simply use their <tt>write()</tt> and <tt>read()</tt> methods repeatedly. (There is no need to call <tt>getCharacteristics()</tt> each time).
  
== Using Bluetooth LE from Python ==
+
= Using Bluetooth LE with Go (Golang) =
  
You can communicate with Bluetooth LE devices from Python using the '''bluepy''' package. This is currently source code you need to build yourself; there isn't an official installer for it yet.
+
[https://github.com/paypal/gatt Gatt] is a [https://golang.org/ Go] package, which provides developers to create BLE applications for Linux and OS X.
  
Fetch the source by changing directory to a suitable work area and running:
+
Developers [https://golang.org/doc/install/source install Go language] on the host machine, and cross-compile the applications for RPi.  
git clone https://github.com/IanHarvey/bluepy.git
 
  
This should create a directory <tt>bluepy/</tt>. If you have already fetched all the packages needed in ''BlueZ 5.4 Build Instructions' above, you can build the source with just:
+
The package accesses HCI devices directly via HCI sockets provided by BlueZ core (kernel space), so it doesn't require the BlueZ userland package.  
  
  cd bluepy/bluepy
+
=== To test the example programs (sample GATT server and clients): ===
  make
 
  
If successful you will have built an executable <tt>bluepy-helper</tt>.
+
Cross-compile the server example for an ARMv6 target device.
 +
  GOARCH=arm GOARM=6 GOOS=linux go build examples/server.go
 +
  cp server <target device>
  
To test this, first ensure that <tt>hcitool lecc</tt> works (see above) so you can manually create a connection, then run <tt>btle.py</tt> giving it your peripheral's MAC address, e.g.
+
Start the server on the target device
 +
  sudo ./server
  
python btle.py 00:1e:3f:22:4b:a7
+
Cross-compile the client example (discoverer) for an ARMv6 target device.
 +
  GOARCH=arm GOARM=6 GOOS=linux go build examples/discoverer.go
 +
  cp discoverer <target device>
  
This will attempt to enumerate the services and characteristics presented by the device.
+
Run the discoverer to scan surrounding peripheral devices.
 +
  sudo ./discoverer
  
There is currently no official documentation for the '''bluepy''' module; you will need to use the Python source as example code.
 
 
 
== Links: other Bluetooth Low Energy resources ==
 
== Links: other Bluetooth Low Energy resources ==
  

Latest revision as of 13:38, 4 July 2017

Bluetooth LE on the Raspberry Pi

The Raspberry Pi 3 has built-in support for Bluetooth Low Energy (BLE). BLE is the technology behind many fitness trackers and smartwatches, and allows small, low-power devices to transmit and receive information from a central computer or smartphone.

This tutorial gets you started with Bluetooth Low Energy using a Texas Instruments SensorTag - these are small, robust, relatively cheap and do interesting things out of the box. You can buy one directly from TI, or in the UK they are available from RS and Farnell.

Pre-requisites

Compatible Hardware

This page was written assuming the following hardware:

  • Raspberry Pi 3 Model B
  • Raspbian Jessie (April 2017), although later versions will probably work.
  • TI CC2650 SensorTag, firmware 1.30 (May 2016) or later

We'll also assume you are familiar with the basics of typing Bash commands using the command line.

Older models of the Pi should work with an external USB adapter. The author has used these two successfully:


Basic installation checks

The Pi 3's built-in Bluetooth adapter is called hci0. You can check it is operating correctly with the command:

hciconfig

This should show something like this:

hci0:	Type: BR/EDR  Bus: UART
	BD Address: B8:27:EB:23:E2:A4  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:1987 acl:0 sco:0 events:91 errors:0
	TX bytes:1647 acl:0 sco:0 commands:57 errors:0
 

If for some reason it is shown as DOWN you can re-enable it with:

sudo hciconfig hci0 up

Using the SensorTag

Scanning using hcitool

SensorTag with outer case removed

If you've not already done so, take a look at TI's Getting Started Guide which explains how to set up the SensorTag.

To wake it up, press the button on the left-hand side (see illustration) and the green LED should flash, once per second.

On the Pi, enter the command:

 sudo hcitool lescan

This will start a scan for any Bluetooth LE devices in range which are currently 'advertising'. You should shortly see something like this:

 pi@raspberrypi:~ $ sudo hcitool lescan 
 LE Scan ...
 B0:B4:48:ED:44:C3 (unknown)
 B0:B4:48:ED:44:C3 CC2650 SensorTag

Type Control-C to stop lescan. The 12 hexadecimal digits (B0:B4:48:ED:44:C3 in the example) are your tag's MAC address. You will need to know this when making Bluetooth LE connections to it.

Connecting with gatttool

The gatttool utility can make connections to Bluetooth LE devices. To try this, make sure the SensorTag's LED is flashing then run:

 gatttool -I -b MAC-address

where MAC-address is the value reported by hcitool (see above). gatttool will give you a prompt, ending [LE]>. Type connect to make a connection to the SensorTag. This is shown below:

 pi@raspberrypi:~ $ gatttool -I -b B0:B4:48:ED:44:C3
 [B0:B4:48:ED:44:C3][LE]> connect
 Attempting to connect to B0:B4:48:ED:44:C3
 Connection successful
 [B0:B4:48:ED:44:C3][LE]>

You can then type a number of commands. The primary command lists the available 'Services', which are groups containing 'Characteristics' - these are data items which can be read or written to the device. So, to read the device name you could use the char-read-uuid command, giving it the ID of the Bluetooth Device Name characteristic:

 [B0:B4:48:ED:44:C3][LE]> char-read-uuid 00002a00-0000-1000-8000-00805f9b34fb
 handle: 0x0003 	 value: 53 65 6e 73 6f 72 54 61 67 20 32 2e 30 

(These are the ASCII values for SensorTag 2.0.

The Sensortag User Guide at http://processors.wiki.ti.com/index.php/CC2650_SensorTag_User's_Guide lists all the available services and characteristics. For example, to turn on the optical sensor, we need to discover the 'handle' for the configuration characteristic (with ID f000aa72-0451-4000-b000-000000000000), then write an 0x01 byte to it:

 [B0:B4:48:ED:44:C3][LE]> characteristics 1 ffff f000aa72-0451-4000-b000-000000000000
 handle: 0x0046, char properties: 0x0a, char value handle: 0x0047, uuid: f000aa72-0451-4000-b000-000000000000
 [B0:B4:48:ED:44:C3][LE]> char-write-req 47 01
 Characteristic value was written successfully

In the example above the handle value was 0x47 (it may change with firmware versions). One the sensor is enabled, the light level can then be read from characteristic f000aa71-0451-4000-b000-000000000000:

 [B0:B4:48:BF:C9:83][LE]> char-read-uuid f000aa71-0451-4000-b000-000000000000
 handle: 0x0044 	 value: bc 0a 

In this example the light level bytes are bc 0a, and will change with the amount of light falling on the CC2650 device.


Using Bluetooth LE with Python

Installing bluepy

Using gatttool is very laborious for doing any useful work, so it's a good idea to use a programming language. The bluepy package is one way to use Bluetooth LE commands from Python - it can be installed from https://pypi.python.org/pypi using the pip command. If you don't already have pip, on the Pi or other Debian systems it is easily installed with:

 sudo apt-get install python-pip

Before installing bluepy you will also need some support libraries:

 sudo apt-get install libglib2.0-dev

Then you can run:

 sudo pip install bluepy

If successful, it will show a message such as this:

     Installing blescan script to /usr/local/bin
     Installing sensortag script to /usr/local/bin
 Successfully installed bluepy
 Cleaning up...

bluepy command-line programs

The blescan program performs a similar function to "hcitool lescan" but produces more information. You will need root privileges (using e.g. sudo) to run it. Typical output looks like this:

 pi@raspberrypi:~ $ sudo blescan 
 Scanning for devices...
     Device (new): b0:b4:48:ed:44:c3 (public), -68 dBm 
 	  Flags: <05>
 	  Incomplete 16b Services: <80aa>
 	  Complete Local Name: 'CC2650 SensorTag'
 	  Tx Power: <00>
 	  0x12: <08002003>
 	  Manufacturer: <0d00030000>

You can alter its behaviour with various command-line options; blescan -h gives help text.

The sensortag program connects to various versions of SensorTag, and reads their sensors. sensortag -h gives a full list of options; a typical run looks like this:

 pi@raspberrypi:~ $ sensortag -T -B B0:B4:48:ED:44:C3
 Connecting to B0:B4:48:ED:44:C3
 ('Temp: ', (26.84375, 21.3125))
 ('Barometer: ', (27.15, 1017.46))
 ('Temp: ', (26.875, 21.5625))
 ('Barometer: ', (27.15, 1017.48))
 ...

Use Ctrl-C to stop the program.

Simple bluepy programming

The following simple example shows how to connect to a device and display its services:

 from bluepy import btle
 
 print "Connecting..."
 dev = btle.Peripheral("B0:B4:48:BF:C9:83")
 
 print "Services..."
 for svc in dev.services:
     print str(svc)

For a Sensortag, ensure the green LED is flashing before trying to connect. To connect to the SensorTag's "light level" service, and list the characteristics, you could add this:

 lightSensor = btle.UUID("f000aa70-0451-4000-b000-000000000000")
 
 lightService = dev.getServiceByUUID(lightSensor)
 for ch in lightService.getCharacteristics():
     print str(ch)

To initialize and read from the light sensor you might first add:

 import time
 import binascii

to the top of the program, then add the following lines:

 uuidConfig = btle.UUID("f000aa72-0451-4000-b000-000000000000")
 lightSensorConfig = lightService.getCharacteristics(uuidConfig)[0]
 # Enable the sensor
 lightSensorConfig.write(bytes("\x01"))
 
 time.sleep(1.0) # Allow sensor to stabilise
 
 uuidValue  = btle.UUID("f000aa71-0451-4000-b000-000000000000")
 lightSensorValue = lightService.getCharacteristics(uuidValue)[0]
 # Read the sensor
 val = lightSensorValue.read()
 print "Light sensor raw value", binascii.b2a_hex(val)

Once you have set lightSensorConfig and lightSensorValue (these are Bluepy Characteristic objects), you can simply use their write() and read() methods repeatedly. (There is no need to call getCharacteristics() each time).

Using Bluetooth LE with Go (Golang)

Gatt is a Go package, which provides developers to create BLE applications for Linux and OS X.

Developers install Go language on the host machine, and cross-compile the applications for RPi.

The package accesses HCI devices directly via HCI sockets provided by BlueZ core (kernel space), so it doesn't require the BlueZ userland package.

To test the example programs (sample GATT server and clients):

Cross-compile the server example for an ARMv6 target device.

 GOARCH=arm GOARM=6 GOOS=linux go build examples/server.go
 cp server <target device>

Start the server on the target device

 sudo ./server

Cross-compile the client example (discoverer) for an ARMv6 target device.

 GOARCH=arm GOARM=6 GOOS=linux go build examples/discoverer.go
 cp discoverer <target device>

Run the discoverer to scan surrounding peripheral devices.

 sudo ./discoverer

Links: other Bluetooth Low Energy resources