扫描QT中的蓝牙设备
Scanning for Bluetooth Devices in Qt
这是我的代码:我有一个名为bluetoothCommunication
的类,其中我需要在其中使用一些方法来通过蓝牙交换数据。
bluetoothCommunication::bluetoothCommunication()
{
QBluetoothLocalDevice localDevice;
QString localDeviceName;
//Check if Bluetooth is available on this device
if(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();
}
}
void bluetoothCommunication::startDeviceDiscovery()
{
qDebug() << "Bluetooth discovery started";
//Create a discovery agent and connect to its signals
QBluetoothDeviceDiscoveryAgent* discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
QObject::connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo*)), &this, SLOT(deviceDiscovered(QBluetoothDeviceInfo*))); //HERE I HAVE AN ERROR //DON'T KNOW WHERE AND WHY
//Start a discovery
discoveryAgent -> start();
}
我试图修改QT文档(以下内容(中的官方示例,如果我复制并粘贴它在编译过程中,这给了我错误:
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();
//...
}
但是,我的修复尝试仍然行不通。带有错误消息:
在成员函数中
void bluetoothCommunication::startDeviceDiscovery()
:lvalue所需 Unary&
操作数
因此,在示例文档之后,我设法制作了您的代码编译的一小部分。
开始注:
- 您需要qtcreator 5.2或更高以编译Qbluetooth库。
- 在.pro文件中添加qt =蓝牙
- 使用QT文档提供的样本
- 在标题文件中包括所有库并添加方法定义。
bluetoothsample.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets bluetooth
TARGET = bluetoothSample
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
Ui::MainWindow *ui;
void startDeviceDiscovery();
private slots:
void deviceDiscovered(const QBluetoothDeviceInfo &device);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
void MainWindow::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 devices
void MainWindow::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
}
我有标准示例的扫描问题。
我解决了问题,删除uuid filtr:
//m_discoveryagent-> setUuidFilter(uuid(;找到的设备。
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- 检查不带转换的扫描格式
- Qt和C++:将QLineEdit添加到QTabWidget中
- 从数据库实时显示QT c++中的数据
- Qt Quick-如何仅从c++代码与qml属性交互
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 如何在Qt中取消捕获字符串
- 扫描QT中的蓝牙设备