扫描QT中的蓝牙设备

Scanning for Bluetooth Devices in Qt

本文关键字:QT 扫描      更新时间:2023-10-16

这是我的代码:我有一个名为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(;找到的设备。