agent->confirmModeChange(mode);
}
-void AgentAdaptor::DisplayPasskey(const QDBusObjectPath &deviceObject, uint passkey)
+void AgentAdaptor::DisplayPasskey(const QDBusObjectPath &deviceObject, uint passkey, uint entered)
{
if(!agent)
{
OrgBluezDeviceInterface device("org.bluez", deviceObject.path(), QDBusConnection::systemBus());
- agent->displayPasskey(device, passkey);
+ agent->displayPasskey(device, passkey, entered);
}
void AgentAdaptor::Release()
void Authorize(const QDBusObjectPath &device, const QString &uuid);
void Cancel();
void ConfirmModeChange(const QString &mode);
- void DisplayPasskey(const QDBusObjectPath &device, uint passkey);
+ void DisplayPasskey(const QDBusObjectPath &device, uint passkey, uint entered);
void Release();
void RequestConfirmation(const QDBusObjectPath &device, uint passkey);
uint RequestPasskey(const QDBusObjectPath &device);
QString alias = props["Alias"].toString();
+ deviceToPair = new BluetoothDevice(QDBusObjectPath(device.path()),this);
+
QMetaObject::invokeMethod(parent(),"requestConfirmation",
Qt::QueuedConnection, Q_ARG(QString, alias), Q_ARG(uint,key));
QString alias = props["Alias"].toString();
+ deviceToPair = new BluetoothDevice(QDBusObjectPath(device.path()),this);
+
QMetaObject::invokeMethod(parent(), "requestPasskey", Qt::QueuedConnection, Q_ARG(QString, alias));
return 0;
QString alias = props["Alias"].toString();
+ deviceToPair = new BluetoothDevice(QDBusObjectPath(device.path()),this);
+
QMetaObject::invokeMethod(parent(), "requestPidCode", Qt::QueuedConnection, Q_ARG(QString, alias));
return "";
#define ASYNCAGENT_H
#include "bluetoothbaseagent.h"
+#include "bluetoothdevice.h"
class AsyncAgent : public BluetoothBaseAgent
{
public:
explicit AsyncAgent(QString path, QObject *parent = 0);
+ BluetoothDevice* device() { return deviceToPair; }
+
void requestConfirmation(OrgBluezDeviceInterface &device, uint key);
uint requestPasskey(OrgBluezDeviceInterface &device);
QString requestPidCode(OrgBluezDeviceInterface &device);
QDBusMessage pendingMessage;
QDBusConnection m_connection;
+ BluetoothDevice* deviceToPair;
+
};
#endif // ASYNCAGENT_H
#include "blueadapter.h"
#include <QTimer>
-BluetoothBaseAgent::BluetoothBaseAgent(QString path, QObject *parent):QObject(parent),requestAccepted(false)
+BluetoothBaseAgent::BluetoothBaseAgent(QString path, QObject *parent):QObject(parent),requestAccepted(false),m_path(path)
{
new AgentAdaptor(this);
QDBusConnection::systemBus().registerObject(path,this);
+}
- OrgBluezManagerInterface manager(
- "org.bluez",
- "/", QDBusConnection::systemBus());
-
- QDBusObjectPath adapterpath = manager.DefaultAdapter();
-
- OrgBluezAdapterInterface adapter(
- "org.bluez",
- adapterpath.path(),
- QDBusConnection::systemBus());
+void BluetoothBaseAgent::registerAgent()
+{
+ OrgBluezManagerInterface manager(
+ "org.bluez",
+ "/", QDBusConnection::systemBus());
- adapter.RegisterAgent(QDBusObjectPath(path),"");
+ QDBusObjectPath adapterpath = manager.DefaultAdapter();
- qDebug()<<"last error: "<<adapter.lastError().message();
+ OrgBluezAdapterInterface adapter(
+ "org.bluez",
+ adapterpath.path(),
+ QDBusConnection::systemBus());
+ adapter.RegisterAgent(QDBusObjectPath(m_path),"");
+ qDebug()<<"last error: "<<adapter.lastError().message();
}
void BluetoothBaseAgent::authorize(OrgBluezDeviceInterface &device, QString uuid)
qDebug()<<"mode changed "<<mode;
}
-void BluetoothBaseAgent::displayPasskey(OrgBluezDeviceInterface &device, uint key)
+void BluetoothBaseAgent::displayPasskey(OrgBluezDeviceInterface &device, uint key, uint entered)
{
qDebug()<<"display key "<<device.path()<<" "<<key;
+
+ ///create and return back an empty reply:
+ QDBusMessage reply = message().createReply();
+ connection().send(reply);
}
void BluetoothBaseAgent::release()
{
Q_OBJECT
public:
- BluetoothBaseAgent(QString path="/pairing/agent", QObject *parent=0);
+ BluetoothBaseAgent(QString path="", QObject *parent=0);
public slots:
virtual void authorize(OrgBluezDeviceInterface &device, QString);
+ virtual void registerAgent();
virtual void cancel()
{
sendErrorReply("org.bluez.Error.Canceled","The request was canceled");
}
virtual void confirmModeChange(QString);
- virtual void displayPasskey(OrgBluezDeviceInterface &device, uint key);
+ virtual void displayPasskey(OrgBluezDeviceInterface &device, uint key, uint entered);
virtual void release();
virtual void requestConfirmation(OrgBluezDeviceInterface &device, uint key);
virtual uint requestPasskey(OrgBluezDeviceInterface &device);
private:
bool requestAccepted;
+ QString m_path;
};
#endif // BLUETOOTHBASEAGENT_H
adapterAdded(QDBusObjectPath());
QHash<int,QByteArray> roles;
- roles[NearbyDevicesModelRoles::name]="name";
- roles[NearbyDevicesModelRoles::address]="address";
+ QMetaObject properties = NearbyItem::staticMetaObject;
+ for(int i=0; i<properties.propertyCount();i++)
+ {
+ roles[i]=properties.property(i).name();
+ }
setRoleNames(roles);
}
int NearbyDevicesModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
- return devicepathlist.size();
+ return devices.size();
}
QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
{
- if (role == NearbyDevicesModelRoles::name)
+ /**if (role == NearbyDevicesModelRoles::name)
{
QString rowData;
- if(index.row() < devicepathlist.size())
+ if(index.row() < devices.size())
{
- rowData = deviceAliasMap[devicepathlist[index.row()]];
+ rowData = deviceAliasMap[devices[index.row()]];
}
return QVariant(rowData);
}
else if (role == NearbyDevicesModelRoles::address)
{
QString rowData;
- if(index.row() < devicepathlist.size())
+ if(index.row() < devices.size())
{
- rowData = devicepathlist[index.row()];
+ rowData = devices[index.row()];
}
return QVariant(rowData);
+ }*/
+
+ QString roleName = roleNames()[role];
+ QMetaObject object = NearbyItem::staticMetaObject;
+
+ for(int i=0; i<object.propertyCount(); i++)
+ {
+ if(object.property(i).name() == roleName)
+ {
+ return object.property(i).read(devices[index.row()]);
+ }
}
+
return QVariant();
}
qDebug()<<"attempting to pair with "<<hwaddy;
if(!adapter) return;
- agent = new AsyncAgent("/temp/agent", this);
+ if(!agent) agent = new AsyncAgent("/temp/agent", this);
adapter->CreatePairedDevice(hwaddy,
QDBusObjectPath("/temp/agent"),"");
void NearbyDevicesModel::removeAll(bool)
{
- for(int i=0;i<devicepathlist.size();i++)
+ for(int i=0;i<devices.size();i++)
{
- devicepathlist.removeAt(i);
+ devices.removeAt(i);
}
}
void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
{
bool found = false;
- foreach(QString path, devicepathlist)
+ foreach(NearbyItem* path, devices)
{
- if(path == hwaddy)
+ if(path->address() == hwaddy)
{
found=true;
break;
if(!found)
{
- beginInsertRows(QModelIndex(), devicepathlist.size()+1, devicepathlist.size()+1);
- devicepathlist.append(hwaddy);
- deviceAliasMap[hwaddy] = properties["Alias"].toString();
- emit nearbyDeviceFound(devicepathlist.indexOf(hwaddy));
+ beginInsertRows(QModelIndex(), devices.size()+1, devices.size()+1);
+
+ NearbyItem* item = new NearbyItem(properties["Alias"].toString(),
+ hwaddy,properties["Icon"].toString(),this);
+
+ devices.append(item);
+ emit nearbyDeviceFound(devices.indexOf(item));
endInsertRows();
}
}
void NearbyDevicesModel::deviceRemoved(QString hwaddy)
{
- int i=-1;
- if((i = devicepathlist.indexOf(hwaddy)) >=0)
+ foreach(NearbyItem* device, devices)
{
- beginRemoveRows(QModelIndex(),i,i);
- devicepathlist.removeAt(i);
- emit nearbyDeviceRemoved(i);
- endRemoveRows();
+ if(device->address() == hwaddy)
+ {
+ int i=devices.indexOf(device);
+ beginRemoveRows(QModelIndex(),i,i);
+ devices.removeAll(device);
+ emit nearbyDeviceRemoved(i);
+ endRemoveRows();
+ }
}
-
}
void NearbyDevicesModel::adapterAdded(QDBusObjectPath path)
#include "bluemanager.h"
#include "blueadapter.h"
#include "asyncagent.h"
+#include "bluetoothdevice.h"
namespace NearbyDevicesModelRoles
{
enum
{
name = Qt::UserRole + 1,
- address
+ address,
+ icon
};
}
+class NearbyItem: public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(QString name READ name)
+ Q_PROPERTY(QString address READ address)
+ Q_PROPERTY(QString icon READ icon)
+public:
+ NearbyItem(QString name="", QString address="", QString icon="", QObject* parent = 0)
+ :QObject(parent)
+ {
+ m_name=name;
+ m_addy=address;
+ m_icon=icon;
+ }
+
+ QString name() { return m_name; }
+ QString address() { return m_addy; }
+ QString icon() { return m_icon; }
+
+private:
+ QString m_name;
+ QString m_addy;
+ QString m_icon;
+};
+
class NearbyDevicesModel : public QAbstractListModel
{
Q_OBJECT
+ Q_PROPERTY(BluetoothDevice* pairingDevice READ pairingDevice)
public:
NearbyDevicesModel(QObject *parent = 0);
+ BluetoothDevice* pairingDevice() { if(agent) return agent->device(); else return NULL; }
+
public slots:
int columnCount(const QModelIndex &) const { return 1;}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
- QString hwAddress(int index) { return devicepathlist[index]; }
- QString alias(int index){ return deviceAliasMap[devicepathlist[index]]; }
+ QString hwAddress(int index) { return devices[index]->address(); }
+ QString alias(int index){ return devices[index]->name(); }
void pair(QString hwaddy);
void discover(bool start);
void removeAll(bool);
void adapterPropertiesChanged(QString name, QVariant value);
private:
- QList<QString> devicepathlist;
- QMap<QString,QString> deviceAliasMap;
+ QList<NearbyItem*> devices;
OrgBluezManagerInterface *manager;
OrgBluezAdapterInterface *adapter;
- AsyncAgent* agent;
+ AsyncAgent* agent;
};
#endif // NEARBYDEVICESMODEL_H