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"
21 BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
22 QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this)),audio(NULL)
24 if(!m_device->isValid())
26 QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
27 this,SLOT(propertyChanged(QString,QDBusVariant)));
30 void BluetoothDevice::unpair()
32 OrgBluezManagerInterface manager(
34 "/", QDBusConnection::systemBus());
36 QDBusObjectPath adapterpath = manager.DefaultAdapter();
38 if(adapterpath.path().isEmpty()) return;
40 OrgBluezAdapterInterface adapter(
43 QDBusConnection::systemBus());
45 adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
48 void BluetoothDevice::connectAudio()
50 if(isProfileSupported(BluetoothProfiles::a2sink) || isProfileSupported(BluetoothProfiles::hs))
52 if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
56 connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
59 void BluetoothDevice::connectAudioSrc()
62 if(isProfileSupported(BluetoothProfiles::a2src))
64 OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
65 QDBusConnection::systemBus());
70 QString BluetoothDevice::connectSerial()
72 if(isProfileSupported(BluetoothProfiles::spp))
74 QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
75 QDBusReply<QString> reply = interface.call(QDBus::AutoDetect, "Connect","spp");
77 if(reply.isValid()) return reply;
78 else qDebug()<<"Error connecting spp profile: "<<reply.error().message();
84 void BluetoothDevice::disconnect()
86 m_device->Disconnect();
89 void BluetoothDevice::disconnectAudio()
91 if(!audio) audio = new OrgBluezAudioInterface("org.bluez", m_device->path(), QDBusConnection::systemBus(), this);
96 QStringList BluetoothDevice::profiles()
98 QVariantMap props = m_device->GetProperties();
100 QStringList uuidlist = props["UUIDs"].toStringList();
105 bool BluetoothDevice::isProfileSupported(QString profile)
107 QVariantMap props = m_device->GetProperties();
109 QStringList uuidlist = props["UUIDs"].toStringList();
111 return uuidlist.contains(profile.toLower());
114 bool BluetoothDevice::connected()
116 QVariantMap props = m_device->GetProperties();
117 return props["Connected"].toBool();
120 bool BluetoothDevice::audioConnected()
124 audio = new OrgBluezAudioInterface("org.bluez",m_device->path(), QDBusConnection::systemBus(),this);
125 connect(audio,SIGNAL(PropertyChanged(QString,QDBusVariant)),this,SLOT(audioPropertiesChanged(QString,QDBusVariant)));
128 QVariantMap props = audio->GetProperties();
129 return props["State"].toString() == "connected";
132 QString BluetoothDevice::alias()
134 QVariantMap props = m_device->GetProperties();
135 return props["Alias"].toString();
138 QString BluetoothDevice::name()
140 QVariantMap props = m_device->GetProperties();
141 return props["Name"].toString();
144 QString BluetoothDevice::address()
146 QVariantMap props = m_device->GetProperties();
147 return props["Address"].toString();
150 QString BluetoothDevice::icon()
152 QVariantMap props = m_device->GetProperties();
153 return props["Icon"].toString();
156 QString BluetoothDevice::path()
158 return m_device->path();
161 void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
163 emit propertyChanged(name,value.variant());
165 if(name == "Connected")
167 emit connectedChanged(value.variant().toBool());
169 ///TODO: create individual signals for each property
172 void BluetoothDevice::audioPropertiesChanged(QString name,QDBusVariant value)
174 emit propertyChanged(name, value.variant());
178 emit audioConnectedChanged(value.variant().toString() == "connected");