[英]Scanning for Bluetooth Devices in Qt
這是我的代碼:我有一個名為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()
:左值必須為一元&
操作數
因此,按照示例文檔,我設法生成了代碼編譯的一個小示例。
開始說明:
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); 找到設備。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.