initial Bluetooth Device Model addition
[profile/ivi/bluetooth-qt.git] / bluetoothdevice.cpp
1 #include "bluetoothdevice.h"
2 #include "blueadapter.h"
3 #include "bluemanager.h"
4 #include "btprofiles.h"
5 #include "audiosink.h"
6 #include "audiosource.h"
7 #include "headset.h"
8
9 BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
10                 QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this))
11 {
12         if(!m_device->isValid())
13                 return;
14         QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
15                                          this,SLOT(propertyChanged(QString,QDBusVariant)));
16 }
17
18 void BluetoothDevice::unpair()
19 {
20         OrgBluezManagerInterface manager(
21                         "org.bluez",
22                         "/", QDBusConnection::systemBus());
23
24         QDBusObjectPath adapterpath = manager.DefaultAdapter();
25
26         if(adapterpath.path().isEmpty()) return;
27
28         OrgBluezAdapterInterface adapter(
29                         "org.bluez",
30                         adapterpath.path(),
31                         QDBusConnection::systemBus());
32
33         adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
34 }
35
36 void BluetoothDevice::connect(QString profile)
37 {
38         if(profile == BluetoothProfiles::a2sink)
39         {
40                 OrgBluezAudioSinkInterface sink("org.bluez",m_device->path(),
41                                                                                 QDBusConnection::systemBus());
42                 sink.Connect();
43         }
44         else if(profile == BluetoothProfiles::a2src)
45         {
46                 OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
47                                                                                 QDBusConnection::systemBus());
48                 source.Connect();
49         }
50         else if(profile == BluetoothProfiles::hs)
51         {
52                 OrgBluezHeadsetInterface headset("org.bluez",m_device->path(),
53                                                                                 QDBusConnection::systemBus());
54                 headset.Connect();
55         }
56         else if(profile == BluetoothProfiles::spp)
57         {
58                 QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
59                 interface.call(QDBus::AutoDetect, "Connect","spp");
60         }
61 }
62
63 void BluetoothDevice::disconnect()
64 {
65         m_device->Disconnect();
66 }
67
68 QStringList BluetoothDevice::profiles()
69 {
70         QVariantMap props = m_device->GetProperties();
71
72         QStringList uuidlist = props["UUIDs"].toStringList();
73
74         return uuidlist;
75 }
76
77 bool BluetoothDevice::isProfileSupported(QString profile)
78 {
79         QVariantMap props = m_device->GetProperties();
80
81         QStringList uuidlist = props["UUIDs"].toStringList();
82
83         return uuidlist.contains(profile.toLower());
84 }
85
86 bool BluetoothDevice::connected()
87 {
88         QVariantMap props = m_device->GetProperties();
89         return props["Connected"].toBool();
90 }
91
92 QString BluetoothDevice::connectedProfile()
93 {
94         foreach(QString profile, profiles())
95         {
96                 if(profile == BluetoothProfiles::a2sink)
97                 {
98                         OrgBluezAudioSinkInterface sink("org.bluez",m_device->path(),
99                                                                                         QDBusConnection::systemBus());
100                         if(sink.IsConnected())
101                                 return BluetoothProfiles::a2sink;
102                 }
103                 else if(profile == BluetoothProfiles::a2src)
104                 {
105                         OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
106                                                                                         QDBusConnection::systemBus());
107                         QVariantMap props = source.GetProperties();
108
109                         if(props["State"].toString() == "connected")
110                         {
111                                 return BluetoothProfiles::a2src;
112                         }
113
114                 }
115                 else if(profile == BluetoothProfiles::hs)
116                 {
117                         OrgBluezHeadsetInterface headset("org.bluez",m_device->path(),
118                                                                                         QDBusConnection::systemBus());
119                         if(headset.IsConnected())
120                         {
121                                 return BluetoothProfiles::hs;
122                         }
123                 }
124         }
125 }
126
127 QString BluetoothDevice::alias()
128 {
129         QVariantMap props = m_device->GetProperties();
130         return props["Alias"].toString();
131 }
132
133 QString BluetoothDevice::name()
134 {
135         QVariantMap props = m_device->GetProperties();
136         return props["Name"].toString();
137 }
138
139 QString BluetoothDevice::address()
140 {
141         QVariantMap props = m_device->GetProperties();
142         return props["Address"].toString();
143 }
144
145 QString BluetoothDevice::icon()
146 {
147         QVariantMap props = m_device->GetProperties();
148         return props["Icon"].toString();
149 }
150
151 QString BluetoothDevice::path()
152 {
153         return m_device->path();
154 }
155
156 void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
157 {
158         if(name == "Connected")
159         {
160                 emit connetedChanged();
161         }
162 }