2017-05-05 125 views
-1

这里是我的代码:我有一个叫bluetoothCommunication的类,我需要放置一些通过蓝牙交换数据的方法。在Qt中扫描蓝牙设备

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():左值要求作为 一元&操作

+0

评论是不适合扩展讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/143526/discussion-on-question-by-elena-scanning-for-bluetooth-devices-in-qt)。 –

回答

1

因此,继sample documentation我设法生产出的代码编译一个小样本。

启动Notes:

  • 你需要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() << ')'; 
} 
+0

仍然无效:-( – Elena

+0

我使用自己的Qt Creator进行了验证。您是否看到我的开始笔记? Qt创建者必须是5.2或更高版本,并且.pro文件中必须添加Qt + =蓝牙。 –

+0

是我做了,没有结果。 – Elena

1

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

// m_discoveryAgent-> setUuidFilter(uuid);已找到 设备。