1 #include "nearbydevicesmodel.h"
2 #include "bluetoothbaseagent.h"
4 NearbyDevicesModel::NearbyDevicesModel(QObject *parent) :
5 QAbstractListModel(parent), adapter(NULL), agent(NULL)
7 manager = new OrgBluezManagerInterface(
9 "/", QDBusConnection::systemBus(), this);
11 connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
12 connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
13 adapterAdded(QDBusObjectPath());
15 QHash<int,QByteArray> roles;
16 roles[Qt::DisplayRole]="name";
22 int NearbyDevicesModel::rowCount(const QModelIndex &parent) const
25 return devicepathlist.size();
28 QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
30 if (role == Qt::DisplayRole)
33 if(index.row() < devicepathlist.size())
35 rowData = deviceAliasMap[devicepathlist[index.row()]];
37 return QVariant(rowData);
43 void NearbyDevicesModel::pair(QString hwaddy)
46 qDebug()<<"attempting to pair with "<<hwaddy;
48 agent = new AsyncAgent("/temp/agent",this);
50 adapter->CreatePairedDevice(hwaddy,
51 QDBusObjectPath("/temp/agent"),"");
53 //qDebug()<<"new object created: "<<object.path();
56 void NearbyDevicesModel::discover(bool start)
59 adapter->StartDiscovery();
60 else adapter->StopDiscovery();
63 void NearbyDevicesModel::removeAll(bool)
65 for(int i=0;i<devicepathlist.size();i++)
67 devicepathlist.removeAt(i);
71 void NearbyDevicesModel::replyRequestConfirmation(bool confirmed)
73 if(agent) agent->replyRequestConfirmation(confirmed);
76 void NearbyDevicesModel::replyPasskey(uint passkey)
78 if(agent) agent->replyPasskey(passkey);
81 void NearbyDevicesModel::replyRequestPidCode(QString pidCode)
83 if(agent) agent->replyRequestPidCode(pidCode);
86 void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
89 foreach(QString path, devicepathlist)
100 beginInsertRows(QModelIndex(), devicepathlist.size()+1, devicepathlist.size()+1);
101 devicepathlist.append(hwaddy);
102 deviceAliasMap[hwaddy] = properties["Alias"].toString();
103 emit nearbyDeviceFound(devicepathlist.indexOf(hwaddy));
108 void NearbyDevicesModel::deviceRemoved(QString hwaddy)
111 if((i = devicepathlist.indexOf(hwaddy)) >=0)
113 beginRemoveRows(QModelIndex(),i,i);
114 devicepathlist.removeAt(i);
115 emit nearbyDeviceRemoved(i);
121 void NearbyDevicesModel::adapterAdded(QDBusObjectPath path)
123 if(adapter && adapter->path() == path.path()) return;
125 QDBusObjectPath adapterpath = manager->DefaultAdapter();
127 if(adapterpath.path() == "")
130 adapter = new OrgBluezAdapterInterface(
133 QDBusConnection::systemBus(), this);
136 SIGNAL(DeviceFound(QString, QVariantMap)),
138 SLOT(deviceCreated(QString, QVariantMap)));
140 SIGNAL(DeviceDisappeared(QString)),
142 SLOT(deviceRemoved(QString)));
145 void NearbyDevicesModel::adapterRemoved(QDBusObjectPath)
147 QDBusObjectPath adapterpath = manager->DefaultAdapter();
149 if(adapterpath.path() == "")
152 if(adapter) delete adapter;