1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

complete bluetooth manager support

This commit is contained in:
tianduanrui 2017-10-17 10:16:53 +08:00
parent 654ed4e678
commit 05879319ee
3 changed files with 142 additions and 17 deletions

View File

@ -1,6 +1,96 @@
#include "qqtbluetoothmanager.h"
QQtBluetoothManager::QQtBluetoothManager(QObject *parent) : QObject(parent)
QQtBluetoothManager* QQtBluetoothManager::_instance = NULL;
QQtBluetoothManager *QQtBluetoothManager::Instance(QObject *parent)
{
if(_instance)
return _instance;
_instance = new QQtBluetoothManager(parent);
return _instance;
}
void QQtBluetoothManager::changeAdapter(QBluetoothAddress &adapterAddress)
{
if(serviceDiscoveryAgent)
delete serviceDiscoveryAgent;
serviceDiscoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress, this);
connect(serviceDiscoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
this, SIGNAL(addService(QBluetoothServiceInfo)));
connect(serviceDiscoveryAgent, SIGNAL(finished()), this, SIGNAL(serviceScanFinished()));
if(deviceDiscoveryAgent)
delete deviceDiscoveryAgent;
deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(adapterAddress, this);
connect(deviceDiscoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this, SIGNAL(addDevice(QBluetoothDeviceInfo)));
connect(deviceDiscoveryAgent, SIGNAL(finished()), this, SIGNAL(deviceScanFinished()));
setAutoScan(true);
}
void QQtBluetoothManager::powerOff()
{
setHostMode(HostPoweredOff);
}
bool QQtBluetoothManager::isPowerOn()
{
HostMode mode = hostMode();
if(mode == HostPoweredOff)
return false;
return true;
}
bool QQtBluetoothManager::isDiscoverable()
{
HostMode mode = hostMode();
if(mode == HostDiscoverable)
return true;
return false;
}
void QQtBluetoothManager::setAutoScan(bool scan)
{
if(scan)
{
bAutoScan = true;
deviceDiscoveryAgent->start();
serviceDiscoveryAgent->clear();
serviceDiscoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
}
else
{
bAutoScan = false;
deviceDiscoveryAgent->stop();
serviceDiscoveryAgent->stop();
}
}
void QQtBluetoothManager::setDiscoverable(bool able)
{
if(able)
setHostMode(HostDiscoverable);
else
setHostMode(HostConnectable);
}
void QQtBluetoothManager::getRemoteService(QBluetoothAddress &remoteAddress)
{
serviceDiscoveryAgent->setRemoteAddress(remoteAddress);
serviceDiscoveryAgent->clear();
serviceDiscoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
}
QQtBluetoothManager::QQtBluetoothManager(QObject *parent) :
QBluetoothLocalDevice(parent),
serviceDiscoveryAgent(nullptr),
deviceDiscoveryAgent(nullptr),
bAutoScan(true)
{
QBluetoothAddress adapterAddress = address();
changeAdapter(adapterAddress);
}

View File

@ -1,17 +1,47 @@
#ifndef QQTBLUETOOTHMANAGER_H
#define QQTBLUETOOTHMANAGER_H
#include <QObject>
#include <qbluetoothglobal.h>
class QQtBluetoothManager : public QObject
#include <qbluetoothuuid.h>
#include <qbluetoothaddress.h>
#include <qbluetoothlocaldevice.h>
#include <qbluetoothserviceinfo.h>
#include <qbluetoothdevicediscoveryagent.h>
#include <qbluetoothservicediscoveryagent.h>
class QQtBluetoothManager : public QBluetoothLocalDevice
{
Q_OBJECT
public:
explicit QQtBluetoothManager(QObject *parent = nullptr);
static QQtBluetoothManager *Instance(QObject* parent = 0);
void changeAdapter(QBluetoothAddress& adapterAddress);
void powerOff();
bool isPowerOn();
bool isDiscoverable();
void setAutoScan(bool scan = true);
void setDiscoverable(bool able = true);
void getRemoteService(QBluetoothAddress& remoteAddress);
signals:
void addService(QBluetoothServiceInfo);
void addDevice(QBluetoothDeviceInfo);
void deviceScanFinished();
void serviceScanFinished();
public slots:
private:
explicit QQtBluetoothManager(QObject *parent = nullptr);
static QQtBluetoothManager* _instance;
QBluetoothServiceDiscoveryAgent *serviceDiscoveryAgent;
QBluetoothDeviceDiscoveryAgent *deviceDiscoveryAgent;
bool bAutoScan;
};
#endif // QQTBLUETOOTHMANAGER_H
#endif // QQTBLUETOOTHMANAGER_H

View File

@ -106,8 +106,8 @@ INCLUDEPATH += $$PWD/printsupport
INCLUDEPATH += $$PWD/sql
INCLUDEPATH += $$PWD/widgets
SOURCES += \
HEADERS += \
SOURCES =
HEADERS =
#root dir
win32 {
@ -156,20 +156,25 @@ SOURCES += \
HEADERS += \
$$PWD/network/qqtserialport.h
#bluetooth
SOURCES += \
$$PWD/network/qqtbluetooth.cpp
HEADERS += \
$$PWD/network/qqtbluetooth.h
DEFINES += __BLUETOOTH__
contains (DEFINES, __BLUETOOTH__) {
greaterThan(QT_MAJOR_VERSION, 4): QT += bluetooth
#bluetooth socket
SOURCES += \
$$PWD/network/qqtbluetooth.cpp
HEADERS += \
$$PWD/network/qqtbluetooth.h
#bluetooth manager
SOURCES += \
$$PWD/network/qqtbluetoothmanager.cpp
HEADERS += \
$$PWD/network/qqtbluetoothmanager.h
}
#ethnet manager
equals(QT_KIT, MIPS32) {
SOURCES += $$PWD/network/qqtethenetmanager.cpp
HEADERS += $$PWD/network/qqtethenetmanager.h
}
#bluetooth manager
SOURCES += \
$$PWD/network/qqtbluetoothmanager.cpp
HEADERS += \
$$PWD/network/qqtbluetoothmanager.h
#qextserialport support
#USE QEXTSERIALPORT
#DEFINES += __QTEXTSERIALPORT__