From 9df90e731612efe1cd9a69ae114f566c464e3e3a Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Sun, 14 Feb 2016 00:35:55 +0100 Subject: Revived old Bluez4 code from r50. --- rockworkd/libpebble/bluez/bluezclient.cpp | 87 ++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 7 deletions(-) (limited to 'rockworkd/libpebble/bluez/bluezclient.cpp') 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 #include @@ -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 > listAdaptersReply = system.call( + QDBusMessage::createMethodCall("org.bluez", "/", "org.bluez.Manager", + "ListAdapters")); + if (!listAdaptersReply.isValid()) { + qWarning() << listAdaptersReply.error().message(); + return; + } + + QList adapters = listAdaptersReply.value(); + + if (adapters.isEmpty()) { + qWarning() << "No BT adapters found"; + return; + } + + QDBusReply adapterPropertiesReply = system.call( + QDBusMessage::createMethodCall("org.bluez", adapters[0].path(), "org.bluez.Adapter", + "GetProperties")); + if (!adapterPropertiesReply.isValid()) { + qWarning() << adapterPropertiesReply.error().message(); + return; + } + + QList devices; + adapterPropertiesReply.value()["Devices"].value() >> devices; + + foreach (QDBusObjectPath path, devices) { + QDBusReply 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 BluezClient::pairedPebbles() const +QList BluezClient::pairedPebbles() const { - QList ret; - if (m_bluezManager.isValid()) { - foreach (const Device &dev, m_devices) { - ret << dev; - } + QList 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(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; -- cgit v1.2.3