当前位置:网站首页>QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent

QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent

2022-07-05 04:28:00 Friendly, friend

One 、 describe

This class is used to find nearby Bluetooth devices . 

Process of finding nearby Bluetooth devices :

  1. establish QBluetoothDeviceDiscoveryAgent Example ,
  2. Connect to deviceDiscovered() or finished() The signal ,
  3. And call start().
#include "widget.h"
#include "ui_widget.h"
#include <QBluetoothDeviceDiscoveryAgent>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,this, &Widget::deviceDiscovered);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
    qDebug() << " Discover new equipment :" << device.name() << '(' << device.address().toString() << ')';
}

void Widget::on_pushButton_clicked()
{
    qDebug() << " Begin your search ";
    discoveryAgent->start();
}

Two 、 Type members

1、enum QBluetoothDeviceDiscoveryAgent::DiscoveryMethod: This enumeration describes the method used by this class to search for Bluetooth devices . Not necessarily useful , See each operating system for details .

  • NoMethod: Can't search . No available methods are supported .
  • ClassicMethod: Search Bluetooth classic (BaseRate) equipment .
  • LowEnergyMethod: Search for low-power Bluetooth devices .

2、enum QBluetoothDeviceDiscoveryAgent::Error: Possible error conditions during Bluetooth device search .

  • NoError: There was no mistake .
  • PoweredOffError: Bluetooth adapter is turned off , Open it before searching .
  • InputOutputError: Error caused by writing or reading from device .
  • InvalidBluetoothAdapterError: The local adapter address passed does not match the physical adapter address of any local Bluetooth device .
  • UnsupportedPlatformError: Device search is not supported on the current platform . The error is a response to start() Set by calling .
  • UnsupportedDiscoveryMethod: The current platform does not support the set search method  DiscoveryMethod.
  • LocationServiceTurnedOffError: Location service is turned off . When turning off location services , Unable to use Bluetooth API.
  • UnknownError: An unknown error occurred .

3、 ... and 、 Member functions

1、QBluetoothDeviceDiscoveryAgent(const QBluetoothAddress &deviceAdapter, QObject *parent = nullptr)

Construct a new Bluetooth device search agent with a parent .

It USES deviceAdapter Perform device search . If deviceAdapter Is constructed by default , Then it generates QBluetoothDeviceDiscoveryAgent Object will use the local default Bluetooth adapter .

If specified deviceAdapter Not a local adapter , be error() Set to InvalidBluetoothAdapterError. Therefore, it is recommended to test the error flag immediately after using this constructor .

2、【 The signal 】void canceled()

When by calling stop() This signal will be sent when the device search is aborted .

3、【 The signal 】void deviceDiscovered(const QBluetoothDeviceInfo &info)

If I found info This signal is sent when the Bluetooth device described .

Once the collected device information , Will send a signal . however , As long as it hasn't been sent finished() The signal , Even for devices that have been discovered , Information gathering will also continue .

4、【 The signal 】void deviceUpdated(const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)

When received by info When describing the additional information of the Bluetooth device , It will send this signal .updatedFields Signs tell which information has been updated .

During search , Some information can change dynamically , Such as signal strength and manufacturer data . This signal informs the user , If the application is displaying this data , You can update it , Instead of waiting until the discovery is complete .

5、【 The signal 】void errorOccurred(QBluetoothDeviceDiscoveryAgent::Error error)

This signal is sent when an error occurs during Bluetooth device search .error The parameter describes the error that occurred .

6、【 The signal 】void finished()

Send this signal when the Bluetooth device search is completed . If the device search ends with an error , Will not send a signal .

7、void start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods)

Start Bluetooth device search ( If not already started ). The search method limits the scope of device search .

Be careful :methods Only determine the type of discovery , It doesn't mean filtering results . for example , Although the method is only set to LowEnergyMethod, But the search may still include classic Bluetooth devices . This may happen because previously cached search results may be merged into search results .

      void start()

Start Bluetooth device discovery ( If not already started ).

8、void stop()

Stop Bluetooth device search . Once the device search is cancelled , It will send out cancel() The signal .

9、QList<QBluetoothDeviceInfo> discoveredDevices()

Return to the list of all Bluetooth devices found .

10、QBluetoothDeviceDiscoveryAgent::Error error()

Return the last error .

11、QString errorString()

Return the readable description of the last error .

12、bool isActive()

Are you searching for Bluetooth devices .

13、int lowEnergyDiscoveryTimeout()

Returns the timeout applied to low-power Bluetooth device search ( millisecond ).

The value is -1 Indicates that the platform does not support this attribute , And the timeout of device search cannot be adjusted .

The value is 0 Indicates that you must pass stop() Endless search stopped manually .

14、void setLowEnergyDiscoveryTimeout(int timeout)

Set the maximum search time for searching low-power Bluetooth devices ( In Milliseconds ). If the timeout is 0, Then the search will always run , Until the call stop().

After restarting device search , The new timeout value will take effect . In addition, timeout does not affect classic Bluetooth device search .

For reliable low-power Bluetooth search , Use at least 40000 millisecond .

15、【static】QBluetoothDeviceDiscoveryAgent::DiscoveryMethods supportedDiscoveryMethods()

Return to the search methods supported by the current platform .

原网站

版权声明
本文为[Friendly, friend]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050425343194.html