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. Unable to connect bluetooth socket to device
Forum Updated to NodeBB v4.3 + New Features

Unable to connect bluetooth socket to device

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.0k Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    qvazar
    wrote on last edited by
    #1

    I'm trying to establish bluetooth connection between laptop and Samsung Smart Touch Remote. My issue is that I can't connect socket to device(socket is always in "Service lookup state"):
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    // Create a discovery agent and connect to its signals
    QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
    this, SLOT(deviceDiscovered(const QBluetoothDeviceInfo&)));

    // Start a discovery
    discoveryAgent->start();
    

    }

    void MainWindow::deviceDiscovered(const QBluetoothDeviceInfo &device)
    {
    socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);
    socket->connectToService(QBluetoothAddress(device.address()),
    QBluetoothUuid(QBluetoothUuid::SerialPort));
    connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)),
    this, SLOT(socketError(QBluetoothSocket::SocketError)));
    connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(socketRead()));
    connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socketStateChanged()));
    }

    void MainWindow::socketRead()
    {
    QByteArray receivedData = socket->readAll();
    QMessageBox msg;
    msg.setText(QString(receivedData));
    msg.exec();
    }

    void MainWindow::socketConnected()
    {
    qDebug() << "Socket connected";
    qDebug() << "Local: "
    << socket->localName()
    << socket->localAddress().toString()
    << socket->localPort();
    qDebug() << "Peer: "
    << socket->peerName()
    << socket->peerAddress().toString()
    << socket->peerPort();
    }

    void MainWindow::socketDisconnected()
    {
    qDebug() << "Socket disconnected";
    socket->deleteLater();
    }

    void MainWindow::socketError(QBluetoothSocket::SocketError error)
    {
    qDebug() << "Socket error: " << error;
    }

    void MainWindow::socketStateChanged()
    {
    int socketState = socket->state();
    QMessageBox msg;
    if(socketState == QAbstractSocket::UnconnectedState)
    {
    msg.setText("unconnected");
    }
    else if(socketState == QAbstractSocket::HostLookupState)
    {
    msg.setText("host lookup");
    }
    else if(socketState == QAbstractSocket::ConnectingState )
    {
    msg.setText("connecting");
    }
    else if(socketState == QAbstractSocket::ConnectedState)
    {
    msg.setText("connected");
    }
    else if(socketState == QAbstractSocket::BoundState)
    {
    msg.setText("bound");
    }
    else if(socketState == QAbstractSocket::ClosingState)
    {
    msg.setText("closing");
    }
    else if(socketState == QAbstractSocket::ListeningState)
    {
    msg.setText("listening");
    }
    msg.exec();
    }
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Which OS/Qt are you running your code on ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qvazar
        wrote on last edited by
        #3

        Hi,
        I'm running my code on Ubuntu 13.10, Qt 5.2.1.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Have you already paired the remote and your computer ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qvazar
            wrote on last edited by
            #5

            Yes, both remote device and computer are paired, and I can discover device and get any information about it in my program, but I can't establish socket connection between them.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Might be a stupid question but are you sure your remote supports RfcommProtocol ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qvazar
                wrote on last edited by
                #7

                This is not a stupid question because I'm not sure that device supports Rfcomm protocol, but I guess it should. Also, I tried to create a socket this way:
                socket = new QBluetoothSocket(QBluetoothServiceInfo::L2capProtocol, this);
                But it didn't work either.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  No information about that in the remote documentation ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qvazar
                    wrote on last edited by
                    #9

                    Unfortunately, I didn't find anything.

                    1 Reply Last reply
                    0

                    • Login

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