ESP for your Android: TI SensorTag

I recently received a TI SensorTag - a low cost development kit for communicating with external sensors via Bluetooth Low Energy from Texas Instruments. It’s great for learning the technology or even building small projects. I’ll detail how you can communicate with it from an HTC One Android phone!

Here is a picture of reading the temperature sensor from the phone on my desk:

Here is the picture again after cupping my hand around the sensor and blowing on it. The plastic fogged up and the temperature is reading higher. Hot!

The TI SensorTag contains sensors for temperature, acceleration, humidity, magnetic compass, air pressure, and a gyroscope. You can imagine the temperature sensing being used for a project for smart clothing for example - maybe a jacket that opens vents in the arms and back when the user gets hot. Acceleration could be used to detect a door being opened. You can order a SensorTag at TI’s page for it, CC2541 SensorTag Development Kit.

Bluetooth Low Energy (BLE) is a recent specification of Bluetooth that allows communicating state using very little power. Devices can be created that last over six months on a single coin cell battery. Programs can be written that read, write, and monitor peripherals from smartphones that support BLE. You can sign up for the HTC BLE SDK on the BLE Overview page on HTCdev.

So for example, you could cause a device to beep, read data from it, write data to it, or have a button on it that makes your phone beep. Or vibrate - or light up indicators - or take a picture - there are many possibilities. This concept is often called the internet of things. It’s the technology that will enable all the things we interact with to be smart and networked together.

If you can’t find your purse, it could have a sensor in it that your phone tracks the last time it was in range, tells you where, and can make it sound an alarm. Your weight machine at the gym could let your phone record exactly how much you did and plan the best workout for you. Your beer glass at the bar could let your friends know you are downing cold ones and to come join you.

The sample code I used for the above pictures is on GitHub as BleActionLog. The BLE API builds on top of already existing Bluetooth functionality in Android. So, for example, if you want to connect to a Polar H7 heart rate monitor or Bluetrek Cookoo watch, you first pair with the device in the phone’s settings. This then creates a bond that allows using encryption with devices like the Cookoo that require it to protect personal information.

Discovering devices works as with standard Bluetooth except that you can use the BLE SDK to check the type of device like this:

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

BluetoothDevice device = (BluetoothDevice) paramIntent

.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if (BleAdapter.getDeviceType(device) == BleAdapter.DEVICE_TYPE_BLE

|| BleAdapter.getDeviceType(device) == BleAdapter.DEVICE_TYPE_DUMO) {

addDevice(device);

}

}



In the code above, only BLE devices are added to the list of choosable devices to connect to for this BLE sample app.

To connect to a device you then call connect or connectBackground on a class extending BleClientProfile. Connection logic looks like this:

mClient.connect(device);

A custom profile class with one required service looks like this:

public class TempProfileClient extends BleClientProfile {

public TempProfileClient(final Context aContext, final byte aEncryption) {

super(aContext, new BleGattID(UUID.randomUUID()));

mService = new TempServiceClient(aContext);

final ArrayList requiredServices = new ArrayList();

requiredServices.add(mService);

init(requiredServices, null);



To receive temperature notifications from the sensor we have to write configuration to it turning on the sensor. That looks like this:

final BleCharacteristic tempConfig = new BleCharacteristic( new BleGattID(TiBleConstants.IRTEMPERATURE_CONF_UUID)); byte[] value = { 1 }; tempConfig.setValue(value); tempConfig.setWriteType(BleConstants.GATTC_TYPE_WRITE); mService.writeCharacteristic(aDevice, 0, tempConfig);

And receive them like this in the service:

public void onCharacteristicChanged(BluetoothDevice d, BleCharacteristic characteristic) {

super.onCharacteristicChanged(d, characteristic);

if ( BleUtils.eqId(characteristic, TiBleConstants.IRTEMPERATURE_DATA_UUID) ) {

final int dieTempRaw = BleCharacteristics.unsignedBytesToInt(

characteristic.getValue()[3],

characteristic.getValue()[2]);

final int dieTempC = dieTempRaw / 128;



In addition to reading temperature the sample code also supports reading heart rate from heart rate monitors and sending alerts to devices. I’ve tested these functions with a Polar H7 heart rate monitor and a Bluetrek Cookoo watch. The Polar H7 is even available down the street at a Sports Authority store nearby. There are newer heart rate monitors that you can wear on your wrist instead of around your chest. It’s really exciting seeing this technology get out!