扫描二维码
随时随地手机看文章




Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//创建搜索服务:https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.html
discoveryAgent =new QBluetoothDeviceDiscoveryAgent(this);
//设置BLE的搜索时间
discoveryAgent->setLowEnergyDiscoveryTimeout(20000);
connect(discoveryAgent,SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),this,SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo)));//找到设备之后添加到列表显示出来
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));
connect(discoveryAgent, SIGNAL(canceled()), this, SLOT(scanCanceled()));
connect(this, SIGNAL(returnAddress(QBluetoothDeviceInfo)), this, SLOT(createCtl(QBluetoothDeviceInfo)));
//开始进行设备搜索
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
//deviceDiscovered signals 对应的槽函数
void Widget::addBlueToothDevicesToList(const QBluetoothDeviceInfo