2017-05-05 664 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);已找到 設備。