Command line BTLE

First things first, set up the Ci20. You can do all of the development for this project directly on the Ci20 if you wish, either by hooking it up to a monitor through HDMI or running it headless and connecting to it via ssh. Developing on the Ci20 makes experimenting easier, because you don't need to re-upload your software each time you want to test something. After you've set up the Ci20, you'll need to install the BlueZ bluetooth stack from source. Open a terminal and run the following (the sudo password is ci20):

sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
mkdir bluez && cd bluez
wget www.kernel.org/pub/linux/bluetooth/bluez-5.33.tar.xz
tar xf bluez-5.33.tar.xz
cd bluez-5.33
./configure --disable-systemd
make
sudo make install
sudo cp attrib/gatttool /usr/bin/

Note: If you're from the future, feel free to build the most recent release of BlueZ - at the time of writing, this is 5.33.

Use hcitool to find your peripheral. If you're using a ChiChinLighting smart bulb, it will be called LEDBlue-XXXXXXXX, where XXXXXXXX is the last eight digits of its mac address.

ci20@ci20:~$ sudo hcitool lescan
LE Scan...
B4:99:4c:47:99:FD (unknown)
B4:99:4c:47:99:FD LEDBlue-4C4799FD
57:08:BA:5A:29:ED (unknown)
57:08:BA:5A:29:ED (unknown)
D2:C7:0A:CF:35:73 (unknown)
D2:C7:0A:CF:35:73 BB-3573
BC:14:05:BD:FC:EA (unknown)
00:02:58:00:15:10 (unknown)
^Cci20@ci20:~$

Press ctrl+C (^C) to stop scanning. The mac address we want here is B4:99:4C:47:99:FD. Now connect to the peripheral using gatttool:

ci20@ci20:~$ gatttool -b b4:99:4c:47:99:fd -I
[b4:99:4c:47:99:fd][LE]> connect
Connecting to b4:99:4c:47:99:fd...
Connection successful.
[b4:99:4c:47:99:fd][LE]> _

If the connection is successful, the mac address at the front of the gatttool prompt will turn blue. If get an error that reads "No route to host (148)", try resetting the bluetooth adapter and running another scan:

sudo hciconfig hci0 reset
sudo hcitool lescan
gatttool -b <mac address> -I

After you've succesfully connected to the peripheral, test whether you can properly control it by issue characteristic write / read requests.

[    <mac address>][LE]> char-write-cmd 43 cc2433
[    <mac address>][LE]> char-write-cmd 43 cc2333
[    <mac address>][LE]> char-write-cmd 43 5651155700f0aa
[    <mac address>][LE]> char-write-cmd 43 560000003a0faa
[    <mac address>][LE]> char-write-cmd 43 bb250544
[    <mac address>][LE]> char-write-cmd 43 ef0177
Notification handle = 0x0050 value: 66 15 23 25 20 05 00 00 11 04 05 99
[    <mac address>][LE]> _

If you're using a ChiChinLighting smart bulb, that set of commands should do the following:

  1. Turn the bulb off.
  2. Turn the bulb on.
  3. Change the colour fo the bulb to purple.
  4. Change the bulb to a low warmth.
  5. Cause the bulb to smoothly cycle through colours.
  6. Cause the bulb to send a notification of its state.

If you're using a different peripheral, now is the time to either reverse engineer the control protocol (see previous post) or look up the service you'll be interfacing with. In the next post, we'll use pexpect to build a bluetooth interface for python using gatttool as a back end, so it's important that you be able to control the periphreral using gatttool.