Experiments with Bluetooth Low Energy (4.0) under Linux

Experiments with Bluetooth Low Energy (4.0) under Linux

With iPhônes liberated from the apple authentication chip by it, and Android also having added support in the latest Android 4.3, Bluetooth Low Energy (4.0) is starting to look more interesting.

In this blog post I‘m going to use a bluegiga BT111 based USB bluetooth dongle on Linux as a client and a TI CC2541 SensorTag as server.

USB dongle in computer

The SensorTag is a nice little bluetooth 4 example device provided by Texas Instruments.

Device

First step is put the device in the computer. As my laptop already has an existing bluetooth device build-in, the new one will enumerate as "hci1". This can be found out also by using hcitool.

$ hcitool dev
Devices:
        hci1    00:07:80:60:CE:4D
        hci0    EC:55:F9:F4:A0:xx

In this case the bluetooth mac address of the BT111 is 00:07:80:60:CE:4D.

Scanning for a device

Normal bluetooth scanning is done with hcitool scan. Low Energy scanning is done with hcitool lescan.

Make sure to press the button on the side of the SensorTag first to make it discoverable. Scanning is continuous to stop it with Ctrl-C. Low Energy devices require root access hence the sudo. I would expect being in the bluetooth group to be enough but for some reason it isn‘t.

$ sudo hcitool -i hci1 lescan
LE Scan ...
BC:6A:29:AC:2E:B4 (unknown)
BC:6A:29:AC:2E:B4 SensorTag
BC:6A:29:AC:2E:B4 (unknown)
BC:6A:29:AC:2E:B4 SensorTag
BC:6A:29:AC:2E:B4 (unknown)
^C

Using gatttool to access the device

Now we know the address of the SensorTag: BC:6A:29:AC:2E:B4. In contrast with regular Bluetooth where there are a whole range of protocols, with Bluetooth Low Energy there is only one protocol at the top and it is GATT (Generic Attribute).

The actual functionality of a device is implemented by means of attributes which can be read, written to or notification/indication enabled for, depending on the attribute.

gatttool is a simple Linux tool that can be used to manipulate these attributes with a Bluetooth Low Energy device. It can be used as a simple command line tool but I find it easier to use it in it‘s interactive mode.

Let‘s enter the interactive mode.

$ sudo gatttool -i hci1 -b BC:6A:29:AC:2E:B4 -I
[   ][BC:6A:29:AC:2E:B4][LE]>

It returns us a prompt. Let‘s connect to the device.

[   ][BC:6A:29:AC:2E:B4][LE]> connect
[CON][BC:6A:29:AC:2E:B4][LE]>

The CON indication tells us that the connection is established.
Now the primary command can be used to find the primary services of the device.

[CON][BC:6A:29:AC:2E:B4][LE]> primary
[CON][BC:6A:29:AC:2E:B4][LE]>
attr handle: 0x0001, end grp handle: 0x000b uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000c, end grp handle: 0x000f uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0x0022 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0023, end grp handle: 0x002a uuid: f000aa00-0451-4000-b000-000000000000
attr handle: 0x002b, end grp handle: 0x0035 uuid: f000aa10-0451-4000-b000-000000000000
attr handle: 0x0036, end grp handle: 0x003d uuid: f000aa20-0451-4000-b000-000000000000
attr handle: 0x003e, end grp handle: 0x0048 uuid: f000aa30-0451-4000-b000-000000000000
attr handle: 0x0049, end grp handle: 0x0054 uuid: f000aa40-0451-4000-b000-000000000000
attr handle: 0x0055, end grp handle: 0x005c uuid: f000aa50-0451-4000-b000-000000000000
attr handle: 0x005d, end grp handle: 0x0061 uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
attr handle: 0x0062, end grp handle: 0x0068 uuid: f000aa60-0451-4000-b000-000000000000
attr handle: 0x0069, end grp handle: 0xffff uuid: f000ffc0-0451-4000-b000-000000000000

Attributes in GATT have uuids and handles. UUIDs (unique identifiers) define a certain type of entry, handles can be used to access a value. To make things more complicated there is also nesting involved. Certain UUIDs are defined in the standard and always have to be provided. Others can just be vendor specific and provide the actual data for the service. It‘s beyond the scope of this blog post to further look into how all that fits together.

SensorTag attributes

Luckily for us, the meaning of the sensortag attributes are defined on the user guide at http://processors.wiki.ti.com/index.php/SensorTag_User_Guide.

For this blog I‘m looking at the humidity sensor as it is the easiest and the values shown in the document are actually correct :) http://processors.wiki.ti.com/index.php/SensorTag_User_Guide#Humidity_Sensor_2

As you can see on that page, there are 3 important handles for the humidity sensor, Data (0x38), DataNotification (0x39) and Config (0x3C).

Let‘s try reading from the Data handle:

[CON][BC:6A:29:AC:2E:B4][LE]> char-read-hnd 38
[CON][BC:6A:29:AC:2E:B4][LE]>
Characteristic value/descriptor: 00 00 00 00

So far so good, but no useful data. We first need to enable the sensor. This is done by writing 1 in the Config handle.

[CON][BC:6A:29:AC:2E:B4][LE]> char-write-req 3c 01
[CON][BC:6A:29:AC:2E:B4][LE]> Characteristic value was written successfully

Let‘s retry reading the value now.

[CON][BC:6A:29:AC:2E:B4][LE]> char-read-hnd 38
[CON][BC:6A:29:AC:2E:B4][LE]>
Characteristic value/descriptor: 68 68 4e 72

That‘s more like it. Using the math provided on the website and a python script, this gives the temperature and the humidity.

>>> -46.85 + 175.72/65536 * 0x6868
24.8151025390625
>>> -6.0 + 125.0/65536 * (0x72e4 & ~3)
50.09893798828125

So 25 Celsius and 50% humidity. That agrees with my local weather sensor. Nice :)

Instead of reading values it is also possible to use notifications.

[CON][BC:6A:29:AC:2E:B4][LE]> char-write-req 39 0100
[CON][BC:6A:29:AC:2E:B4][LE]> Characteristic value was written successfully

Notification handle = 0x0038 value: 68 68 f6 71
[CON][BC:6A:29:AC:2E:B4][LE]>
...

This will provide the temperature and the humidity every second automatically. Let‘s disable it again it is spamming me :)

[CON][BC:6A:29:AC:2E:B4][LE]> char-write-req 39 0000
[CON][BC:6A:29:AC:2E:B4][LE]> Characteristic value was written successfully

Finally let‘s disable the sensor and disconnect.

[CON][BC:6A:29:AC:2E:B4][LE]> char-write-req 3c 00
[CON][BC:6A:29:AC:2E:B4][LE]> Characteristic value was written successfully
[CON][BC:6A:29:AC:2E:B4][LE]> disconnect
[   ][BC:6A:29:AC:2E:B4][LE]>

Conclusion

Bluetooth Low Energy might look a bit different/strange at first but it is really quite nice! I‘m certainly going to explore it further!

Shameless plug!

Don‘t forget to check out the very nice BT111 based USB dongle available for sale at my tindie store!

 

copy from:http://joost.damad.be/2013/08/experiments-with-bluetooth-low-energy.html

时间: 2024-10-27 13:53:34

Experiments with Bluetooth Low Energy (4.0) under Linux的相关文章

基于蓝牙4.0(Bluetooth Low Energy)胎压监测方案设计

基于一种新的蓝牙技术——蓝牙4.0(Bluetooth Low Energy)新型的胎压监测系统(TPMS)的设计方案.鉴于蓝牙4.0(Bluetooth Low Energy)的低成本.低功耗.高稳定性等特点,适用于胎压监测系统,目前业界还没有出现类似的设计方案.本设计为直接式胎压监测系统,即在车辆轮胎上安装压力和温度传感器,通过蓝牙传输方式将胎压的信息传送给搭载蓝牙4.0的iPod.iPhone以及iPad,并在所安装的APP软件上显示实时数据.由此可实时监测车辆的胎压情况,并在胎压异常情况

Overview and Evaluation of Bluetooth Low Energy: An Emerging Low-Power Wireless Technology

转自:http://www.mdpi.com/1424-8220/12/9/11734/htm Sensors 2012, 12(9), 11734-11753; doi:10.3390/s120911734 Article Carles Gomez 1,*, Joaquim Oller 2 and Josep Paradells 2 1 Universitat Politècnica de Catalunya/Fundació i2Cat, C/Esteve Terradas, 7, Cast

Android as Bluetooth Low Energy Peripherial (GATT server).

I demonstrate how to write a simple BLE peripheral application in Android here. I am bad in Android development, The UI would be very ugly, but the code work: Currently(5/25/2015), the code could be running in Nexus 6 or Nexus 9 only based on my test

Bluetooth Low Energy 介绍

1.简介 BLE(Bluetooth Low Energy,低功耗蓝牙)是对传统蓝牙BR/EDR技术的补充.尽管BLE和传统蓝牙都称之为蓝牙标准,且共享射频,但是,BLE是一个完全不一样的技术.BLE不具备和传统蓝牙BR/EDR的兼容性.它是专为小数据率.离散传输的应用而设计的.通信距离上也有改变,传统蓝牙的传输距离几十米到几百米不等,BLE则规定为100米. 2.低功耗蓝牙(BLE) 低功耗蓝牙分为单模(Bluetooth Smart)和双模(Bluetooth Smart Ready)两种设

Bluetooth Low Energy(低功耗蓝牙)-For蓝牙4.x

此文翻译至Android API里的Bluetooth Low Energy,希望对大家有所帮助.谢谢. Android4.3(API版本18)介绍了内置平台支持BLE的中心角色,并且提供了相关API,高大尚的程序员们可以使用这些API来扫描设备.查询服务(指服务端进程).读写特性值(指特定的字符).与经典蓝牙不同的是,BLE的设计是为了提供显著的低功耗支持.这使得Android应用可以仅需很低的功耗与BLE设备进行通信,心跳频率(不是人的心跳,是指发送心跳包检测设备是否还在),监听适配设备等等

Bluetooth Low Energy 嗅探

0x00 前言 如果你打开这篇文章时期望看到一些新的东西,那么很抱歉这篇文章不是你在找的那篇文章.因为严格的来说这只是一篇整理文.里面没有任何我的发现,也没有我的实际案例.因为我手头上暂时还没有一个有趣的蓝牙低功耗设备.整篇文章的基础都建立在mike ryan这几年公布的演讲内容之上. 0x01 BLE BLE是什么?BLE全称bluetooth low energy中文又称蓝牙低功耗.最早被人们所知道是在2010年的时候出现在了bluetooth4的spec当中.由于它比传统的蓝牙更能控制功耗

Bluetooth low energy介绍

1. 介绍 Bluetooth low energy,也称BLE(低功耗蓝牙),在4.0规范中提出 BLE分为两种设备 - 单模(single-mode): Logo为「Bluetooth?Smart」 - 双模(dual-mode): Logo为「Bluetooth?Smart Ready」 .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New&quo

Android Bluetooth Low Energy官方文档翻译

Android Bluetooth Low Energy官方文档翻译 Android4.3(API18)为Bluetooth Low Energy(简称BLE)的核心功能提供了平台支撑,App能够通过它用来发现设备,查询服务,以及读写特性.与传统的蓝牙相比,BLE设计的最大特征就是低功耗.这使得Android的APP能够与具备低功耗的BLE设备进行通信,比如距离传感器,心跳检测,健身设备等等. 关键术语和概念 下面是一些关于BLE的核心术语和概念 Generic Attribute Profil

Android bluetooth low energy (ble) writeCharacteristic delay callback

I am implementing a application on Android using BLE Api (SDK 18), and I have a issue that the transfer data process is delay very slow. This is my log. 03-12 16:20:05.121: D/BluetoothGatt(13578): writeCharacteristic() - uuid: ... 03-12 16:20:06.272: