Qt蓝牙无法将插座连接到设备

时间:2014-03-09 10:52:58

标签: c++ qt sockets bluetooth

我正在尝试在笔记本电脑和三星智能触控遥控器之间建立蓝牙连接。我的问题是我无法将套接字连接到设备(套接字始终处于“主机查找状态”):

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

有人可以告诉我我做错了什么吗?

2 个答案:

答案 0 :(得分:3)

您是否尝试在便携式计算机上打开连接到笔记本电脑蓝牙模块的COM端口?

您可以转到蓝牙设置 - &gt; Com Ports - &gt;添加一个新的COM端口。然后使用hypertermical或Teraterm打开此端口。现在尝试从您的Android手机连接......

希望这有效!

答案 1 :(得分:1)

我遇到了samme问题。解决方法是在socket->connectToService(addr, 1)的参数中使用端口1。

这告诉SDP通过SDP将连接用作通道。