1 #include "bluetoothdevice.h"
2 #include "blueadapter.h"
3 #include "bluemanager.h"
4 #include "btprofiles.h"
6 #include "audiosource.h"
9 BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
10 QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this))
12 if(!m_device->isValid())
14 QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
15 this,SLOT(propertyChanged(QString,QDBusVariant)));
18 void BluetoothDevice::unpair()
20 OrgBluezManagerInterface manager(
22 "/", QDBusConnection::systemBus());
24 QDBusObjectPath adapterpath = manager.DefaultAdapter();
26 if(adapterpath.path().isEmpty()) return;
28 OrgBluezAdapterInterface adapter(
31 QDBusConnection::systemBus());
33 adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
36 void BluetoothDevice::connect(QString profile)
38 if(profile == BluetoothProfiles::a2sink)
40 OrgBluezAudioSinkInterface sink("org.bluez",m_device->path(),
41 QDBusConnection::systemBus());
44 else if(profile == BluetoothProfiles::a2src)
46 OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
47 QDBusConnection::systemBus());
50 else if(profile == BluetoothProfiles::hs)
52 OrgBluezHeadsetInterface headset("org.bluez",m_device->path(),
53 QDBusConnection::systemBus());
56 else if(profile == BluetoothProfiles::spp)
58 QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
59 interface.call(QDBus::AutoDetect, "Connect","spp");
63 void BluetoothDevice::disconnect()
65 m_device->Disconnect();
68 QStringList BluetoothDevice::profiles()
70 QVariantMap props = m_device->GetProperties();
72 QStringList uuidlist = props["UUIDs"].toStringList();
77 bool BluetoothDevice::isProfileSupported(QString profile)
79 QVariantMap props = m_device->GetProperties();
81 QStringList uuidlist = props["UUIDs"].toStringList();
83 return uuidlist.contains(profile.toLower());
86 bool BluetoothDevice::connected()
88 QVariantMap props = m_device->GetProperties();
89 return props["Connected"].toBool();
92 QString BluetoothDevice::connectedProfile()
94 foreach(QString profile, profiles())
96 if(profile == BluetoothProfiles::a2sink)
98 OrgBluezAudioSinkInterface sink("org.bluez",m_device->path(),
99 QDBusConnection::systemBus());
100 if(sink.IsConnected())
101 return BluetoothProfiles::a2sink;
103 else if(profile == BluetoothProfiles::a2src)
105 OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
106 QDBusConnection::systemBus());
107 QVariantMap props = source.GetProperties();
109 if(props["State"].toString() == "connected")
111 return BluetoothProfiles::a2src;
115 else if(profile == BluetoothProfiles::hs)
117 OrgBluezHeadsetInterface headset("org.bluez",m_device->path(),
118 QDBusConnection::systemBus());
119 if(headset.IsConnected())
121 return BluetoothProfiles::hs;
127 QString BluetoothDevice::alias()
129 QVariantMap props = m_device->GetProperties();
130 return props["Alias"].toString();
133 QString BluetoothDevice::name()
135 QVariantMap props = m_device->GetProperties();
136 return props["Name"].toString();
139 QString BluetoothDevice::address()
141 QVariantMap props = m_device->GetProperties();
142 return props["Address"].toString();
145 QString BluetoothDevice::icon()
147 QVariantMap props = m_device->GetProperties();
148 return props["Icon"].toString();
151 QString BluetoothDevice::path()
153 return m_device->path();
156 void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
158 if(name == "Connected")
160 emit connetedChanged();