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
Below is a bash script that will perform the commands above and translate the numeric values to their ascii equivalents.