2017-05-05 2 views
-1

Hier ist mein Code: Ich habe eine Klasse namens bluetoothCommunication, in der ich einige Methoden für den Austausch von Daten über Bluetooth platzieren muss.Scannen nach Bluetooth-Geräten in 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(); 
} 

Ich habe versucht, das offizielle Beispiel von qt Dokumentation zu ändern (die im folgenden), das gibt mir Fehler beim Kompilieren, wenn ich es kopieren und einfügen:

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(); 

//... 
} 

meinen Versuchs jedoch zu beheben es funktioniert immer noch nicht. Mit Fehlermeldung:

In Memberfunktion void bluetoothCommunication::startDeviceDiscovery(): L-Wert als einstellige & Operand erforderlich

+0

Kommentare sind nicht für längere Diskussionen; Diese Konversation wurde [in den Chat verschoben] (http://chat.stackoverflow.com/rooms/143526/discussion-on-question-by-elena-scanning-for-bluetooth-devices-in-qt). –

Antwort

1

Also, nach dem sample documentation ich es geschafft, eine kleine Probe des Codes Kompilierung zu erzeugen.

Anfang Hinweise:

  • Sie benötigen QtCreator 5.2 oder höher die QBluetooth Bibliotheken zu kompilieren.
  • In der .pro-Datei hinzufügen Qt + = bluetooth
  • Verwenden Sie die von Qt Dokumentation Probe
  • In der Header-Datei alle Bibliotheken umfassen und die Methoden Definitionen hinzuzufügen.

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

Funktioniert immer noch nicht :-( – Elena

+0

Ich verifiziert auf meinem eigenen Qt Creator. Hast du meine Startnotizen gesehen? Qt Creator muss Version 5.2 oder höher sein und in der .pro Datei musst du Qt + = Bluetooth hinzufügen. –

+0

Ja Ich habe, ohne Ergebnisse. – Elena

1

Ich habe Probleme mit Standart-Beispielen.
Ich löste Problem, entferne uuid filtr:

// m_discoveryAgent-> setUuidFilter (uuid); Geräte gefunden.