Bluetooth Low Energy: listening for notifications/indications in linux - linux

I'm trying to communicate with a BLE module through a Linux machine (the module is running a heart rate profile). So far, I've been able to do everything I need except listening for Notifications and indications (e.g. listening for the Heart Rate Measurement Notification). I'm using kernel version 3.5 and bluez-5.3.
Succcessful commands used so far:
hcitool lescan
hcitool lecc
gatttool -b <Mac Address> --primary
gatttool -b <MAC Address> --characteristics
gatttool -b <MAC Address> --char-read
gatttool -b <MAC Address> --char-desc
gatttool -b <MAC Address> --interactive
Failed commands:
gatttool -b <MAC Address> --listen
Any help is greatly appreciated.

Try this...
Run gatttool -b <MAC Address> --interactive like you did before. You'll get a prompt and then you type connect. You should see a CON in the prompt indicating that you've connected to the device. Then type char-read-uuid 2902. You should get a list of all CCC (Client Characteristic Configuration) attributes on the device. You can try setting them all to 0100 to get notifications, 0200 for indications, 0300 for both, or 0000 for everything off. Type help to see all the commands and their arguments.
EDIT:
The use of the --listen argument requires you to couple it with other commands to turn on the notifications and/or indications. So here's an example that works in Bluez 4.101:
gatttool -b <MAC Address> --char-write-req --handle=0x0031 --value=0100 --listen
Obviously you need to change the handle to the handle of the CCC that you want to turn on notifications for. However, I still find it way easier to just use the interactive mode.

Looks like the older version of Bluez (hcitool & gatttool) don't allow you to write to Bluetooth Low Energy devices. I ended up installing a newer version (5.17 as of this writing) in order to enabled notifications, etc.
To get a list of all your handles you can run the following:
char-desc
You can then read from a handle:
char-read-hnd 0x000e
(the above handle is for my nrf51822 battery level)
Where the handle is one from the list you got from char-desc.
Just like Tim said above, you can write to the notification related handle to get indications or notifications. (in my case my device only had notifications)
char-write-req 0x000f 0100
(the above handle is for my nrf51822 battery level notification)
In my case the battery notification shouldn't send anything until the battery level has changed.
I wrote a pretty lengthy blog post on getting setup with Bluez. You can find it here: Get Started with Bluetooth Low Energy Feel free to check it out!

Final answer for reading heart rate on Mio Alpha :
gatttool -b xx:xx:xx:xx:xx:xx -t random --char-write-req -a 0x0025 -n 0100 --listen
Characteristic value was written successfully
Notification handle = 0x0024 value: 10 4b 33 03
Notification handle = 0x0024 value: 10 4b 33 03
Notification handle = 0x0024 value: 10 4b 33 03
Notification handle = 0x0024 value: 10 4a 3e 03
Notification handle = 0x0024 value: 10 4c 28 03 28 03
Notification handle = 0x0024 value: 10 4c 28 03
Notification handle = 0x0024 value: 10 4b 33 03
Notification handle = 0x0024 value: 10 4a 3e 03 3e 03

to retain the CCC value you need to pair the two device. once they are paired you do not need to set the CCC again. on next reconnection it will find the setting , which will be saved in thec FLASH of Key fob. try to configure after pairing via SMP.

Related

BlueZ, How to turn off br/edr without using btmgmt?

I'm new to Bluetooth and BlueZ, I was working on a BLE project using BlueZ, for some reason I cannot use btmgmt -i hci0 bredr off command to turn off br/edr. I was wondering how to turn of br/edr using hcitool or any other util provided by BlueZ. Thanks in advance.
After a few days reading some question on Stackoverflow and reading Bluetooth specification. I finally found a way to use hcitool to do it. The command is
hcitool -i hci0 cmd 0x08 0x0008 12 02 01 04 02 0A 0C 0B
where 01 04 is the key 01 referred to the flag, 04 referred to bredr not supported. Thank u all.

How do we get RSSI values from Bluetooth beacons (estimote to be specific) in Linux?

I need to get distances from multiple bluetooth beacons for triangulation so that I can do indoor positioning.
I have tried to give as many relevant details as possible. This project is for an RPi, but I am currently trying to test it through my laptop first. I have tried most of what I can find on the internet about this:
The best way I could find to get the rssi values was hcitool rssi <address of beacon>
From here Bluetooth LE Signal Strength Linux
To test this, I tried to get it for my mobile first. It showed "Not Connected". I thought I need to connect first, so I tried many ways to connect my mobile:
a) hcittol cc <address of mobile>
b) gatttool -b <address of mobile> -t random --interactive
c) rfcomm connect 0 <address of mobile> 10
In each case, when I clicked pair in my mobile, connection terminated.
Then, I paired my mobile the usual way from bluetooth menu, and then tried hcitool rssi <Address of mobile> which then finally returned a value -8.
Now that I had it for my mobile, I now moved to beacons.
Then, I tried the same for my beacons. This time I had to
first do hcitool lescan to get addresses of available beacons
then connect to one of the beacons using the command hcitool lecc <address of beacon> which returned the error Could not create connection: Connection timed out
So next, I again tried connecting with the other two ways:
rfcomm connect 0 <address of beacon> 10 which returned the error Can't connect RFCOMM socket: Host is down when I can be sure hci0 was up.
Then I tried with sudo gatttool -b F1:15:A7:E3:17:63 --interactive after which interactive mode opened up and I gave the command connect. This gave different errors after Attempting to connect to <address of beacon>,
mainly connect error : Connection timed out
and Error: connect error: Connection refused (111)
Then I read I need to use -t random from Bluetooth LE on Raspbian , https://ubuntuforums.org/showthread.php?t=2204808 and one another source which I can't find right now.
So I entered sudo gatttool -b F1:15:A7:E3:17:63 -t random --interactive in which when I gave the connect command, I got error Error: connect error: Device or resource busy (16) and also sometimes Error: connect error: Input/Output Error
then I read that I perhaps need to restart my hci0 by hciconfig hci0 down and then hciconfig hci0 up which did the trick, the beacon now got connected.
But then, it quickly got disconnected after saying `GLib-WARNING **: Invalid file descriptor.
`
I read here https://raspberrypi.stackexchange.com/questions/53982/how-to-fix-gatttools-glib-warning-invalid-file-descriptor that this is because of poor connections. I tried keeping the beacons closer, but it did the same.
So I tried to anyway at least get one rssi value during the short duration it is connected. But, even when it was thus connected, I did hcitool rssi <address of beacon> the error came like this:
hcitool rssi F1:15:A7:E3:17:63
Get connection info failed: No such file or directory
To which I haven't yet found a proper solution.
I tried another way of reading the rssi value, which gave the following :
hcidump -R
HCI sniffer - Bluetooth packet analyzer ver 5.37
device: hci0 snap_len: 1500 filter: 0xffffffffffffffff
> 04 0E 04 01 0B 20 00
> 04 0E 04 01 0C 20 00
> 04 0E 04 01 0C 20 00
> 04 0F 04 00 01 0D 20
> 04 0F 04 00 01 16 20
> 04 0F 04 00 01 13 20
This was before it got disconnected.
I read here Obtain RSSI with hcidump which said to read the 14th byte. since all these seemed to be hex numbers, this would mean that each number is 1 byte. But this way, I only have 7 numbers per line, how can I read the 14th byte?
Getting distance from beacons has been troubling me a lot, and has used up a a lot of my time. Please help me out on this issue. If you can help me find a resource to help me do so, I'd appreciate the help.
You don't need to connect to a device to get the signal strength RSSI values, just use the following code and pipe that to a script that extracts values from the devices you are after:
btmon &
hcitool lescan --duplicates

iBeacon Dynamic Minor Value

I am using a Raspberry Pi as an iBeacon by creating a LaunchService that runs on startup executing the following (using BlueZ):
sudo hciconfig hci0 up
sudo hcitool -i hci0 cmd 0x08 0x0006 40 [...] 00
sudo hcitool -i hci0 cmd 0x08 0x000A 01
sudo hcitool -i hci0 cmd 0x08 0x0008 1E [...] 02 C0 00 00 00 00 C5 00
So far this has worked beautifully. The service runs and as long as the Pi is powered, the iBeacon is advertised.
Now I would like to send some Information with the advertising data. I have a brightness sensor wired up to the Pi that simply returns true if a certain threshold of brightness has been exceeded.
My idea is to use the Minor identifier and use 1 to represent "it is bright" and 0to represent "it is dark". My app can then interpret this without having to poll the Pi via a network request what the current sensor status is.
But since I set the advertisement package statically, I won't be able to use this approach going forward I believe.
I have no idea how else to do this however, so I was hoping someone might have some insight and could point me in the right direction.
Thanks for your time!
Easy. You can simply re-issue those commands, specifically the last one:
sudo hcitool -i hci0 cmd 0x08 0x0008 1E [...] 02 C0 00 00 00 00 C5 00
Just change the last byte before the C5 above to be 01 if you want to send out a minor of 1, and change it back to 00 if you want to send out a minor of 0.

Gatttool non-interactive mode --char-write

Still asking about BLE devices and gatttool.
I'm able to send a char-write-cmd in interactive mode, but I'm not able to do the same in non-interactive.
This is what I send in interactive mode :
gatttool -I
[]> connect BTADDR
[BTADDR]> char-write-cmd 0x0040 01
[BTADDR]> exit
In this way I start the Alert service, which in my case makes the buzzer sounds.
In theory, the non-interactive mode should be:
gatttool -b BTADDR --char-write -a 0x0040 -n 01
But this do not send the request command event to the board, I'm checking it using a dev board.
The manual (non-interactive) way to read or write to your BLE peripheral:
To write and receive reply once: (depending on how you configure your BLE device)
sudo gatttool -i hci0 -b xx:xx:xx:xx:xx:xx --char-write-req -a 0x0025 -n ff
To write and receive reply indefinitely: (Until you disconnect from BLE device or stop your Bluetooth client)
sudo gatttool -i hci0 -b xx:xx:xx:xx:xx:xx --char-write-req -a 0x0025 -n ff --listen
Notice I only added the --listen option at the end. This mechanism only works if you configure your BLE node (sensor or actuator) to read and reply.
sources::
How to use gatttool non-interactive mode
Bluetooth Low Energy: listening for notifications/indications in linux
All that said, I still do not think it is best to control or get values from a BLE device. My direction is to move forward and use an API (maybe in python) to do the job for you.
https://github.com/peplin/pygatt
This API has been proven to work with Raspberry Pi Jessie destro.

Zengge BLE bulb protocol

I've just got a smart led bulb from Zengge and want to control it from my Linux machine with Bluetooth 4.0. I've installed latest bluez 5.x package, so I have a gatttool available. I am able to connect to my bulb and list it's services. But I have no ideas how to control it. Maybe someone have already done that (I was unable to Google for ready solution)? Or have some developments in this area? Or maybe someone have the specs?
For those who are interested, here are my findings about the bulb protocol.
1) Check if your bluetooth device is up: hcitool dev
2) If device is down: hciconfig hci0 up
3) Scan for bluetooth le devices: hcitool lescan
4) Turn on device: gatttool -b YOUR:LE:DEVICE:MAC:ADDRESS --char-write-req -a 0x001a -n 04
5) Turn off device: gatttool -b YOUR:LE:DEVICE:MAC:ADDRESS --char-write-req -a 0x001a -n 63
6) Dim warm light: gatttool -b YOUR:LE:DEVICE:MAC:ADDRESS --char-write-req -a 0x0013 -n 56000000ff0faa where ff is the hex code for the intensity.
7) Colored light: gatttool -b YOUR:LE:DEVICE:MAC:ADDRESS --char-write-req -a 0x0013 -n 56e92fff99f9aa where e9 is hex red intensity, 2f is hex green intensity, 2f is hex blue intensity
(I'v got the codes by turning on the Bluetooth HCI-snoop-log in Android wile using the Magic Light app and analysing it with Wireshark)

Resources