[英]How to connect bluetooth headset with Qt libraries and manage input/output audio streams
我有一個簡單的應用程序,它應該能夠在我的嵌入式設備上連接藍牙耳機。 我正在使用 qt 庫(QBluetooth)來連接藍牙設備。 耳機已正確配對,但我不知道一旦打開套接字(在 socketConnected() 函數中)如何管理音頻。 我不知道這是否是正確的方法。 如何將音頻文件發送到藍牙耳機、管理音量或設置輸入和 output 音頻電平? 謝謝指教。
bluetoothMngmt.h
class 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);
}
我相信應該有單獨的 class 用於您應該使用的藍牙耳機配置文件。 問題中顯示的類是關於基本藍牙操作的。 藍牙耳機配置文件可能具有打開和關閉 SCO(用於同步數據)通道的信息,以及通過 ACL 通道發送的 AT+ 命令來控制音量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.