13,000
回編集
897行目: | 897行目: | ||
#include <QBluetoothSocket> | #include <QBluetoothSocket> | ||
#include <QDebug> | #include <QDebug> | ||
class BluetoothConnection : public QObject | class BluetoothConnection : public QObject | ||
904行目: | 903行目: | ||
private: | private: | ||
QBluetoothSocket socket; | |||
// | // エラーコード | ||
QString errorToString(QBluetoothSocket::SocketError error) | QString errorToString(QBluetoothSocket::SocketError error) | ||
{ | { | ||
920行目: | 919行目: | ||
} | } | ||
// | // 接続状態 | ||
QString stateToString(QBluetoothSocket::SocketState state) | QString stateToString(QBluetoothSocket::SocketState state) | ||
{ | { | ||
935行目: | 934行目: | ||
public: | public: | ||
explicit BluetoothConnection(QObject* parent = nullptr) : QObject(parent) | explicit BluetoothConnection(QObject *parent = nullptr) : QObject(parent), socket(QBluetoothServiceInfo::RfcommProtocol, this) | ||
{ | { | ||
// 各種シグナルとスロットの接続 | |||
connect(&socket, &QBluetoothSocket::connected, this, &BluetoothConnection::onConnected); | |||
connect(&socket, &QBluetoothSocket::disconnected, this, &BluetoothConnection::onDisconnected); | |||
connect(&socket, &QBluetoothSocket::errorOccurred, this, &BluetoothConnection::onError); | |||
connect(&socket, &QBluetoothSocket::readyRead, this, &BluetoothConnection::onDataReceived); | |||
connect(&socket, &QBluetoothSocket::stateChanged, this, &BluetoothConnection::onStateChanged); | |||
} | |||
~BluetoothConnection() | |||
{ | |||
disconnect(); | |||
} | } | ||
957行目: | 952行目: | ||
void connectToDevice(const QBluetoothAddress &address, quint16 port) | void connectToDevice(const QBluetoothAddress &address, quint16 port) | ||
{ | { | ||
if (socket.state() == QBluetoothSocket::ConnectedState) { | |||
qDebug() << "既に接続済み"; | |||
return; | |||
} | |||
qDebug() << "デバイスに接続します: " << address.toString(); | |||
qDebug() << "ポート: " << port; | |||
socket.connectToService(address, port); | |||
} | } | ||
976行目: | 965行目: | ||
void disconnect() | void disconnect() | ||
{ | { | ||
if (socket.state() != QBluetoothSocket::UnconnectedState) { | |||
qDebug() << "接続を切断..."; | |||
socket.disconnectFromService(); | |||
} | } | ||
} | } | ||
991行目: | 974行目: | ||
bool sendData(const QByteArray &data) | bool sendData(const QByteArray &data) | ||
{ | { | ||
if (socket.state() != QBluetoothSocket::ConnectedState) { | |||
qDebug() << "送信エラー: 接続されていません"; | |||
return false; | |||
} | |||
qint64 bytesWritten = socket.write(data); | |||
if (bytesWritten == -1) { | |||
qDebug() << "送信エラー: データの書き込みに失敗"; | |||
return false; | |||
} | |||
qDebug() << bytesWritten << "バイトデータの送信完了"; | |||
return true; | |||
} | } | ||
private slots: | private slots: | ||
// | // 接続確立時 | ||
void onConnected() | void onConnected() | ||
{ | { | ||
qDebug() << "接続が確立"; | qDebug() << "接続が確立"; | ||
qDebug() << " ローカルアドレス:" << socket | qDebug() << " ローカルアドレス:" << socket.localAddress().toString(); | ||
qDebug() << " リモートアドレス:" << socket | qDebug() << " リモートアドレス:" << socket.peerAddress().toString(); | ||
} | } | ||
// | // 切断時 | ||
void onDisconnected() | void onDisconnected() | ||
{ | { | ||
1,027行目: | 1,004行目: | ||
} | } | ||
// | // エラー発生時 | ||
void onError(QBluetoothSocket::SocketError error) | void onError(QBluetoothSocket::SocketError error) | ||
{ | { | ||
1,033行目: | 1,010行目: | ||
} | } | ||
// | // データ受信時 | ||
void onDataReceived() | void onDataReceived() | ||
{ | { | ||
QByteArray data = socket | QByteArray data = socket.readAll(); | ||
qDebug() << "データを受信: " << data.size() << "バイト"; | qDebug() << "データを受信: " << data.size() << "バイト"; | ||
1,043行目: | 1,020行目: | ||
} | } | ||
// | // 接続状態変更時 | ||
void onStateChanged(QBluetoothSocket::SocketState state) | void onStateChanged(QBluetoothSocket::SocketState state) | ||
{ | { |