使用 Qt 藍(lán)牙 API 的典型用例是:
檢索有關(guān)本地藍(lán)牙設(shè)備的信息弧蝇。
掃描范圍內(nèi)的其他藍(lán)牙設(shè)備并檢索有關(guān)它們的信息碳褒。
使用 OBEX 對(duì)象推送配置文件 (OPP) 將文件推送到遠(yuǎn)程設(shè)備
使用串行端口配置文件 (SPP) 通過(guò) RFCOMM 通道連接到遠(yuǎn)程設(shè)備。
創(chuàng)建一個(gè)允許使用 SPP 的傳入連接的 RFCOMM 服務(wù)器看疗。
檢索有關(guān)低功耗藍(lán)牙設(shè)備的規(guī)范沙峻。
連接到低功耗藍(lán)牙設(shè)備。
從低功耗藍(lán)牙設(shè)備接收廣告两芳。
請(qǐng)注意摔寨,Android 和 Windows 不支持對(duì)象推送配置文件。
注意:部分 RFCOMM 功能不能由 Qt 在 Windows 上配置怖辆。服務(wù)的ServiceClassIds和ProtocolDescriptorList是自動(dòng)填充的是复。因此,使用這些字段的自定義值注冊(cè)服務(wù)可能不會(huì)在 Windows 上產(chǎn)生預(yù)期的結(jié)果竖螃。
注意:?Win32 后端不支持接收信號(hào)強(qiáng)度指示器 (RSSI) 以及藍(lán)牙 LE 設(shè)備宣傳的制造商特定數(shù)據(jù)淑廊。此外,只能找到之前已通過(guò) Windows 設(shè)置配對(duì)的設(shè)備特咆。
檢索本地設(shè)備信息
Qt 藍(lán)牙 API 有三個(gè)主要用途季惩。第一個(gè)是獲取本地和遠(yuǎn)程設(shè)備信息。檢索設(shè)備信息的第一步是檢查設(shè)備上的藍(lán)牙是否可用,并讀取本地設(shè)備地址和名稱(chēng)画拾。QBluetoothLocalDevice是提供所有這些信息的類(lèi)关摇。此外,您可以使用它來(lái)打開(kāi)/關(guān)閉藍(lán)牙碾阁、設(shè)置設(shè)備的可見(jiàn)性并確定當(dāng)前連接输虱。
QBluetoothLocalDevice?localDevice;QString?localDeviceName;//?Check?if?Bluetooth?is?available?on?this?deviceif?(localDevice.isValid())?{
????//?Turn?Bluetooth?on
????localDevice.powerOn();
????//?Read?local?device?name
????localDeviceName?=?localDevice.name();
????//?Make?it?visible?to?others
????localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
????//?Get?connected?devices
????QList<QBluetoothAddress>?remotes;
????remotes?=?localDevice.connectedDevices();}
掃描藍(lán)牙設(shè)備
與QBluetoothLocalDevice類(lèi)似,API 提供QBluetoothDeviceInfo脂凶,它為遠(yuǎn)程設(shè)備提供類(lèi)似的信息宪睹。盡管您可以自己創(chuàng)建QBluetoothDeviceInfo對(duì)象并用數(shù)據(jù)填充它們,但更簡(jiǎn)單的方法是使用QBluetoothDeviceDiscoveryAgent開(kāi)始自動(dòng)搜索可連接范圍內(nèi)的可見(jiàn)藍(lán)牙設(shè)備蚕钦。
void?MyClass::startDeviceDiscovery(){
????//?Create?a?discovery?agent?and?connect?to?its?signals
????QBluetoothDeviceDiscoveryAgent?*discoveryAgent?=?new?QBluetoothDeviceDiscoveryAgent(this);
????connect(discoveryAgent,?SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
????????????this,?SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
????//?Start?a?discovery
????discoveryAgent->start();
????//...}//?In?your?local?slot,?read?information?about?the?found?devicesvoid?MyClass::deviceDiscovered(const?QBluetoothDeviceInfo?&device){
????qDebug()?<<?"Found?new?device:"?<<?device.name()?<<?'('?<<?device.address().toString()?<<?')';}
將文件推送到遠(yuǎn)程設(shè)備
找到所需設(shè)備后亭病,Qt 藍(lán)牙提供了兩個(gè)主要用例。更簡(jiǎn)單的一種是通過(guò) Obex 對(duì)象推送配置文件 (OPP) 發(fā)送文件嘶居。顧名思義罪帖,此配置文件可以將文件從一臺(tái)設(shè)備推送到另一臺(tái)設(shè)備。目前無(wú)法拉取文件或?yàn)g覽遠(yuǎn)程文件系統(tǒng)邮屁。該配置文件不需要在交換數(shù)據(jù)之前將兩個(gè)設(shè)備配對(duì)整袁。要將文件推送到遠(yuǎn)程設(shè)備,請(qǐng)創(chuàng)建一個(gè)QBluetoothTransferRequest并要求QBluetoothTransferManager通過(guò)調(diào)用其put()函數(shù)來(lái)推送包含在請(qǐng)求中的文件佑吝。
//?Create?a?transfer?managerQBluetoothTransferManager?*transferManager?=?new?QBluetoothTransferManager(this);//?Create?the?transfer?request?and?file?to?be?sentQBluetoothAddress?remoteAddress("00:11:22:33:44:55:66");QBluetoothTransferRequest?request(remoteAddress);QFile?*file?=?new?QFile("testfile.txt");//?Ask?the?transfer?manager?to?send?itQBluetoothTransferReply?*reply?=?transferManager->put(request,?file);if?(reply->error()?==?QBluetoothTransferReply::NoError)?{
????//?Connect?to?the?reply's?signals?to?be?informed?about?the?status?and?do?cleanups?when?done
????QObject::connect(reply,?SIGNAL(finished(QBluetoothTransferReply*)),
?????????????????????this,?SLOT(transferFinished(QBluetoothTransferReply*)));
????QObject::connect(reply,?SIGNAL(error(QBluetoothTransferReply::TransferError)),
?????????????????????this,?SLOT(error(QBluetoothTransferReply::TransferError)));}?else?{
????qWarning()?<<?"Cannot?push?testfile.txt:"?<<?reply->errorString();}
在設(shè)備之間交換數(shù)據(jù)
在兩個(gè)啟用藍(lán)牙的設(shè)備之間進(jìn)行通信的更靈活的方法是創(chuàng)建一個(gè)虛擬串行端口連接并通過(guò)該連接自由交換數(shù)據(jù)坐昙。這可以通過(guò)串行端口配置文件 (SPP) 來(lái)完成。串行端口配置文件模擬通過(guò)藍(lán)牙傳輸協(xié)議 RFCOMM 的串行連接芋忿。
為了能夠接收傳入的 SPP 連接炸客,您需要使用QBluetoothServer監(jiān)聽(tīng)傳入的連接。
rfcommServer?=?new?QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol,?this);connect(rfcommServer,?&QBluetoothServer::newConnection,
????????this,?QOverload<>::of(&ChatServer::clientConnected));bool?result?=?rfcommServer->listen(localAdapter);if?(!result)?{
????qWarning()?<<?"Cannot?bind?chat?server?to"?<<?localAdapter.toString();
????return;}
使用QBluetoothSocket從扮演客戶(hù)端角色的另一臺(tái)設(shè)備連接到此服務(wù)器:
void?ChatClient::startClient(const?QBluetoothServiceInfo?&remoteService){
????if?(socket)
????????return;
????//?Connect?to?service
????socket?=?new?QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
????qDebug()?<<?"Create?socket";
????socket->connectToService(remoteService);
????qDebug()?<<?"ConnectToService?done";
????connect(socket,?&QBluetoothSocket::readyRead,?this,?&ChatClient::readSocket);
????connect(socket,?&QBluetoothSocket::connected,?this,?QOverload<>::of(&ChatClient::connected));
????connect(socket,?&QBluetoothSocket::disconnected,?this,?&ChatClient::disconnected);
????connect(socket,?QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error),
????????????this,?&ChatClient::onSocketErrorOccurred);}
使用這種連接允許雙向交換任何形式的數(shù)據(jù)戈钢。它非常適合游戲或在兩個(gè)設(shè)備上的應(yīng)用程序的兩個(gè)實(shí)例之間同步狀態(tài)痹仙。