Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detect connection of certain bluetooth device

Detect connection of certain bluetooth device

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrKew
    wrote on 21 Jun 2018, 13:00 last edited by MrKew
    #1

    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.

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 21 Jun 2018, 14:57 last edited by
      #2

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

      M 1 Reply Last reply 21 Jun 2018, 18:19
      0
      • M mrjj
        21 Jun 2018, 14:57

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

        M Offline
        M Offline
        MrKew
        wrote on 21 Jun 2018, 18:19 last edited by MrKew
        #3

        @mrjj Edited, thanks

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrKew
          wrote on 22 Jun 2018, 21:59 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gojir4
            wrote on 23 Jun 2018, 07:20 last edited by Gojir4
            #5

            @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.

            M 1 Reply Last reply 23 Jun 2018, 16:13
            0
            • G Gojir4
              23 Jun 2018, 07:20

              @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.

              M Offline
              M Offline
              MrKew
              wrote on 23 Jun 2018, 16:13 last edited by
              #6

              @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?

              1 Reply Last reply
              0

              1/6

              21 Jun 2018, 13:00

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved