Unable to connect bluetooth socket to device



  • 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();
    }
    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Which OS/Qt are you running your code on ?



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


  • Lifetime Qt Champion

    Have you already paired the remote and your computer ?



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    No information about that in the remote documentation ?



  • Unfortunately, I didn't find anything.


Log in to reply
 

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