void QBluetoothLocalDevice::requestPairing(const QBluetoothAddress &address, Pairing pairing) { if (address.isNull()) { QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, Q_ARG(QBluetoothLocalDevice::Error, QBluetoothLocalDevice::PairingError)); return; } const Pairing current_pairing = pairingStatus(address); if (current_pairing == pairing) { QMetaObject::invokeMethod(this, "pairingFinished", Qt::QueuedConnection, Q_ARG(QBluetoothAddress, address), Q_ARG(QBluetoothLocalDevice::Pairing, pairing)); return; } d_ptr->requestPairing(address, pairing); }
void QBluetoothLocalDevice::requestPairing(const QBluetoothAddress &address, Pairing pairing) { if(pairing == Paired || pairing == AuthorizedPaired) { d_ptr->address = address; d_ptr->pairing = pairing; if(!d_ptr->agent){ d_ptr->agent = new OrgBluezAgentAdaptor(d_ptr); bool res = QDBusConnection::systemBus().registerObject(d_ptr->agent_path, d_ptr); if(!res){ qDebug() << "Failed to register agent"; return; } } Pairing current_pairing = pairingStatus(address); if(current_pairing == Paired && pairing == AuthorizedPaired){ OrgBluezDeviceInterface *device = getDevice(address, d_ptr); if(!device) return; QDBusPendingReply<> deviceReply = device->SetProperty(QLatin1String("Trusted"), QDBusVariant(true)); deviceReply.waitForFinished(); if(deviceReply.isError()){ qDebug() << Q_FUNC_INFO << "reply failed" << deviceReply.error(); return; } delete device; QMetaObject::invokeMethod(this, "pairingFinished", Qt::QueuedConnection, Q_ARG(QBluetoothAddress, address), Q_ARG(QBluetoothLocalDevice::Pairing, QBluetoothLocalDevice::AuthorizedPaired)); } else if(current_pairing == AuthorizedPaired && pairing == Paired){ OrgBluezDeviceInterface *device = getDevice(address, d_ptr); if(!device) return; QDBusPendingReply<> deviceReply = device->SetProperty(QLatin1String("Trusted"), QDBusVariant(false)); deviceReply.waitForFinished(); if(deviceReply.isError()){ qDebug() << Q_FUNC_INFO << "reply failed" << deviceReply.error(); return; } delete device; QMetaObject::invokeMethod(this, "pairingFinished", Qt::QueuedConnection, Q_ARG(QBluetoothAddress, address), Q_ARG(QBluetoothLocalDevice::Pairing, QBluetoothLocalDevice::Paired)); } else { QDBusPendingReply<QDBusObjectPath> reply = d_ptr->adapter->CreatePairedDevice(address.toString(), QDBusObjectPath(d_ptr->agent_path), QLatin1String("NoInputNoOutput")); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), d_ptr, SLOT(pairingCompleted(QDBusPendingCallWatcher*))); if(reply.isError()) qDebug() << Q_FUNC_INFO << reply.error() << d_ptr->agent_path; } } else if(pairing == Unpaired) { QDBusPendingReply<QDBusObjectPath> reply = this->d_ptr->adapter->FindDevice(address.toString()); reply.waitForFinished(); if(reply.isError()) { qDebug() << Q_FUNC_INFO << "failed to find device" << reply.error(); return; } QDBusPendingReply<> removeReply = this->d_ptr->adapter->RemoveDevice(reply.value()); removeReply.waitForFinished(); if(removeReply.isError()){ qDebug() << Q_FUNC_INFO << "failed to remove device" << removeReply.error(); } return; } return; }