3 * meego handset bluetooth
4 * Copyright © 2010, Intel Corporation.
6 * This program is licensed under the terms and conditions of the
7 * Apache License, version 2.0. The full text of the Apache License is at
8 * http://www.apache.org/licenses/LICENSE-2.0
12 #include "bluetoothdevice.h"
13 #include "blueadapter.h"
14 #include "bluemanager.h"
15 #include "btprofiles.h"
16 #include "audiosink.h"
17 #include "audiosource.h"
22 BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
23 QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this)),audio(NULL),input(NULL)
25 if(!m_device->isValid())
27 QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
28 this,SLOT(propertyChanged(QString,QDBusVariant)));
31 void BluetoothDevice::unpair()
33 OrgBluezManagerInterface manager(
35 "/", QDBusConnection::systemBus());
37 QDBusObjectPath adapterpath = manager.DefaultAdapter();
39 if(adapterpath.path().isEmpty()) return;
41 OrgBluezAdapterInterface adapter(
44 QDBusConnection::systemBus());
46 adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
49 void BluetoothDevice::connectAudio()
51 if(isProfileSupported(BluetoothProfiles::a2sink) || isProfileSupported(BluetoothProfiles::hs))
53 if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
57 connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
60 void BluetoothDevice::connectAudioSrc()
63 if(isProfileSupported(BluetoothProfiles::a2src))
65 OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
66 QDBusConnection::systemBus());
71 QString BluetoothDevice::connectSerial()
73 if(isProfileSupported(BluetoothProfiles::spp))
75 QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
76 QDBusReply<QString> reply = interface.call(QDBus::AutoDetect, "Connect","spp");
78 if(reply.isValid()) return reply;
79 else qDebug()<<"Error connecting spp profile: "<<reply.error().message();
85 void BluetoothDevice::connectInput()
87 if(isProfileSupported(BluetoothProfiles::hid))
91 input = new OrgBluezInputInterface("org.bluez", m_device->path(),
92 QDBusConnection::systemBus(), this);
93 connect(input,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,
94 SLOT(inputPropertiesChanged(QString,QDBusVariant)));
100 void BluetoothDevice::disconnect()
102 m_device->Disconnect();
105 void BluetoothDevice::disconnectAudio()
107 if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
112 QStringList BluetoothDevice::profiles()
114 QVariantMap props = m_device->GetProperties();
116 QStringList uuidlist = props["UUIDs"].toStringList();
121 bool BluetoothDevice::isProfileSupported(QString profile)
123 QVariantMap props = m_device->GetProperties();
125 QStringList uuidlist = props["UUIDs"].toStringList();
127 return uuidlist.contains(profile.toLower());
130 bool BluetoothDevice::connected()
132 QVariantMap props = m_device->GetProperties();
133 return props["Connected"].toBool();
136 bool BluetoothDevice::audioConnected()
140 audio = new OrgBluezAudioInterface("org.bluez",m_device->path(), QDBusConnection::systemBus(),this);
141 connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
144 QVariantMap props = audio->GetProperties();
145 return props["State"].toString() == "connected";
148 bool BluetoothDevice::inputConnected()
152 input = new OrgBluezInputInterface("org.bluez",m_device->path(), QDBusConnection::systemBus(),this);
153 connect(input,SIGNAL(PropertyChanged(QString,QDBusVariant)),this, SLOT(inputPropertiesChanged(QString,QDBusVariant)));
156 QVariantMap props = input->GetProperties();
157 return props["Connected"].toBool();
160 QString BluetoothDevice::alias()
162 QVariantMap props = m_device->GetProperties();
163 return props["Alias"].toString();
166 QString BluetoothDevice::name()
168 QVariantMap props = m_device->GetProperties();
169 return props["Name"].toString();
172 QString BluetoothDevice::address()
174 QVariantMap props = m_device->GetProperties();
175 return props["Address"].toString();
178 QString BluetoothDevice::icon()
180 QVariantMap props = m_device->GetProperties();
181 return props["Icon"].toString();
184 QString BluetoothDevice::path()
186 return m_device->path();
189 void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
191 emit propertyChanged(name,value.variant());
193 if(name == "Connected")
195 emit connectedChanged(value.variant().toBool());
197 ///TODO: create individual signals for each property
200 void BluetoothDevice::audioPropertiesChanged(QString name,QDBusVariant value)
202 ///don't think i need to be doing this:
203 //emit propertyChanged(name, value.variant());
207 emit audioConnectedChanged(value.variant().toString() == "connected");
211 void BluetoothDevice::inputPropertiesChanged(QString name, QDBusVariant value)
213 qDebug()<<"Input properties changed: "<<name<<" "<<value.variant();
214 if(name == "Connected")
216 emit inputConnectedChanged(value.variant().toBool());