Qt app与蓝牙打印机配对 - 将PIN传递给打印机

时间:2015-09-28 09:38:49

标签: qt printing bluetooth

我有以下代码,头文件: 的 uebluetoothmanager.h

#ifndef UEBLUETOOTHMANAGER_H
#define UEBLUETOOTHMANAGER_H

#include <QObject>
#include <QBluetoothServiceDiscoveryAgent>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothServiceInfo>
#include <QBluetoothLocalDevice>
#include <QDebug>

#include "../settings/uedefaults.h"

class UeBluetoothManager : public QObject
{
    Q_OBJECT

private:
    QBluetoothServiceDiscoveryAgent* m_ueBtServiceDiscoveryAgent;
    QBluetoothDeviceDiscoveryAgent* m_ueBtDeviceDiscoveryAgent;
    QBluetoothLocalDevice* m_ueBtPrinter;

public:
    explicit UeBluetoothManager(QObject *parent = 0);
    ~UeBluetoothManager();

    inline void ueSetBtServiceDiscoveryAgent(QBluetoothServiceDiscoveryAgent* const btServiceDiscoveryAgent)
        { this->m_ueBtServiceDiscoveryAgent=btServiceDiscoveryAgent; }
    inline void ueSetBtDeviceDiscoveryAgent(QBluetoothDeviceDiscoveryAgent* const btDeviceDiscoveryAgent)
        { this->m_ueBtDeviceDiscoveryAgent=btDeviceDiscoveryAgent; }
    inline void ueSetBtPrinter(QBluetoothLocalDevice* btLocalDevice)
        { this->m_ueBtPrinter=btLocalDevice; }

    inline QBluetoothServiceDiscoveryAgent* ueBtServiceDiscoveryAgent() const
        { return this->m_ueBtServiceDiscoveryAgent; }
    inline QBluetoothDeviceDiscoveryAgent* ueBtDeviceDiscoveryAgent() const
        { return this->m_ueBtDeviceDiscoveryAgent; }
    inline QBluetoothLocalDevice* ueBtPrinter() const
        { return this->m_ueBtPrinter; }

signals:

public slots:
    void ueSlotServiceDiscovered(const QBluetoothServiceInfo&);
    void ueSlotDeviceDiscovered(const QBluetoothDeviceInfo& deviceInfo);
    void ueSlotPairingFinished(const QBluetoothAddress& address,
                               QBluetoothLocalDevice::Pairing);
};

#endif // UEBLUETOOTHMANAGER_H

及其实施, uebluetoothmanager.cpp

#include "uebluetoothmanager.h"

UeBluetoothManager::UeBluetoothManager(QObject *parent) : QObject(parent)
{
    this->ueSetBtServiceDiscoveryAgent(new QBluetoothServiceDiscoveryAgent(this));
    this->ueSetBtDeviceDiscoveryAgent(new QBluetoothDeviceDiscoveryAgent(this));
    this->ueSetBtPrinter(new QBluetoothLocalDevice(this));

    this->connect(this->ueBtServiceDiscoveryAgent(),
                  SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
                  this,
                  SLOT(ueSlotServiceDiscovered(QBluetoothServiceInfo)));

    this->connect(this->ueBtDeviceDiscoveryAgent(),
                  SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
                  this,
                  SLOT(ueSlotDeviceDiscovered(QBluetoothDeviceInfo)));

    this->connect(this->ueBtPrinter(),
                  SIGNAL(pairingFinished(QBluetoothAddress,
                                         QBluetoothLocalDevice::Pairing)),
                  this,
                  SLOT(ueSlotPairingFinished(QBluetoothAddress,
                                             QBluetoothLocalDevice::Pairing)));

    this->ueBtDeviceDiscoveryAgent()->start();
    this->ueBtServiceDiscoveryAgent()->start();
}   // default constructor

UeBluetoothManager::~UeBluetoothManager()
{
    delete this->ueBtPrinter();
    delete this->ueBtDeviceDiscoveryAgent();
    delete this->ueBtServiceDiscoveryAgent();
}   // destructor

void UeBluetoothManager::ueSlotServiceDiscovered(const QBluetoothServiceInfo& info)
{
    qDebug() << info.serviceName()
             << " "
             << info.serviceDescription()
             << " "
             << info.serviceProvider();
}   // ueSlotServiceDiscovered

void UeBluetoothManager::ueSlotDeviceDiscovered(const QBluetoothDeviceInfo& info)
{
    QBluetoothLocalDevice::Pairing pairingStatus=this->ueBtPrinter()->pairingStatus(info.address());

    if(info.name().contains(UeHardware::UeReceiptPrinter::UeReceiptPrinterTSM210::PRINTER_NAME,
                            Qt::CaseSensitive))
    {
//        if(pairingStatus==QBluetoothLocalDevice::Unpaired)
//        {
            this->ueBtPrinter()->requestPairing(info.address(),
                                                QBluetoothLocalDevice::AuthorizedPaired);
//        }   // if
    }   // if
}   // ueSlotDeviceDiscovered

void UeBluetoothManager::ueSlotPairingFinished(const QBluetoothAddress& address,
                                               QBluetoothLocalDevice::Pairing pairing)
{
    qDebug() << "Pairing with "
             << address.toString()
             << " finished with status "
             << pairing
             << ".";
}   // ueSlotPairingFinished

现在,此代码(我使用KUbuntu 15.04内核Linux work002 3.19.0-28-generic #30-Ubuntu SMP Mon Aug 31 15:52:51 UTC 2015 x86_64 x86_64 x86_64 GNU/LinuxQt 5.5.0)的目的是建立与蓝牙POS收据打印机的配对连接。打印机的默认PIN值为1234。如何使用Qt Bluetooth API将此密码发送给打印机,以便应用与其配对(在公共广告位ueSlotDeviceDiscovered内)?发现设备及其服务(qDebug输出):

  

与“20:13:08:12:07:02”配对完成状态&lt;&lt; 1。

     

“Dev B”“”“”

从打印机文档开始,一旦打印机成功与设备配对,蓝色led二极管就会亮起,但在执行此代码后,此二极管不会激活。在当前的代码中,我有一行

this->ueBtPrinter()->requestPairing(info.address(),QBluetoothLocalDevice::Paired);

而不是

this->ueBtPrinter()>requestPairing(info.address(),QBluetoothLocalDevice::AuthorizedPaired);

因为如果上面的行是活动的,我从插槽中得到qDebug()输出,如果QBluetoothLocalDevice::AuthorizedPaired处于活动状态,我根本不会得到qDebug()输出,但我应该/必须使用QBluetoothLocalDevice::AuthorizedPaired启用了 blue led 。如何在QBluetoothLocalDevice::AuthorizedPaired模式下将PIN码传递给打印机?打印机的这个Dev B服务似乎是它的simaluted串口,所以如何打开这个端口,可能使用QSerialPort一个重要信息:此应用程序正在Linux中开发,但将部署到Android平板电脑上!

0 个答案:

没有答案
相关问题