简体   繁体   中英

How to force a bluetooth pairing pin with QT?

i am writing an apllication in qt on my laptop (Debian 8). This applicatio should connect to other devices like my android smartphones (4.4.2, 5.0.2, 6.0.0). This work great, i can pair and connect, but i want the smartphone and the application to show a pin for pairing. I looked in the documentation and it says

This signal is only emitted for pairing requests issues by calling requestPairing().

( http://doc.qt.io/qt-5/qbluetoothlocaldevice.html#pairingDisplayConfirmation ).

So means this, QT tries first to connect without pin and will use the pin just if something goes wrong? How can i force it to show always a pin? (There is no need for a custom pin.) Some parts of my code:

localDevice = new QBluetoothLocalDevice();

connect(localDevice, SIGNAL(pairingFinished(QBluetoothAddress,QBluetoothLocalDevice::Pairing)), this, SLOT(pairingDone(QBluetoothAddress,QBluetoothLocalDevice::Pairing)));

connect(localDevice, SIGNAL(pairingDisplayConfirmation(const QBluetoothAddress&, QString)), this, SLOT(pairingMyDisplayConfirmation(const QBluetoothAddress&, QString)));

connect(localDevice, SIGNAL(pairingDisplayPinCode(QBluetoothAddress,QString)), this, SLOT(displayPin(QBluetoothAddress,QString)));

...

void MainWindow::doPairingAction()
{
    if (ui->list->count() == 0){
        return;
    }

    if (ui->selectedMac->text().length() == 0 || ui->selectedName->text().length() == 0){
        return;
    }

    QBluetoothAddress address (ui->selectedMac->text());
    if (ui->pair->text() == "Pair") {
        qDebug() << "pair " << address;



        localDevice->requestPairing(address, QBluetoothLocalDevice::Paired);
    } else if (ui->pair->text() == "Unpair") {
        localDevice->requestPairing(address, QBluetoothLocalDevice::Unpaired);
    }
}

void MainWindow::pairingMyDisplayConfirmation(const QBluetoothAddress &address, QString pin){
    qDebug() << "#PAIRING_DISPLAY_CONFIRMATION_PIN: " << pin << "for" << address;
}


void MainWindow::pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
{
    QList<QListWidgetItem *> items = ui->list->findItems(address.toString(), Qt::MatchContains);

    if (pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired ) {
        for (int var = 0; var < items.count(); ++var) {
            QListWidgetItem *item = items.at(var);
            item->setTextColor(QColor(Qt::green));
            setPairStatus(true);
            qDebug() << "pair " << address << "success";
        }
    } else {
        for (int var = 0; var < items.count(); ++var) {
            QListWidgetItem *item = items.at(var);
            item->setTextColor(QColor(Qt::red));
            setPairStatus(false);
            qDebug() << "pair " << address << "failed";
        }
    }
}


void MainWindow::displayPin(const QBluetoothAddress &address, QString pin)
{
    qDebug() << "#DISPLAY_PIN: " << pin << "for" << address;
}

I know there is no dialog, but if i could debug the message for pin request or confirmation would be great.

Thank You.

So means this, QT tries first to connect without pin and will use the pin just if something goes wrong? How can i force it to show always a pin? (There is no need for a custom pin.) Some parts of my code:

There are two kind of pairing method, one is general bonding and the other one is dedicated bonding. The first one just like your description, ie first try to connect some profile and the security module would request pairing. the second one is just pairing but does not connect profile actually.

Regarding to your question, you need care whether your device are using the pin code or SSP, if for the second one, generally they are using "just work" method and then you can not print the pin code request information since they are using the different function. if your device using the pin code, just unpair it and then pair again I believe it should be pin dialog appears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM