gpt4 book ai didi

c++ - 如何将蓝牙耳机与 Qt 库连接并管理输入/输出音频流

转载 作者:行者123 更新时间:2023-11-28 04:08:47 24 4
gpt4 key购买 nike

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

蓝牙管理.h

BluetoothMgmt 类:公共(public) 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

蓝牙管理.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);

}

最佳答案

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

关于c++ - 如何将蓝牙耳机与 Qt 库连接并管理输入/输出音频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58239036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com