nearbydevicesmodel.h \
bluetoothbaseagent.h \
asyncagent.h \
- devicetypes.h
+ devicetypes.h \
+ bluetoothdevice.h \
+ bluetoothdevicemodel.h
SOURCES += agentadaptor.cpp \
bluemanager.cpp \
bluedevice.cpp \
headset.cpp \
nearbydevicesmodel.cpp \
bluetoothbaseagent.cpp \
- asyncagent.cpp
+ asyncagent.cpp \
+ bluetoothdevice.cpp \
+ bluetoothdevicemodel.cpp
target.path = $$INSTALL_ROOT/usr/lib
headers.path = $$INSTALL_ROOT/usr/include/bluetooth-qt/
--- /dev/null
+#include "bluetoothdevice.h"
+#include "blueadapter.h"
+#include "bluemanager.h"
+#include "btprofiles.h"
+#include "audiosink.h"
+#include "audiosource.h"
+#include "headset.h"
+
+BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
+ QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this))
+{
+ if(!m_device->isValid())
+ return;
+ QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
+ this,SLOT(propertyChanged(QString,QDBusVariant)));
+}
+
+void BluetoothDevice::unpair()
+{
+ OrgBluezManagerInterface manager(
+ "org.bluez",
+ "/", QDBusConnection::systemBus());
+
+ QDBusObjectPath adapterpath = manager.DefaultAdapter();
+
+ if(adapterpath.path().isEmpty()) return;
+
+ OrgBluezAdapterInterface adapter(
+ "org.bluez",
+ adapterpath.path(),
+ QDBusConnection::systemBus());
+
+ adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
+}
+
+void BluetoothDevice::connect(QString profile)
+{
+ if(profile == BluetoothProfiles::a2sink)
+ {
+ OrgBluezAudioSinkInterface sink("org.bluez",m_device->path(),
+ QDBusConnection::systemBus());
+ sink.Connect();
+ }
+ else if(profile == BluetoothProfiles::a2src)
+ {
+ OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
+ QDBusConnection::systemBus());
+ source.Connect();
+ }
+ else if(profile == BluetoothProfiles::hs)
+ {
+ OrgBluezHeadsetInterface headset("org.bluez",m_device->path(),
+ QDBusConnection::systemBus());
+ headset.Connect();
+ }
+ else if(profile == BluetoothProfiles::spp)
+ {
+ QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
+ interface.call(QDBus::AutoDetect, "Connect","spp");
+ }
+}
+
+void BluetoothDevice::disconnect()
+{
+ m_device->Disconnect();
+}
+
+QStringList BluetoothDevice::profiles()
+{
+ QVariantMap props = m_device->GetProperties();
+
+ QStringList uuidlist = props["UUIDs"].toStringList();
+
+ return uuidlist;
+}
+
+bool BluetoothDevice::isProfileSupported(QString profile)
+{
+ QVariantMap props = m_device->GetProperties();
+
+ QStringList uuidlist = props["UUIDs"].toStringList();
+
+ return uuidlist.contains(profile.toLower());
+}
+
+bool BluetoothDevice::connected()
+{
+ QVariantMap props = m_device->GetProperties();
+ return props["Connected"].toBool();
+}
+
+QString BluetoothDevice::connectedProfile()
+{
+ foreach(QString profile, profiles())
+ {
+ if(profile == BluetoothProfiles::a2sink)
+ {
+ OrgBluezAudioSinkInterface sink("org.bluez",m_device->path(),
+ QDBusConnection::systemBus());
+ if(sink.IsConnected())
+ return BluetoothProfiles::a2sink;
+ }
+ else if(profile == BluetoothProfiles::a2src)
+ {
+ OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
+ QDBusConnection::systemBus());
+ QVariantMap props = source.GetProperties();
+
+ if(props["State"].toString() == "connected")
+ {
+ return BluetoothProfiles::a2src;
+ }
+
+ }
+ else if(profile == BluetoothProfiles::hs)
+ {
+ OrgBluezHeadsetInterface headset("org.bluez",m_device->path(),
+ QDBusConnection::systemBus());
+ if(headset.IsConnected())
+ {
+ return BluetoothProfiles::hs;
+ }
+ }
+ }
+}
+
+QString BluetoothDevice::alias()
+{
+ QVariantMap props = m_device->GetProperties();
+ return props["Alias"].toString();
+}
+
+QString BluetoothDevice::name()
+{
+ QVariantMap props = m_device->GetProperties();
+ return props["Name"].toString();
+}
+
+QString BluetoothDevice::address()
+{
+ QVariantMap props = m_device->GetProperties();
+ return props["Address"].toString();
+}
+
+QString BluetoothDevice::icon()
+{
+ QVariantMap props = m_device->GetProperties();
+ return props["Icon"].toString();
+}
+
+QString BluetoothDevice::path()
+{
+ return m_device->path();
+}
+
+void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
+{
+ if(name == "Connected")
+ {
+ emit connetedChanged();
+ }
+}
--- /dev/null
+#ifndef BLUETOOTHDEVICE_H
+#define BLUETOOTHDEVICE_H
+
+#include <QObject>
+#include <QStringList>
+#include <bluedevice.h>
+/*
+#include <audiosource.h>
+#include <audiosink.h>
+#include <headset.h>
+*/
+class BluetoothDevice : public QObject
+{
+ Q_OBJECT
+ /*Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
+ Q_PROPERTY(QString alias READ alias)
+ Q_PROPERTY(QString name READ name)
+ Q_PROPERTY(QString address READ address)
+ Q_PROPERTY(QString icon READ icon)
+ Q_PROPERTY(QString path READ path)
+ Q_PROPERTY(QString connectedProfile READ connectedProfile)*/
+
+public:
+ explicit BluetoothDevice(QDBusObjectPath path, QObject *parent = 0);
+
+signals:
+ void connetedChanged();
+
+public slots:
+ void unpair();
+ void connect(QString profile);
+ void disconnect();
+
+ QStringList profiles();
+ bool isProfileSupported(QString profile);
+ QString connectedProfile();
+
+ ///properties:
+ bool connected();
+ QString alias();
+ QString name();
+ QString address();
+ QString icon();
+ QString path();
+
+private slots:
+ void propertyChanged(QString name,QDBusVariant value);
+
+private:
+ OrgBluezDeviceInterface *m_device;
+};
+
+#endif // BLUETOOTHDEVICE_H
--- /dev/null
+#include "bluetoothdevicemodel.h"
+
+BluetoothDeviceModel::BluetoothDeviceModel(QObject *parent) :
+ QAbstractListModel(parent), adapter(NULL)
+{
+ manager = new OrgBluezManagerInterface(
+ "org.bluez",
+ "/", QDBusConnection::systemBus(), this);
+
+ connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
+ connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
+ adapterAdded(QDBusObjectPath());
+
+ QHash<int, QByteArray> roles;
+ roles[name]="name";
+ roles[address]="address";
+ roles[path]="path";
+
+ setRoleNames(roles);
+}
+
+int BluetoothDeviceModel::rowCount(const QModelIndex &parent) const
+{
+ return m_devices.size();
+}
+
+QVariant BluetoothDeviceModel::data(const QModelIndex &index, int role) const
+{
+ if(role == name)
+ {
+ QString rowData;
+ if(index.row() < m_devices.size())
+ {
+ rowData = m_devices[index.row()]->name();
+ }
+ return QVariant(rowData);
+ }
+ else if(role == address)
+ {
+ QString rowData;
+ if(index.row() < m_devices.size())
+ {
+ rowData = m_devices[index.row()]->address();
+ }
+ return QVariant(rowData);
+ }
+ else if(role == path)
+ {
+ QString rowData;
+ if(index.row() < m_devices.size())
+ {
+ rowData = m_devices[index.row()]->path();
+ }
+ return QVariant(rowData);
+ }
+ return QVariant();
+}
+
+void BluetoothDeviceModel::adapterAdded(QDBusObjectPath path)
+{
+ if(adapter && adapter->path() == path.path()) return;
+
+ QDBusObjectPath adapterpath = manager->DefaultAdapter();
+
+ if(adapterpath.path() == "")
+ return;
+
+ adapter = new OrgBluezAdapterInterface(
+ "org.bluez",
+ adapterpath.path(),
+ QDBusConnection::systemBus(), this);
+
+ connect(adapter,
+ SIGNAL(DeviceRemoved(QDBusObjectPath)),
+ this,
+ SIGNAL(deviceRemoved(QDBusObjectPath)));
+
+ connect(adapter,
+ SIGNAL(DeviceCreated(QDBusObjectPath)),
+ this,
+ SIGNAL(deviceCreated(QDBusObjectPath)));
+
+ QList<QDBusObjectPath> list = adapter->ListDevices();
+ foreach(QDBusObjectPath item, list)
+ {
+ m_devices.append(new BluetoothDevice(item));
+ }
+}
+
+void BluetoothDeviceModel::adapterRemoved(QDBusObjectPath)
+{
+ QDBusObjectPath adapterpath = manager->DefaultAdapter();
+
+ if(adapterpath.path() == "")
+ {
+ beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
+ foreach(BluetoothDevice* device, m_devices)
+ {
+ delete device;
+ }
+ m_devices.clear();
+ endRemoveRows();
+
+ if(adapter) delete adapter;
+ return;
+ }
+}
+
+void BluetoothDeviceModel::deviceCreated(QDBusObjectPath devicepath)
+{
+
+ beginInsertRows(QModelIndex(),m_devices.size()+1,m_devices.size()+1);
+ m_devices.append(new BluetoothDevice(devicepath));
+ endInsertRows();
+}
+
+void BluetoothDeviceModel::deviceRemoved(QDBusObjectPath devicepath)
+{
+ for(int i=0; i<m_devices.size(); i++)
+ {
+
+ if(m_devices[i]->path() == devicepath.path())
+ {
+ beginRemoveRows(QModelIndex(), i, i);
+ delete m_devices[i];
+ m_devices.removeAt(i);
+ endRemoveRows();
+ }
+ }
+}
--- /dev/null
+#ifndef BLUETOOTHDEVICEMODEL_H
+#define BLUETOOTHDEVICEMODEL_H
+
+#include <QAbstractListModel>
+#include "bluemanager.h"
+#include "blueadapter.h"
+#include "bluetoothdevice.h"
+
+class BluetoothDeviceModel : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ explicit BluetoothDeviceModel(QObject *parent = 0);
+
+ enum Role
+ {
+ name = Qt::UserRole + 1,
+ address,
+ path
+ };
+
+public slots:
+
+ int columnCount(const QModelIndex &) const { return 1; }
+ int rowCount(const QModelIndex &parent = QModelIndex()) const;
+ QVariant data(const QModelIndex &index, int role) const;
+
+ void adapterAdded(QDBusObjectPath);
+ void adapterRemoved(QDBusObjectPath);
+ void deviceCreated(QDBusObjectPath);
+ void deviceRemoved(QDBusObjectPath);
+
+ QList<BluetoothDevice*> devices(){ return m_devices; }
+
+private:
+ OrgBluezManagerInterface *manager;
+ OrgBluezAdapterInterface *adapter;
+ QList<BluetoothDevice*> m_devices;
+};
+
+#endif // BLUETOOTHDEVICEMODEL_H
#ifndef BTPROFILES_H
#define BTPROFILES_H
+#include <QMap>
+
namespace BluetoothProfiles
{
-const char a2sink[] = "0000110b-0000-1000-8000-00805f9b34fb";
-const char a2src[] = "0000110a-0000-1000-8000-00805f9b34fb";
-const char syncml[] = "00005601-0000-1000-8000-0002ee000001";
-const char hf[] = "0000111e-0000-1000-8000-00805f9b34fb";
-const char hfag[] = "0000111f-0000-1000-8000-00805f9b34fb";
-const char hs[] = "00001108-0000-1000-8000-00805f9b34fb";
-const char hsag[] = "00001112-0000-1000-8000-00805f9b34fb";
-const char opush[] = "00001105-0000-1000-8000-00805f9b34fb";
-const char ftp[] = "00001106-0000-1000-8000-00805f9b34fb";
-const char panu[] = "00001115-0000-1000-8000-00805f9b34fb";
-const char spp[] = "00001101-0000-1000-8000-00805f9b34fb";
-
+ const char a2sink[] = "0000110b-0000-1000-8000-00805f9b34fb";
+ const char a2src[] = "0000110a-0000-1000-8000-00805f9b34fb";
+ const char syncml[] = "00005601-0000-1000-8000-0002ee000001";
+ const char hf[] = "0000111e-0000-1000-8000-00805f9b34fb";
+ const char hfag[] = "0000111f-0000-1000-8000-00805f9b34fb";
+ const char hs[] = "00001108-0000-1000-8000-00805f9b34fb";
+ const char hsag[] = "00001112-0000-1000-8000-00805f9b34fb";
+ const char opush[] = "00001105-0000-1000-8000-00805f9b34fb";
+ const char ftp[] = "00001106-0000-1000-8000-00805f9b34fb";
+ const char panu[] = "00001115-0000-1000-8000-00805f9b34fb";
+ const char spp[] = "00001101-0000-1000-8000-00805f9b34fb";
}
#endif // BTPROFILES_H
void NearbyDevicesModel::pair(QString hwaddy)
{
- qDebug("attempting to pair...");
- OrgBluezManagerInterface manager(
- "org.bluez",
- "/", QDBusConnection::systemBus());
-
- QDBusObjectPath adapterpath = manager.DefaultAdapter();
-
- OrgBluezAdapterInterface adapter(
- "org.bluez",
- adapterpath.path(),
- QDBusConnection::systemBus());
+ qDebug()<<"attempting to pair with "<<hwaddy;
+ if(!adapter) return;
agent = new AsyncAgent("/temp/agent",this);
- //QDBusPendingCall response =
- adapter.CreatePairedDevice(hwaddy,
+ adapter->CreatePairedDevice(hwaddy,
QDBusObjectPath("/temp/agent"),"");
//qDebug()<<"new object created: "<<object.path();
if(!found)
{
+ beginInsertRows(QModelIndex(), devicepathlist.size()+1, devicepathlist.size()+1);
devicepathlist.append(hwaddy);
- beginInsertRows(QModelIndex(), devicepathlist.size(), devicepathlist.size());
deviceAliasMap[hwaddy] = properties["Alias"].toString();
emit nearbyDeviceFound(devicepathlist.indexOf(hwaddy));
endInsertRows();