0

我有一个简单的应用程序,它应该能够在我的嵌入式设备上连接蓝牙耳机。我正在使用 qt 库(QBluetooth)连接蓝牙设备。耳机已正确配对,但我不知道一旦打开套接字(在 socketConnected() 函数中)如何管理音频。我不知道这是否是正确的方法。如何将音频文件发送到蓝牙耳机、管理音量或设置输入和输出音频电平?谢谢指教。

bluetoothMngmt.h

类 BluetoothMgmt:公共 QObject { Q_OBJECT

public:
    BluetoothMgmt();
    ~BluetoothMgmt();    

private:
    //Classes
    QList<QBluetoothServiceInfo> *servicesInfo;

    //Variables/Objects
    QBluetoothLocalDevice *localDevice;
    QString localDeviceName;
    QBluetoothDeviceDiscoveryAgent *scanDevices;
    QBluetoothServiceDiscoveryAgent *scanServices;
    QBluetoothServer *rfcommServer;
    QBluetoothSocket *socket;

    //Functions

private slots:
    void startScan();
    void foundDevices(const QBluetoothDeviceInfo &device);
    void pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing);
    void foundService(const QBluetoothServiceInfo &info);
    void socketConnected();
    void socketDisconnect();
    void readSocket();
    void socketError(QBluetoothSocket::SocketError error);
    void serviceDiscovered(const QBluetoothServiceInfo &service);
    void manageBtServices();

};

#endif // BLUETOOTHMGMT_H

bluetoothMngmt.cpp

//Constructor
BluetoothMgmt::BluetoothMgmt() {

    /* QT libraries */        
    /* Check if Bluetooth is available on this device */
    localDevice = new QBluetoothLocalDevice(this);
    scanDevices = new QBluetoothDeviceDiscoveryAgent(this);
    //scanDevices->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry);
    scanServices = new QBluetoothServiceDiscoveryAgent(this);
    servicesInfo = new QList<QBluetoothServiceInfo>;
    socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);

    /* Create a discovery agent and connect to its signals */
    connect(scanDevices, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
            this, SLOT(foundDevices(QBluetoothDeviceInfo)));
    connect(localDevice, SIGNAL(pairingFinished(QBluetoothAddress, QBluetoothLocalDevice::Pairing)),
            this, SLOT(pairingDone(QBluetoothAddress, QBluetoothLocalDevice::Pairing)));
    connect(scanServices, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
                this, SLOT(foundService(QBluetoothServiceInfo)) );
    connect(scanServices, SIGNAL(finished()),
            this, SLOT(manageBtServices()));
    connect(socket, SIGNAL(connected()),
            this, SLOT(socketConnected()) );
    connect(socket, SIGNAL(disconnected()),
            this, SLOT(socketDisconnect()) );
    connect(socket, SIGNAL(readyRead()),
            this, SLOT(readSocket()) );
    connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)),
            this, SLOT(socketError(QBluetoothSocket::SocketError)) );


    if (localDevice->isValid()) {

        /* Turn Bluetooth on */
        localDevice->powerOn();

        /* Read local device name */
        localDeviceName = localDevice->name();
        qDebug() << "local device name:" << localDeviceName;

        /* Make it visible to others */
        localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable);

        /* Get connected devices */
        QList<QBluetoothAddress> remotes;
        remotes = localDevice->connectedDevices();
        for(int i=0; i<remotes.size(); i++) {
            qDebug() << remotes.at(i).toString();
        }

    }

    // Start a discovery
    startScan();

}

//Destroyer
BluetoothMgmt::~BluetoothMgmt() {
    delete navisIntLogger;
    delete localDevice;
    delete scanDevices;
    delete scanServices;
    delete servicesInfo;
    delete socket;
}

void BluetoothMgmt::startScan()
{
    scanDevices->start();
    navisIntLogger->log(InternalLogger::LOG_INFO, "scan bluetooth devices started");
}

void BluetoothMgmt::foundDevices(const QBluetoothDeviceInfo &device) {


    qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';


    if(QString::compare("WH-CH500", device.name(), Qt::CaseSensitive) == 0) {
        qDebug() << "WH-CH500 found with associated address:" << device.address().toString();
       //

        localDevice->requestPairing(device.address(), QBluetoothLocalDevice::Paired);

scanDevices->stop();


    }


    if(QString::compare(device.address().toString(), "D8:68:C3:5E:8A:48", Qt::CaseSensitive) == 0) {
        qDebug() << "CELLUILARE TROVATo";
        localDevice->requestPairing(device.address(), QBluetoothLocalDevice::Paired);
        scanDevices->stop();
    }

}

void BluetoothMgmt::pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) {
    if(pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired) {

        qDebug() << "PAIRING done";

        QBluetoothAddress address("00:18:09:8A:09:35"); //cuffia

        servicesInfo->clear();
        scanServices->setRemoteAddress(address);

        connect(scanServices, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
                    this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
        scanServices->start();

    }
}

void BluetoothMgmt::foundService(const QBluetoothServiceInfo &info) {
    qDebug() << "TROVATO SERVIZIO E LO AGGIUNGO ALLA LISTA DI SERVICESINFO";
    QBluetoothServiceInfo infoCopy(info);
    //servicesInfo->append(infoCopy);
}

void BluetoothMgmt::socketConnected() {
    qDebug() << "Socket connected:" << socket->peerName();
}

void BluetoothMgmt::socketDisconnect() {
    qDebug() << "Socket disconnected";
}

void BluetoothMgmt::readSocket() {
    while (socket->canReadLine()) {
        qDebug()<<QString::fromLatin1(socket->readLine().toHex());
        qDebug()<<"hehehe";
    }
}

void BluetoothMgmt::socketError(QBluetoothSocket::SocketError error) {
    qDebug() << "Socket Error:" << socket->errorString();
}


// In your local slot, read information about the found devices
void BluetoothMgmt::serviceDiscovered(const QBluetoothServiceInfo &service)
{
    qDebug() << "Found new service:" << service.serviceName()
             << '(' << service.device().address().toString() << ')';
}

void BluetoothMgmt::manageBtServices() {
    QBluetoothAddress addressR("00:18:09:8A:09:35");
    quint16 port1 = 1; /* for headset and generic audio services */
    socket->connectToService(addressR, port1, QIODevice::ReadWrite);

}
4

1 回答 1

1

我相信您应该使用单独的蓝牙耳机配置文件。问题中显示的类是关于基本蓝牙操作的。蓝牙耳机配置文件可能有关于打开和关闭 SCO(用于同步数据)通道的信息以及发送的 AT+命令来控制 ACL 通道上的音量

于 2019-10-05T07:46:31.423 回答