日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt 蓝牙通讯

發(fā)布時間:2023/12/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt 蓝牙通讯 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

pro文件加上QT += bluetooth

頭文件:

#ifndef BLUETOOTHPROXY_H #define BLUETOOTHPROXY_H#include <QObject>#include <QtBluetooth/qbluetoothlocaldevice.h> #include <qbluetoothaddress.h> #include <qbluetoothdevicediscoveryagent.h> #include <qbluetoothlocaldevice.h> #include <qbluetoothsocket.h>class BluetoothProxy : public QObject {Q_OBJECT public:explicit BluetoothProxy(QObject *parent = nullptr);~BluetoothProxy(); public slots:void addBlueToothDevicesToList(const QBluetoothDeviceInfo&);void readBluetoothDataEvent();void bluetoothConnectedEvent();void bluetoothDisconnectedEvent();void onError(QBluetoothSocket::SocketError error);private:QBluetoothDeviceDiscoveryAgent *discoveryAgent;QBluetoothLocalDevice *localDevice;QBluetoothSocket *socket;QByteArray blueArray;};#endif // BLUETOOTHPROXY_H

源文件:

#include "bluetoothproxy.h"//這個UUID要根據(jù)自己的使用情況來確定,我使用的是串口類的UUID,具體可https://www.jianshu.com/p/eb85cb690e86 static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB"); BluetoothProxy::BluetoothProxy(QObject *parent) : QObject(parent) {discoveryAgent = new QBluetoothDeviceDiscoveryAgent();localDevice = new QBluetoothLocalDevice();socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);discoveryAgent->start();connect(discoveryAgent,SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),this,SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo)));connect(socket,SIGNAL(readyRead()),this,SLOT(readBluetoothDataEvent()));connect(socket,SIGNAL(connected()),this,SLOT(bluetoothConnectedEvent()));connect(socket,SIGNAL(disconnected()),this,SLOT(bluetoothDisconnectedEvent()));connect(socket,SIGNAL(error(QBluetoothSocket::SocketError)),this,SLOT(onError(QBluetoothSocket::SocketError)));}BluetoothProxy::~BluetoothProxy() {}void BluetoothProxy::onError(QBluetoothSocket::SocketError error) {QString str;if(QBluetoothSocket::UnknownSocketError == error){str = "UnknownSocketError";}else if(QBluetoothSocket::NoSocketError == error){str = "NoSocketError";}else if(QBluetoothSocket::HostNotFoundError == error){str = "HostNotFoundError";}else if(QBluetoothSocket::ServiceNotFoundError == error){str = "ServiceNotFoundError";}else if(QBluetoothSocket::NetworkError == error){str = "NetworkError";}else if(QBluetoothSocket::UnsupportedProtocolError == error){str = "UnsupportedProtocolError";}else if(QBluetoothSocket::OperationError == error){str = "OperationError";}else if(QBluetoothSocket::RemoteHostClosedError == error){str = "RemoteHostClosedError";}qDebug()<<error;emit backendRunMesgPost(str);emit backendRunMesgPost("SocketError"); }void BluetoothProxy::addBlueToothDevicesToList( const QBluetoothDeviceInfo &info ) {QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());qDebug()<<label;if(label.contains("TableSoccer")){//找到需要連接的藍牙名字int index = label.indexOf(' ');if(index == -1){qDebug()<<"index";return;}QBluetoothAddress address(label.left(index));QString name(label.mid(index + 1));qDebug() << "You has choice the bluetooth address is " << address<<name;qDebug() << "The device is connneting.... ";emit backendRunMesgPost("The device is connneting");socket->connectToService(address, QBluetoothUuid(serviceUuid) ,QIODevice::ReadWrite);} }void BluetoothProxy::readBluetoothDataEvent() {QByteArray line = socket->readAll();if(line.size() > 4)return;blueArray.append(line);if(blueArray.size() < 4 && blueArray.size()%4 != 0)return;//qDebug()<<blueArray.size();for(int i = 0; i< blueArray.size();i++){if(blueArray.at(i) == 0x01 && blueArray.at(i+1) == 0x01 && blueArray.at(i+2) == 0x01 && blueArray.at(i+3) == 0x03){emit redTeam();}else if(blueArray.at(i) == 0x01 && blueArray.at(i+1) == 0x02 && blueArray.at(i+2) == 0x01 && blueArray.at(i+3) == 0x04){emit blueTeam();}/*else if(blueArray.at(i) == 3 && blueArray.at(i+1) == 4 && blueArray.at(i+2) == 5 && blueArray.at(i+3) == 0x0f){count = 0;qDebug()<<"------------------------------------------>";}*/}if(blueArray.size() >= 4){blueArray.clear();} }void BluetoothProxy::bluetoothConnectedEvent() {qDebug() << "The android device has been connected successfully!";emit backendRunMesgPost("connected successfully"); }void BluetoothProxy::bluetoothDisconnectedEvent() {qDebug() << "The android device has been disconnected successfully!";emit backendRunMesgPost("disconnected");emit speakString("藍牙已斷開連接"); }

?

總結

以上是生活随笔為你收集整理的Qt 蓝牙通讯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。