diff options
Diffstat (limited to 'rockworkd/libpebble/bluez/bluezclient.cpp')
| -rw-r--r-- | rockworkd/libpebble/bluez/bluezclient.cpp | 87 |
1 files changed, 80 insertions, 7 deletions
diff --git a/rockworkd/libpebble/bluez/bluezclient.cpp b/rockworkd/libpebble/bluez/bluezclient.cpp index 8cdf848..313c540 100644 --- a/rockworkd/libpebble/bluez/bluezclient.cpp +++ b/rockworkd/libpebble/bluez/bluezclient.cpp @@ -1,5 +1,6 @@ #include "bluezclient.h" #include "dbus-shared.h" +#include "device.h" #include <QDBusConnection> #include <QDBusReply> @@ -26,32 +27,84 @@ BluezClient::BluezClient(QObject *parent): InterfaceList ifaces = objectList.value(path); if (ifaces.contains(BLUEZ_DEVICE_IFACE)) { QString candidatePath = path.path(); + qDebug() << "have device" << candidatePath; auto properties = ifaces.value(BLUEZ_DEVICE_IFACE); addDevice(path, properties); } } + + if (m_devices.isEmpty()) { + // Try with bluez 4 + QDBusConnection system = QDBusConnection::systemBus(); + + QDBusReply<QList<QDBusObjectPath> > listAdaptersReply = system.call( + QDBusMessage::createMethodCall("org.bluez", "/", "org.bluez.Manager", + "ListAdapters")); + if (!listAdaptersReply.isValid()) { + qWarning() << listAdaptersReply.error().message(); + return; + } + + QList<QDBusObjectPath> adapters = listAdaptersReply.value(); + + if (adapters.isEmpty()) { + qWarning() << "No BT adapters found"; + return; + } + + QDBusReply<QVariantMap> adapterPropertiesReply = system.call( + QDBusMessage::createMethodCall("org.bluez", adapters[0].path(), "org.bluez.Adapter", + "GetProperties")); + if (!adapterPropertiesReply.isValid()) { + qWarning() << adapterPropertiesReply.error().message(); + return; + } + + QList<QDBusObjectPath> devices; + adapterPropertiesReply.value()["Devices"].value<QDBusArgument>() >> devices; + + foreach (QDBusObjectPath path, devices) { + QDBusReply<QVariantMap> devicePropertiesReply = system.call( + QDBusMessage::createMethodCall("org.bluez", path.path(), "org.bluez.Device", + "GetProperties")); + if (!devicePropertiesReply.isValid()) { + qCritical() << devicePropertiesReply.error().message(); + continue; + } + + const QVariantMap &dict = devicePropertiesReply.value(); + + QString name = dict["Name"].toString(); + if (name.startsWith("Pebble") && !name.startsWith("Pebble Time LE") && !name.startsWith("Pebble-LE")) { + qDebug() << "Found Pebble:" << name; + addDevice(path, dict); + } + } + } } } -QList<Device> BluezClient::pairedPebbles() const +QList<BluezDevice> BluezClient::pairedPebbles() const { - QList<Device> ret; - if (m_bluezManager.isValid()) { - foreach (const Device &dev, m_devices) { - ret << dev; - } + QList<BluezDevice> ret; + + foreach (const BluezDevice &dev, m_devices) { + ret << dev; } + return ret; + } void BluezClient::addDevice(const QDBusObjectPath &path, const QVariantMap &properties) { QString address = properties.value("Address").toString(); QString name = properties.value("Name").toString(); + qDebug() << "Adding device" << address << name; if (name.startsWith("Pebble") && !name.startsWith("Pebble Time LE") && !name.startsWith("Pebble-LE") && !m_devices.contains(address)) { qDebug() << "Found new Pebble:" << address << name; - Device device; + BluezDevice device; device.address = QBluetoothAddress(address); device.name = name; device.path = path.path(); @@ -70,6 +123,26 @@ void BluezClient::slotInterfacesAdded(const QDBusObjectPath &path, InterfaceList } } +void BluezClient::slotDevicePairingDone(bool success) +{ + qDebug() << "pairing done" << success; + if (!success) { + return; + } + + Device *device = static_cast<Device*>(sender()); + device->deleteLater(); + + if (!m_devices.contains(device->getAddress())) { + BluezDevice bluezDevice; + bluezDevice.address = QBluetoothAddress(device->getAddress()); + bluezDevice.name = device->getName(); + bluezDevice.path = device->getPath(); + m_devices.insert(device->getAddress(), bluezDevice); + emit devicesChanged(); + } +} + void BluezClient::slotInterfacesRemoved(const QDBusObjectPath &path, const QStringList &ifaces) { qDebug() << "interfaces removed" << path.path() << ifaces; |
