Index: sources/bluetooth/BluetoothInterface.cpp =================================================================== diff -u -r1c97eebc80b33b429fa6e9489584ce3bf24f0bbc -r4a95b249fb87bf69d909452ba374c794b89d423b --- sources/bluetooth/BluetoothInterface.cpp (.../BluetoothInterface.cpp) (revision 1c97eebc80b33b429fa6e9489584ce3bf24f0bbc) +++ sources/bluetooth/BluetoothInterface.cpp (.../BluetoothInterface.cpp) (revision 4a95b249fb87bf69d909452ba374c794b89d423b) @@ -425,9 +425,47 @@ // initial scan to populate bluetooth device list startScan(); + + // It is necessary to initialize the pairing agent to assure that the pairing is + // retained after the device disconnects (Similar to how bluetoothctl does it) + initializePairingAgent(); } /*! + * \brief BluetoothInterface::initializePairingAgent + * \details Initialize and register the pairing agent components + */ +void BluetoothInterface::initializePairingAgent() { + // construct the custom bluetooth pairing agent + _pairingAgent = new BluetoothPairingAgent(); + + // construct the QDBusAbstractAdaptor class + _agentAdaptor = new Agent1Adaptor(_pairingAgent); + + // For pairing agent object to be visible on the d-bus, need to call the registerObject method + if (QDBusConnection::systemBus().registerObject(_pairingObjectPath, _pairingAgent)) { + // Get the interface reference for the org.bluez.AgentManager1 + QDBusInterface agentManager("org.bluez", "/org/bluez", "org.bluez.AgentManager1", QDBusConnection::systemBus(), this); + if (!agentManager.isValid()) { + qWarning() << Q_FUNC_INFO << agentManager.lastError().message(); + } else { + QVariant agentPath{ QVariant::fromValue(QDBusObjectPath(_pairingObjectPath)) }; + // Call the RegisterAgent method with the correct arguments, passing DisplayOnly + QDBusMessage msg{ agentManager.call("RegisterAgent", agentPath, "DisplayOnly") }; + if (msg.type() == QDBusMessage::ErrorMessage) + qWarning() << Q_FUNC_INFO << msg.errorMessage(); + else { + msg = agentManager.call("RequestDefaultAgent", agentPath); + if (msg.type() == QDBusMessage::ErrorMessage) + qWarning() << Q_FUNC_INFO << msg.errorMessage(); + } + } + } else { + // TODO need error handling + } +} + +/*! * \brief BluetoothInterface::ondoScan * \details the thread safe slot to be called when the public function doScan is called. * initializes/validates the discovery agent _agent and setups it up to start the discovery.