Warm tip: This article is reproduced from serverfault.com, please click

reading data from bluetooth thermometer with gatttool

发布于 2020-11-27 16:04:40

I stumbled upon this Question: Reading Thermometer Data with Bluez Bluetooth Low Energy and was following it trying to read data from a bluetooth thermometer that I got.

I was able to extract and read all handles with this command:

gatttool -b 00:11:22:33:44:55 -I
[00:11:22:33:44:55][LE]> connect
Connection successful
[00:11:22:33:44:55][LE]> primary
attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0008, end grp handle: 0x000b uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x000c, end grp handle: 0x001e uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x001f, end grp handle: 0xffff uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-desc 0x0001 0x0001
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-desc 0x0001 0x0007
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-read-hnd 01
Characteristic value/descriptor: 00 18 
[00:11:22:33:44:55][LE]> char-read-hnd 02
Characteristic value/descriptor: 02 03 00 00 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 03
Characteristic value/descriptor: 54 68 65 72 6d 6f 42 65 61 63 6f 6e 
[00:11:22:33:44:55][LE]> char-read-hnd 04
Characteristic value/descriptor: 02 05 00 01 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 05
Characteristic value/descriptor: 00 00 
[00:11:22:33:44:55][LE]> char-read-hnd 06
Characteristic value/descriptor: 02 07 00 04 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 07
Characteristic value/descriptor: 50 00 a0 00 00 00 e8 03 
[00:11:22:33:44:55][LE]> char-desc 0x0008 0x000b
handle: 0x0008, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0009, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000a, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x000b, uuid: 00002902-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-read-hnd 08
Characteristic value/descriptor: 01 18 
[00:11:22:33:44:55][LE]> char-read-hnd 09
Characteristic value/descriptor: 20 0a 00 05 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 0a
Error: Characteristic value/descriptor read failed: Attribute can't be read
[00:11:22:33:44:55][LE]> char-read-hnd 0b
Characteristic value/descriptor: 00 00 
[00:11:22:33:44:55][LE]> char-desc 0x000c 0x001e
handle: 0x000c, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000e, uuid: 00002a23-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0010, uuid: 00002a24-0000-1000-8000-00805f9b34fb
handle: 0x0011, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0012, uuid: 00002a25-0000-1000-8000-00805f9b34fb
handle: 0x0013, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0014, uuid: 00002a26-0000-1000-8000-00805f9b34fb
handle: 0x0015, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0016, uuid: 00002a27-0000-1000-8000-00805f9b34fb
handle: 0x0017, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0018, uuid: 00002a28-0000-1000-8000-00805f9b34fb
handle: 0x0019, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001a, uuid: 00002a29-0000-1000-8000-00805f9b34fb
handle: 0x001b, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001c, uuid: 00002a2a-0000-1000-8000-00805f9b34fb
handle: 0x001d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001e, uuid: 00002a50-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-read-hnd 0c
Characteristic value/descriptor: 0a 18 
[00:11:22:33:44:55][LE]> char-read-hnd 0d
Characteristic value/descriptor: 02 0e 00 23 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 0e
Characteristic value/descriptor: 5e 0b 00 00 00 00 f4 00 
[00:11:22:33:44:55][LE]> char-read-hnd 0f
Characteristic value/descriptor: 02 10 00 24 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 10
Characteristic value/descriptor: 4d 6f 64 65 6c 20 4e 75 6d 62 65 72 
[00:11:22:33:44:55][LE]> char-read-hnd 11
Characteristic value/descriptor: 02 12 00 25 2a
[00:11:22:33:44:55][LE]> char-read-hnd 12
Characteristic value/descriptor: 53 65 72 69 61 6c 20 4e 75 6d 62 65 72 
[00:11:22:33:44:55][LE]> char-read-hnd 13
Characteristic value/descriptor: 02 14 00 26 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 14
Characteristic value/descriptor: 46 69 72 6d 77 61 72 65 20 52 65 76 69 73 69 6f 6e 
[00:11:22:33:44:55][LE]> char-read-hnd 15
Characteristic value/descriptor: 02 16 00 27 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 16

Some of them like for example 4d 61 6e 75 66 61 63 74 75 72 65 72 20 4e 61 6d 65 reads Manufacturer Name letter by letter when converted with HEX to ASCII converter but some of them like for example 02 1c 00 2a 2a read ** with some blank squares

I also tried converting to decimal some of the numbers to try to get a temperature value but with no luck.

The values stay the same every time I read them so I guess this is no way to read the temperature value.

Do I have to somehow request that data from those handles. How do I find the termperature value from this data that I have above?

While I was reading this temperature was from 19.8 to 20.2°C, something like that (in case it is hidden somewhere in those values I have listed above)

I just want to read out the temperatur value from it nothing else.

UPDATE:

After turning scan on on bluetoothctl I am getting this packets from bluetooth thermometer:

[CHG] Device 00:11:22:33:44:55 RSSI: -81
[CHG] Device 00:11:22:33:44:55 TxPower: 0
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 24 0c 43 01 75 04 f3 5b  ..^.....$.C.u..[
  01 00                                            ..              
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 23 0c 43 01 79 04 02 5c  ..^.....#.C.y..\
  01 00                                            ..              
[CHG] Device 00:11:22:33:44:55 RSSI: -63
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 8c 01 2a 3d 00 00 34 01  ..^.......*=..4.
  c3 40 01 00                                      .@..            
[CHG] Device 00:11:22:33:44:55 RSSI: -81
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 24 0c 44 01 75 04 22 5c  ..^.....$.D.u."\
  01 00                                            ..              
[CHG] Device 00:11:22:33:44:55 RSSI: -54
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 8c 01 2a 3d 00 00 34 01  ..^.......*=..4.
  c3 40 01 00

Lets take the first set of data:

[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
      00 00 55 44 33 22 11 00 24 0c 43 01 75 04 f3 5b  ..^.....$.C.u..[
      01 00

I noticed by skipping the first 2 Bytes 55 44 33 22 11 00 MAC address of the device but in reverse.

After that 24 0c part repeats simiraly in other sets like for example in next one its 23 0c.

The next 2 Bytes (43 01) are the ones that I noticed change when temperature in my room changes and are the bytes that represent the temperature. Here is how I calculated temperature. Reverse the byte order -> 01 43 -> 0x0143 -> 323 in decimal -> 323/16 -> 20.1875 which is 20.2 rounded up. This is the exact temp that is on my thermometer and I tried it when temp was higher and lower and it always shows the exact temp.

Similarly the next two 75 04: 0x0475 -> 1141 in decimal -> 1141/16 = 71.3125 which is 71% rounded down -> humidity shown on thermometer

Is this the right interpretation?

What confuses me is the 3rd set of data which is longer and the data packets alternate between those two:

[CHG] Device 00:11:22:33:44:55 RSSI: -63
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 8c 01 2a 3d 00 00 34 01  ..^.......*=..4.
  c3 40 01 00

Is that some other data that thermometer is sending?

Questioner
cheshire
Viewed
0
ukBaz 2020-11-30 03:24:53

As a side note, gatttool is deprecated and the current supported tool for doing this is bluetoothctl.

GATT UUID's with the format 0000xxxx-0000-1000-8000-00805f9b34fb mean they have been adopted by the Bluetooth SIG and you can look up what they represent in the 16-bit UUID Numbers Document

There are also generic Bluetooth Low Energy scanning and exploration tools such as nRF connect that can be helpful exploring a device.

From the GATT information you posted, I could only see generic content that needs to be included and not anything specific about temperature.

The advertising data is including Manufacturer Data which can (as maybe the name suggests) be anything that the manufacturer wants. From the Core Specification Supplement

enter image description here

This means you need to get the information from the manufacturer or work backwards from what you think the data is saying. As you have not shared any information about the device broadcasting, it seems like you are heading in the right direction.

Most Bluetooth data is little endian so having to swap the bytes around is not unexpected. Beacon formats like iBeacon and Eddystone tend to be big endian but they are the exception rather than the rule.

You will probably want to use the D-Bus API if you want to get the data into some kind of code. There are D-Bus bindings for most languages and the BlueZ API you will need is documented at: