Friday, July 3, 2015

TI SimpleLink SensorTag 2015 - Reading the Device Name

In the last post we reviewed setting up a Raspberry Pi with Bluetooth to access the sensor tag.  We finished up by successfully performing a Bluetooth Low Energy scan which saw the sensor tag and returned the address.

In this post we will walk through reading and writing to the sensor tag using gatttool which is provided as part of the bluez Bluetooth stack.

Reading the Sensor Tag’s Device Name

As discussed in the previous post you will need to make sure your sensor tag is advertising.  When advertising the green LED visible on the back of the sensor tag will be blinking.  If it isn’t blinking try removing and reinstalling the battery.  If that doesn’t work replace the battery.

In the example below the hcitool lescan function is used to find the sensor tag.  It reports back the sensor tag’s address shown in green below.  Once you have the sensor tag’s address you should run gatttool providing –b and the address and a –I.  The –b tells gatttool that the next set of numbers is the address of the device you want to use.  The –I is for interactive mode.  gatttool has both an interactive mode and a command line mode.

As you can see the steps start with connecting to the device.  If the connect fails it could be because you waited too long and the sensor tag exited advertise mode or it could be that you have a version of gatttool that doesn’t work.  Put the sensor tag back into advertise mode, make sure the green LED is blinking and try to connect again.  If it fails this time it is most likely problem with gatttool.

Once you successfully connect to the sensor tag the green LED will stop flashing.  Enter the char-read-hnd command which stands for read characteristic by handle for handle 0x3 which is the device name.  You should get back the string of numbers below which if you convert from ascii values to characters spells SensorTag 2.0.

Disconnect from the sensor tag and the green LED should start blinking again and exit gatttool.

pi@raspberrypi ~ $ sudo hcitool lescan
LE Scan ...
B0:B4:48:B9:2C:82 (unknown)
B0:B4:48:B9:2C:82 CC2650 SensorTag
^C
pi@raspberrypi ~ $ gatttool -b B0:B4:48:B9:2C:82 -I
[B0:B4:48:B9:2C:82][LE]> connect
Attempting to connect to B0:B4:48:B9:2C:82
Connection successful
[B0:B4:48:B9:2C:82][LE]> char-read-hnd 0x3
Characteristic value/descriptor: 53 65 6e 73 6f 72 54 61 67 20 32 2e 30
[B0:B4:48:B9:2C:82][LE]> disconnect

[B0:B4:48:B9:2C:82][LE]> exit

Below is a bash script that will perform the commands above and translate the numeric values to their ascii equivalents.

getDeviceName.sh

#!/bin/bash
if [ "$#" != 1 ]; then
  echo "Need to pass BLE Address $0 <BLE Address>"
else
  # Discard everything up to and including the ": " in the
  # gatttool response.  Put the bytes returned into an array. 
  # Convert the bytes to ascii and print out.
  str=`gatttool -b $1 --char-read --handle=0x3`
  IFS=' ' read -a array <<< ${str#*: }
  for element in "${array[@]}"
  do
    printf "\x$(printf "%x" "0x$element")"
  done
  echo
fi

No comments: