About Me

My photo
I know the last digit of PI

Wednesday, April 22, 2015

Read time and battery status from bluetooth device

Pair with the device

[root@laptop ~]$ bluetoothctl 
[NEW] Controller AA:BB:CC:DD:EE:FF laptop [default]
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller AA:BB:CC:DD:EE:FF Discovering: yes
[NEW] Device F1:F2:F3:F4:F5:F6 CHRONOS ECO XXXX
[bluetooth]# info F1:F2:F3:F4:F5:F6
Device F1:F2:F3:F4:F5:F6
 Name: CHRONOS ECO XXXX
 Alias: CHRONOS ECO XXXX
 Appearance: 0x1234
 Paired: no
 Trusted: no
 Blocked: no
 Connected: no
 LegacyPairing: no
 UUID: Vendor specific           (6e400001-b5a3-f393-e0a9-e50e24dcca9e)
[bluetooth]# trust F1:F2:F3:F4:F5:F6
[CHG] Device F1:F2:F3:F4:F5:F6 Trusted: yes
Changing F1:F2:F3:F4:F5:F6 trust succeeded
[bluetooth]# agent on
Agent registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# pair F1:F2:F3:F4:F5:F6
Attempting to pair with F1:F2:F3:F4:F5:F6
[CHG] Device F1:F2:F3:F4:F5:F6 Connected: yes
[CHG] Device F1:F2:F3:F4:F5:F6 UUIDs:
 00001800-0000-1000-8000-00805f9b34fb
 00001801-0000-1000-8000-00805f9b34fb
 00001805-0000-1000-8000-00805f9b34fb
 0000180a-0000-1000-8000-00805f9b34fb
 0000180f-0000-1000-8000-00805f9b34fb
 6e400001-b5a3-f393-e0a9-e50e24dcca9e
[CHG] Device F1:F2:F3:F4:F5:F6 Paired: yes
Pairing successful
[bluetooth]# paired-devices 
Device F1:F2:F3:F4:F5:F6 CHRONOS ECO XXXX
[bluetooth]# info F1:F2:F3:F4:F5:F6
Device F1:F2:F3:F4:F5:F6
 Name: CHRONOS ECO XXXX
 Alias: CHRONOS ECO XXXX
 Appearance: 0x1234
 Paired: yes
 Trusted: yes
 Blocked: no
 Connected: no
 LegacyPairing: no
 UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
 UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
 UUID: Current Time Service      (00001805-0000-1000-8000-00805f9b34fb)
 UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
 UUID: Battery Service           (0000180f-0000-1000-8000-00805f9b34fb)
 UUID: Vendor specific           (6e400001-b5a3-f393-e0a9-e50e24dcca9e)
[bluetooth]# quit

We will read the information from the "Current Time Service" and "Battery Service"

Connect to the device and list services with handles
[root@laptop ~]$ gatttool -t random -I -b F1:F2:F3:F4:F5:F6
[F1:F2:F3:F4:F5:F6][LE]> connect
Attempting to connect to F1:F2:F3:F4:F5:F6
Connection successful
[F1:F2:F3:F4:F5:F6][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: 0x0011 uuid: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
attr handle: 0x0012, end grp handle: 0x0015 uuid: 0000180f-0000-1000-8000-00805f9b34fb
attr handle: 0x0016, end grp handle: 0x0020 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0021, end grp handle: 0xffff uuid: 00001805-0000-1000-8000-00805f9b34fb
Connect to handle of "Current Time Service" (00001805-0000-1000-8000-00805f9b34fb).

Use the handle and the grp handle values.
[F1:F2:F3:F4:F5:F6][LE]> characteristics 0x0021 0xffff
handle: 0x0022, char properties: 0x12, char value handle: 0x0023, uuid: 00002a2b-0000-1000-8000-00805f9b34fb
Note the "char value handle" and use it for reading characteristics data.
[F1:F2:F3:F4:F5:F6][LE]> char-read-hnd 0x0023
Characteristic value/descriptor: df 07 04 1A 16 0e 00 00 03 00 00 00 
the first two bytes are the year: 07df = 2015
the thrid byte is the month: 04 = April
the fourth byte is the date: 1A = 26
the fifth byte is the hour: 16 = 22
the sixth byte is the minutes: 0e = 14
So the date is: 26.April.2015 22:14
More information about the format could be found here

Now let's read the battery status from Battery Service (0000180f-0000-1000-8000-00805f9b34fb)
[F1:F2:F3:F4:F5:F6][LE]> characteristics 0x0012 0x0015
handle: 0x0013, char properties: 0x12, char value handle: 0x0014, uuid: 00002a19-0000-1000-8000-00805f9b34fb
[F1:F2:F3:F4:F5:F6][LE]> char-read-hnd 0x0014
Characteristic value/descriptor: 5a
This is the battery status (in percentage): 5a = 90
So the battery level is 90%.
More information about the format could be found here