gpt4 book ai didi

c++ - Qt蓝牙无法将套接字连接到设备

转载 作者:搜寻专家 更新时间:2023-10-31 01:05:40 26 4
gpt4 key购买 nike

我正在尝试在笔记本电脑和 Samsung Smart Touch Remote 之间建立蓝牙连接。我的问题是我无法将套接字连接到设备(套接字始终处于“主机查找状态”):

#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 端口。然后使用 hypertermical 或 Teraterm 打开这个端口。现在尝试从您的 Android 手机连接...

希望这能奏效!

关于c++ - Qt蓝牙无法将套接字连接到设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22281291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com