在Qt中扫描蓝牙设备

时间:2017-05-05 14:00:51

标签: android c++ qt sockets bluetooth

这是我的代码:我有一个名为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()中:需要左值   一元&操作数

2 个答案:

答案 0 :(得分:1)

因此,在sample documentation之后,我设法生成了一小段代码编译样本。

开始笔记:

  • 您需要QtCreator 5.2或更高版本来编译QBluetooth库。
  • 在.pro文件中添加Qt + = bluetooth
  • 使用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() << ')';
}

答案 1 :(得分:1)

我有标准示例的扫描问题 我解决了问题,删除了uuid filtr:

// m_discoveryAgent-&GT; setUuidFilter(UUID); 发现的设备。