如何使用QT强制蓝牙配对引脚?

时间:2016-09-02 09:28:50

标签: c++ qt bluetooth

我在笔记本电脑上写了一个关于qt的应用程序(Debian 8)。此应用程序应连接到其他设备,如我的Android智能手机(4.4.2,5.0.2,6.0.0)。这项工作很棒,我可以配对和连接,但我希望智能手机和应用程序显示配对的引脚。我查看了文档并说它

  

此信号仅用于通过呼叫配对请求问题   requestPairing()。

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

所以这意味着,QT首先尝试连接没有引脚,如果出现问题就会使用引脚?我怎么能强迫它总是显示一个针? (不需要自定义引脚。)我的代码的某些部分:

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;
}

我知道没有对话框,但如果我可以调试针脚请求的消息或确认会很好。

谢谢。

1 个答案:

答案 0 :(得分:1)

  

所以意味着,QT首先尝试连接而不使用引脚,并将使用   如果出现问题,请确定?我怎么能强迫它总是显示一个   销? (不需要自定义引脚。)我的代码的某些部分:

有两种配对方法,一种是普通键合,另一种是专用键合。第一个就像你的描述一样,即首先尝试连接一些配置文件,安全模块会请求配对。第二个是配对但实际上没有连接配置文件。

关于您的问题,您需要注意您的设备是使用密码还是SSP,如果是第二个,通常他们使用“正常工作”方法然后您不能打印密码请求信息,因为他们正在使用不同的功能。 如果您的设备使用密码,只需取消配对然后再配对,我相信它应该是针对话框出现。