DBus Introspection Example with Bluez

DBus Introspection Example with Bluez

The Qt DBus bindings are incredibly useful. Given a DBus introspection file like the following for Bluez ( http://www.bluez.org/ ) - Bluetooth for Linux, you can easily convert it to a Qt DBus interface class with the command:

qdbusxml2cpp -p headset.h:headset.cpp -i devicetypes.h xml/headset.xml

Bluez Introspection file for the Headset object:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"



<interface name="org.bluez.Headset">

<method name="Connect"/>

<method name="Disconnect"/>

<method name="IsConnected">

<arg type="b" direction="out"/>


<method name="IndicateCall"/>

<method name="CancelCall"/>

<method name="Play"/>

<method name="Stop"/>

<method name="IsPlaying">

<arg type="b" direction="out"/>


<method name="GetSpeakerGain">

<arg type="q" direction="out"/>


<method name="GetMicrophoneGain">

<arg type="q" direction="out"/>


<method name="SetSpeakerGain">

<arg type="q" direction="in"/>


<method name="SetMicrophoneGain">

<arg type="q" direction="in"/>


<method name="GetProperties">

<arg type="a{sv}" direction="out"/>

<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QVariantMap"/>


<method name="SetProperty">

<arg type="s" direction="in"/>

<arg type="v" direction="in"/>


<signal name="Connected"/>

<signal name="Disconnected"/>

<signal name="AnswerRequested"/>

<signal name="Stopped"/>

<signal name="Playing"/>

<signal name="SpeakerGainChanged">

<arg type="q"/> </signal>

<signal name="MicrophoneGainChanged">

<arg type="q"/>


<signal name="CallTerminated"/>

<signal name="PropertyChanged">

<arg type="s"/>

<arg type="v"/>




Once generated, add the headset class source files to your project and instantiate a Headset with something like the following:

m_Headset = new OrgBluezHeadsetInterface(BLUEZ_SERVICE_NAME,pairedHeadsetPath.path(),QDBusConnection::systemBus(), this);

No you can easily do cool stuff like Connect and Play audio through a paired headset:

if (!m_Headset->isValid()) {


else {

// Now connect to the headset

QDBusPendingCall connectOperation = m_Headset->Connect();


// Determine if we are connected

bool checkConnection = m_Headset->IsConnected();

if ( checkConnection ) {

// Now play the audio


// Finally, send audio to the bluetooth headset


// Success

return 0;


else {

// Error

return -1;




Happy Coding!

ClassyBits 2016