Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt Bluetooth无法将套接字连接到设备_C++_Qt_Sockets_Bluetooth - Fatal编程技术网

C++ Qt Bluetooth无法将套接字连接到设备

C++ Qt Bluetooth无法将套接字连接到设备,c++,qt,sockets,bluetooth,C++,Qt,Sockets,Bluetooth,我正在尝试在笔记本电脑和三星智能触摸遥控器之间建立蓝牙连接。我的问题是无法将套接字连接到设备socket始终处于主机查找状态: #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // Creat

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

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

有人能告诉我我做错了什么吗

您是否尝试打开笔记本电脑上连接到笔记本电脑蓝牙模块的COM端口

您可以进入蓝牙设置->Com端口->添加新Com端口。然后使用超级终端或Teraterm打开此端口。现在尝试从Android手机连接


希望这能奏效

您是否尝试打开笔记本电脑上连接到笔记本电脑蓝牙模块的COM端口

您可以进入蓝牙设置->Com端口->添加新Com端口。然后使用超级终端或Teraterm打开此端口。现在尝试从Android手机连接


希望这能奏效

我有萨米问题。修复方法是在socket->connectToServiceaddr,1的参数中使用端口1


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

我遇到了samme问题。修复方法是在socket->connectToServiceaddr,1的参数中使用端口1

这告诉SDP通过SDP使用连接作为通道