0

I'm new to Qt framework. This is my first C++ implementation in real world applications. I was facing problem in preparing this bluetooth based application. I went through Qt documentation too, but it didn't work. Code is : CLASS HEADER

#ifndef MAINCLASS_H
#define MAINCLASS_H

#include <QObject>
#include <QBluetoothServiceDiscoveryAgent>
#include <QBluetoothServiceInfo>
#include <QBluetoothLocalDevice>

class MainClass : public QObject
{
    Q_OBJECT
public:
    explicit MainClass(QObject *parent = 0);

    ~MainClass();
    void startDiscovery(void);
signals:

public slots:
    void onDiscovery(const QBluetoothServiceInfo &serviceInfo);

private:
    QBluetoothServiceDiscoveryAgent *discoveryAgent;
    QBluetoothLocalDevice *bluetoothDevice;
};

#endif // MAINCLASS_H


                //  MEMBER DEFINITIONS


#include "mainclass.h"
#include <QDebug>


MainClass::MainClass(QObject *parent) : QObject(parent)
{
    bluetoothDevice = new QBluetoothLocalDevice();
    QBluetoothAddress bluetoothAddress = bluetoothDevice->address();

    discoveryAgent = new QBluetoothServiceDiscoveryAgent(bluetoothAddress);
    connect(discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
            this, SLOT(onDiscovery(QBluetoothServiceInfo)));
    discoveryAgent->setUuidFilter(QBluetoothUuid(QBluetoothUuid::ObexObjectPush));
    discoveryAgent->start();
    if(!discoveryAgent->isActive())
        qDebug()<<"Not active";
    if(discoveryAgent->error() != QBluetoothServiceDiscoveryAgent::NoError)
        qDebug()<<discoveryAgent->errorString();
}

MainClass::~MainClass()
{
    delete(discoveryAgent);
}

void MainClass::onDiscovery(const QBluetoothServiceInfo &serviceInfo)
{
    qDebug() << "Discovered service on"
             << serviceInfo.device().name() << serviceInfo.device().address().toString();
    qDebug() << "\tService name:" << serviceInfo.serviceName();
    qDebug() << "\tDescription:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
    qDebug() << "\tProvider:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
    qDebug() << "\tL2CAP protocol service multiplexer:"
             << serviceInfo.protocolServiceMultiplexer();
    qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();
}

Main Function

#include "mainclass.h"

int main()
{

    MainClass obj;
}

This piece of code wasn't showing lists of surrounding bluetooth devices. Any suggestions?

CherryGot
  • 49
  • 7
  • 1
    see this example: http://doc.qt.io/qt-5/qtbluetooth-btscanner-example.html – eyllanesc Apr 18 '17 at 17:40
  • I've taken excerpts from the example that you've mentioned. But still, it's not discovering devices available. – CherryGot Apr 18 '17 at 19:41
  • Have you tried this example without making any modifications? – eyllanesc Apr 18 '17 at 19:47
  • @FedUser I guess, the essential difference is that the Qt example has an `QApplication` instance and calls `QApplication::exec()`. Without, certain signals are not processed. (I realized something similar when I learnt how to use [`QGeoServiceProvider`](http://stackoverflow.com/questions/43259557/how-to-get-latitude-longitude-from-one-geo-address-using-qt-c-on-windows/43272961#43272961). – Scheff's Cat Apr 19 '17 at 05:50
  • You'll need a QCoreApplication and call exec() on it, without event loop this (asynchronous) code won't work – Frank Osterfeld Apr 19 '17 at 05:53
  • Thanks, that worked! – CherryGot Apr 20 '17 at 07:06

0 Answers0