Qt 蓝牙库基础

1.目的

使用QT自带的蓝牙功能库进行学习开发,了解蓝牙设备的工作原理和使用流程,目的搭建可以自由控制的蓝牙app,为以后的学习生活做基础。

2.本地设备

采用自己vivo Y53的手机作为实验对象。

使用到的QT蓝牙库的类名:  

QBluetoothLocalDevice

我们可以直接到QT软件帮助文档去搜索,如下图所示:

我们可以了解到这个类适用于QT5.2以上版本

主要我们在新建的工程中加上头文件和附加库声明。

1 #include <lQBluetoothLocalDevice.h>    //本地设备驱动头文件
2  
3 private4  
5 QBluetoothLocalDevice *localDevice;    //声明变量

在.pro中

QT += bluetooth

为变量声明新的空间:

localDevice = new QBluetoothLocalDevice();

然后我们就能调用里面的函数:

1 localDevice->address();  返回此蓝牙设备的MAC地址。
2 localDevice->hostMode();  返回此本地蓝牙设备的当前主机模式。
3 localDevice->powerOn(); 将设备返回hostMode()状态后,如果关闭电源,则为设备供电
4 localDevice->pairingStatus(info.address());返回地址的当前蓝牙配对状态(如果是未配对、配对或已授权。
5 localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff);将此本地蓝牙设备的主机模式设置为关闭模式。

3.搜索设备

用到第二个类:

QBluetoothDeviceDiscoveryAgent类-----------发现附近的蓝牙设备。

1 #include <QBluetoothDeviceDiscoverAgent.h>    //本地设备驱动头文件
2  
3 private4  
5 QBluetoothDeviceDiscoverAgent *discoveryAgent;    //声明变量
discoveryAgent= new QBluetoothLocalDevice();

1 discoveryAgent->error(); //返回最后一个错误
2 discoveryAgent->errorString(); //返回对最后一个错误的描述
3 discoveryAgent->inquiryType(); //返回查询类型
4 discoveryAgent->isActive(); //如果代理当前正在发现蓝牙设备,则返回true,否则返回false。
5 discoveryAgent->lowEnergyDiscoveryTimeout(); //返回一个以毫秒为单位的超时,该超时应用于蓝牙低能耗设备搜索。值为-1意味着平台不支持此属性,并且无法调整设备搜索的超时。返回值为0意味着一个永无止境的搜索,必须通过stop()手动停止搜索。

执行函数:

discoveryAgent->start();  //开始搜索设备
discoveryAgent->stop();  //停止搜索设备

信号槽:

connect(discoveryAgent, SIGNAL(finished()), this, SLOT(findFinish()));  //完成信号发出进行动作
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo)));   //当发现info描述的蓝牙设备时发出此信号来进行动作

而canceled()     //当设备发现被stop()调用中止时发出此信号。

同样我们也可以使用error()来根据不同的错误类型进行不同的操作。

4.蓝牙服务器代理

QBluetoothSocket:

操作同上:

1   socket->abort();//终止当前连接并重置套接字。与disconnectFromService()不同,这个函数立即关闭套接字,丢弃写缓冲区中的任何挂起的数据。
2 socket->disconnectFromService();//试图关闭套接字。如果有等待写入的挂起数据,QBluetoothSocket将进入ClosingState并等待所有数据被写入。最后,它将进入UnconnectedState并发出disconnected()信号。
3 socket->localAddress();//返回本地设备的地址。
4 socket->localName();//返回本地设备的名称。
5 socket->localPort();//如果可用,返回本地套接字的端口号,否则返回0。虽然有些平台可能有所不同,但套接字通常必须连接,以确保返回有效的端口号。
6 socket->setSocketDescriptor();//将套接字设置为使用socketDescriptor和socketType类型,socketType处于状态、socketState和模式、openMode。
socket->connectToService(address, QBluetoothUuid(serviceUuid) ,QIODevice::ReadWrite);

connect(socket,SIGNAL(readyRead()),this,SLOT(readBluetoothDataEvent()));  //每当有新数据可从设备当前的读取通道读取时,就会发出此信号一次。它只在新数据可用时才会再次发出,例如当网络数据的新负载到达您的网络套接字时,或者当一个新的数据块被添加到您的设备时。
connect(socket,SIGNAL(connected()),this,SLOT(bluetoothConnectedEvent()));//此信号在建立连接时发出。
connect(socket,SIGNAL(disconnected()),this,SLOT(bluetoothDisconnectedEvent()));//当套接字断开时发出此信号。

5.搜索连接蓝牙设备

显示可用的蓝牙:

 1 /* 在ListWidget上显示查找到的蓝牙设备 */
 2 void Widget::addBlueToothDevicesToList(const QBluetoothDeviceInfo &info)
 3 {
 4     QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
 5     //%1为蓝牙设备的地址,%2为蓝牙设备的名字
 6     QList<QListWidgetItem *> items = ui->listWidget->findItems(label, Qt::MatchExactly);
 7  
 8     if (items.empty()) {
 9         QListWidgetItem *item = new QListWidgetItem(label);
10         QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(info.address());
11        
12  /* 蓝牙状态pairingStatus,Pairing枚举类型 0:Unpaired没配对 1:Paired配对但没授权 2:AuthorizedPaired配对且授权 */
13         if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired )
14             item->setTextColor(QColor(Qt::green));
15         else
16             item->setTextColor(QColor(Qt::black));
17        
18  //输出显示
19         ui->listWidget->addItem(item);    
20     }
21 }

双击屏幕产生信号:

 1 /* 蓝牙连接 */
 2 void Widget::connectBLE(QListWidgetItem *item)
 3 {
 4     QString text = item->text();
 5     int index = text.indexOf(' ');//到空格之前的字节大小
 6     if (index == -1)
 7         return;
 8     QBluetoothAddress address(text.left(index));//返回一个子字符串,该子字符串包含字符串最左边的n个字符。
 9     QString name(text.mid(index + 1));//返回一个字符串,该字符串包含从指定的位置索引开始的n个字符。右边
10     QMessageBox::information(this,tr("Info"),tr("The device is connecting..."));//弹出提示框
11     socket->connectToService(address, QBluetoothUuid(serviceUuid) ,QIODevice::ReadWrite);//建立连接
12 }

原文地址:https://www.cnblogs.com/ybqjymy/p/14372897.html