Detect connection of certain bluetooth device



  • Hello,
    I want to call function when bluetooth device is connected

    Qt: 5.11.0
    I would like it to run on Android, but as emulator doesn't support bluetooth I am compiling for macOS
    Compiler: Desktop Qt 5.11.0 clang 64bit

    My code now looks like this:
    .h

    private:
        QBluetoothLocalDevice *localDevice;
        QLowEnergyController *controller;
    
    public slots:
        void connection(const QBluetoothAddress &adress);
    

    .cpp

    BackEnd::BackEnd(QObject *parent) : QObject(parent)
    {
        localDevice = new QBluetoothLocalDevice();
        connect(localDevice, SIGNAL(deviceConnected(QBluetoothAddress)), this, SLOT(connection(QBluetoothAddress)));
        qDebug() << "Signals and Slots: " << localDevice->address().toString();
    }
    
    void BackEnd::connection(const QBluetoothAddress &adress) {
        QBluetoothDeviceInfo device(adress, "Device", 0);
        qDebug() << device.address().toString();
        controller = QLowEnergyController::createCentral(device);
    }
    

    I managed to call function connection(QBluetoothAddress), but I have no idea how to access any services and other info (like UUID) about that device, when I know just mac address.

    I tried to create QBluetoothDeviceInfo and passing it to QLowEnergyController, but controller remote address stays "00:00:00:00:00:00"

    Overall I don't thing I am going right way, so my main question:

    How to detect connection of bluetooth device and get some info about it?

    I would appreciate any help with my approach or some other ideas how to achieve this.


  • Qt Champions 2017

    Hi
    For better suggestions from forum users, could you add
    platform ( android, windows etc) and compiler and Qt version ?



  • @mrjj Edited, thanks



  • Well, still no success
    My ideas was to search for all available services and then choose those with right Mac address, but QBluetoothServiceDiscoveryAgent doesn't seam to find anything

    .h

    #ifndef BACKEND_H
    #define BACKEND_H
    
    #include <QObject>
    #include <QBluetoothLocalDevice>
    #include <QBluetoothDeviceInfo>
    #include <QBluetoothServiceDiscoveryAgent>
    #include <QBluetoothServiceInfo>
    
    
    #include <QDebug>
    
    class BackEnd : public QObject
    {
        Q_OBJECT
    public:
        BackEnd(QObject *parent = nullptr);
        ~BackEnd();
    
    private:
        QBluetoothLocalDevice *localDevice;
        QBluetoothServiceDiscoveryAgent *discoveryAgent;
    
    public slots:
        void connection(const QBluetoothAddress &address);
        void serviceDiscovered(const QBluetoothServiceInfo &info);
        void discoveryFinished();
    };
    
    #endif // BACKEND_H
    
    

    .cpp

    BackEnd::BackEnd(QObject *parent) : QObject(parent)
    {
        localDevice = new QBluetoothLocalDevice();
        connect(localDevice, SIGNAL(deviceConnected(QBluetoothAddress)), this, SLOT(connection(QBluetoothAddress)));
        qDebug() << "Signals and Slots: " << localDevice->address().toString();
    
        discoveryAgent = new QBluetoothServiceDiscoveryAgent(localDevice->address());
        connect(discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
        connect(discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
    }
    
    BackEnd::~BackEnd() {
        delete localDevice;
        delete discoveryAgent;
    }
    
    void BackEnd::connection(const QBluetoothAddress &address) {
        qDebug() << "Connection: " << address.toString();
      
        discoveryAgent->setRemoteAddress(address);
        discoveryAgent->setUuidFilter();
        discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
    }
    
    void BackEnd::serviceDiscovered(const QBluetoothServiceInfo &info) {
        QBluetoothDeviceInfo device = info.device();
        qDebug() << "Device: " << device.name() << device.deviceUuid().toString() << device.address().toString();
        qDebug() << "Service: " << info.serviceName() << info.serviceName();
    }
    
    void BackEnd::discoveryFinished() {
        qDebug() << "Finished";
        //discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
    }
    

    Any help appreciated, how to get DeviceInfo or LowEnergyController of already connected device?



  • @MrKew Hi,
    Can you try without giving the address to the constructor ? and also to test the error flag ?

    discoveryAgent = new QBluetoothServiceDiscoveryAgent;
    qDebug()  << discoveryAgent->errorString();
    

    You can also connect to the signal QBluetoothDeviceDiscoveryAgent::error().

    The doc says: (both for QBluetoothServiceDiscoveryAgent and QBluetoothDeviceDiscoveryAgent)

    It uses deviceAdapter for the service search. If deviceAdapter is default constructed the resulting QBluetoothServiceDiscoveryAgent object will use the local default Bluetooth adapter.

    If a deviceAdapter is specified that is not a local adapter error() will be set to InvalidBluetoothAdapterError. Therefore it is recommended to test the error flag immediately after using this constructor.



  • @Gojir4 Thanks for answer,
    sadly still no services discovered and errorString is empty, no error is emitted through error signal.

    But I think it is because my device (and all available bluetooth devices in range) are Low Energy as documentation says:

    The service discovery may find Bluetooth Low Energy services too if the target device is a combination of a classic and Low Energy device. Those devices are required to advertise their Low Energy services via SDP. If the target device only supports Bluetooth Low Energy services, it is likely to not advertise them via SDP. The QLowEnergyController class should be utilized to perform the service discovery on Low Energy devices.

    But it seems to me, that getting QLowEnergyController of already connected device is not possible.
    So how am I supposed to do that?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.