initial Bluetooth Device Model addition
authorKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 9 Sep 2010 23:52:49 +0000 (16:52 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 9 Sep 2010 23:52:49 +0000 (16:52 -0700)
bluetooth-qt.pro
bluetoothdevice.cpp [new file with mode: 0644]
bluetoothdevice.h [new file with mode: 0644]
bluetoothdevicemodel.cpp [new file with mode: 0644]
bluetoothdevicemodel.h [new file with mode: 0644]
btprofiles.h
nearbydevicesmodel.cpp

index 1416855..21468f3 100644 (file)
@@ -29,7 +29,9 @@ HEADERS += agentadaptor.h \
        nearbydevicesmodel.h \
        bluetoothbaseagent.h \
        asyncagent.h \
-       devicetypes.h
+       devicetypes.h \
+    bluetoothdevice.h \
+    bluetoothdevicemodel.h
 SOURCES += agentadaptor.cpp \
        bluemanager.cpp \
        bluedevice.cpp \
@@ -39,7 +41,9 @@ SOURCES += agentadaptor.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/
diff --git a/bluetoothdevice.cpp b/bluetoothdevice.cpp
new file mode 100644 (file)
index 0000000..8446927
--- /dev/null
@@ -0,0 +1,162 @@
+#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();
+       }
+}
diff --git a/bluetoothdevice.h b/bluetoothdevice.h
new file mode 100644 (file)
index 0000000..03b9779
--- /dev/null
@@ -0,0 +1,53 @@
+#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
diff --git a/bluetoothdevicemodel.cpp b/bluetoothdevicemodel.cpp
new file mode 100644 (file)
index 0000000..ca786ed
--- /dev/null
@@ -0,0 +1,130 @@
+#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();
+               }
+       }
+}
diff --git a/bluetoothdevicemodel.h b/bluetoothdevicemodel.h
new file mode 100644 (file)
index 0000000..6181871
--- /dev/null
@@ -0,0 +1,41 @@
+#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
index 84e4b9a..b6a4ac7 100644 (file)
 #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
index 00d07f5..db7acff 100644 (file)
@@ -42,22 +42,12 @@ QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
 
 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();
@@ -107,8 +97,8 @@ void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
 
        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();