Debugging Tips

Probably the most important section of this guide, I'll show you what to do when your code isn't working as you expected. With BlueZ, D-Bus, and our own code in play, the first thing to figure out is which component is the problem...
Debugging D-Bus
The first thing to do is to make sure that D-Bus knows about your application. Use busctl
to see if services are added when your application starts up.
sudo watch --interval 1 busctl list
If you service is showing up, the you need to inspect the messages that your application is putting on D-Bus and sending to BlueZ, as well as those that BlueZ is sending your way:
sudo apt-get install libglib2.0-bin
sudo gdbus introspect -y -d "org.bluez" -o "/org/bluez/hci0"
Here's an example, where I hadn't yet implemented the GetManagedObjects
method yet. Note that 1.99
is the identifier D-Bus was using for my application:
error time=1637951174.380515 sender=:1.99 -> destination=:1.5 error_name=org.freedesktop.DBus.Error.UnknownMethod reply_serial=382
string "No such method “GetManagedObjects”"
most of the time gdbus
will point you right to the problem. However, there are some errors that BlueZ generates, but doesn't put much detail about why back on D-Bus. If the error you're seeing in D-Bus isn't helpful, we need to go a layer deeper.
Debugging BlueZ
tail -f /var/log/syslog | grep bluetoothd
You might see something like this in the syslog:
bluetoothd[514]: error("Failed to obtain service path for characteristic");
If see those, go to the /src
directory in BlueZ's code Search the files there for the error message you're seeing. You may have to read some c-lang code, but you should be able to pin-point what checks are failing and causing BlueZ to error out.
A Reference Central
It's incredibly helpful to have a central that you know works. I downloaded an app called BLE Hero that lets you visualize and connect to all the BLE peripherals that are around you. If you can't see or connect to your app with BLE Hero something is wrong.
With these tools under our control, we should be able to troubleshoot any problems that come up with our application. Now that our peripheral thermometer application up and running, we can work on the central.