Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to force a Bluetooth pairing pin?



  • Hey,

    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.


Log in to reply