Qt Bluetooth 개요

Qt Bluetooth API의 일반적인 사용 사례는 다음과 같습니다:

  • 로컬 Bluetooth 디바이스에 대한 정보를 검색합니다.
  • 범위 내에 있는 다른 Bluetooth 장치를 검색하고 해당 장치에 대한 정보를 검색합니다.
  • OBEX 객체 푸시 프로필(OPP)을 사용하여 원격 장치에 파일 푸시하기
  • 직렬 포트 프로필(SPP)을 사용해 RFCOMM 채널을 통해 원격 장치에 연결합니다.
  • SPP를 사용하여 들어오는 연결을 허용하는 RFCOMM 서버를 만듭니다.
  • 블루투스 저에너지 장치에 대한 사양을 검색합니다.
  • 블루투스 저에너지 장치에 연결합니다.
  • 블루투스 저에너지 장치로부터 광고를 수신합니다.

Android 및 Windows에서는 개체 푸시 프로필이 지원되지 않습니다.

참고: Windows에서 Qt로 RFCOMM 기능의 일부를 구성할 수 없습니다. 서비스의 ServiceClassIdsProtocolDescriptorList 은 자동으로 채워집니다. 따라서 이러한 필드에 대한 사용자 지정 값을 사용하여 서비스를 등록하면 Windows에서 예상한 결과가 나오지 않을 수 있습니다.

참고: 수신 신호 강도 표시기(RSSI)와 Bluetooth LE 장치에서 광고하는 제조업체별 데이터는 Win32 백엔드에서 지원되지 않습니다. 또한 Windows 설정을 통해 이전에 페어링된 디바이스만 찾을 수 있습니다.

다음 섹션에서는 위의 사용 사례에 Qt Bluetooth C++ API 클래스를 사용하는 방법을 설명합니다.

로컬 디바이스 정보 검색하기

Qt Bluetooth API는 크게 세 가지 용도로 사용됩니다. 첫 번째는 로컬 및 원격 디바이스 정보를 가져오는 것입니다. 디바이스 정보 검색의 첫 번째 단계는 디바이스에서 블루투스를 사용할 수 있는지 확인하고 로컬 디바이스 주소와 이름을 읽는 것입니다. QBluetoothLocalDevice 이 모든 정보를 제공하는 클래스입니다. 또한 이 클래스를 사용하여 블루투스를 켜거나 끄고, 장치의 가시성을 설정하고, 현재 연결 상태를 확인할 수 있습니다.

QBluetoothLocalDevice localDevice;
QString localDeviceName;

// Check if Bluetooth is available on this device
if (localDevice.isValid()) {

    // Turn Bluetooth on
    localDevice.powerOn();

    // Read local device name
    localDeviceName = localDevice.name();

    // Make it visible to others
    localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);

    // Get connected devices
    QList<QBluetoothAddress> remotes;
    remotes = localDevice.connectedDevices();
}

블루투스 장치 검색

QBluetoothLocalDevice 과 유사하게 원격 장치에 대한 유사한 정보를 제공하는 QBluetoothDeviceInfo 을 제공합니다. QBluetoothDeviceInfo 개체를 직접 만들어 데이터를 채울 수도 있지만, 더 쉬운 방법은 QBluetoothDeviceDiscoveryAgent 을 사용하여 연결 가능한 범위 내에서 보이는 블루투스 장치에 대한 자동 검색을 시작하는 것입니다.

void MyClass::startDeviceDiscovery() { // 검색 에이전트를 생성하고 해당 신호에 연결합니다.   QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo))); // 디스커버리 시작 discoveryAgent->start(); //...}// 로컬 슬롯에서 발견된 디바이스에 대한 정보를 읽습니다void MyClass::deviceDiscovered(const QBluetoothDeviceInfo &device){
    qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';

장치 간 데이터 교환

두 Bluetooth 지원 장치 간의 통신을 위한 보다 유연한 접근 방식은 가상 직렬 포트 연결을 생성하고 해당 연결을 통해 데이터를 자유롭게 교환하는 것입니다. 이는 직렬 포트 프로파일(SPP)로 수행할 수 있습니다. 직렬 포트 프로필은 Bluetooth 전송 프로토콜 RFCOMM을 통해 직렬 연결을 에뮬레이트합니다.

들어오는 SPP 연결을 수신하려면 QBluetoothServer 을 사용하여 들어오는 연결을 수신해야 합니다.

rfcommServer = 새로운 QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); connect(rfcommServer, &.QBluetoothServer::newConnection, this, QOverload<>::of(&ChatServer::clientConnected));bool result =  rfcommServer->listen(localAdapter);if (!result) {    qWarning() << "Cannot bind chat server to" << localAdapter.toString();
   return; }

QBluetoothSocket 을 사용하여 클라이언트 역할을 하는 다른 장치에서 이 서버에 연결합니다:

void ChatClient::startClient(const QBluetoothServiceInfo &remoteService) { if (socket) return; // 서비스에 연결socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);    qDebug() << "Create socket";
    소켓-> 연결 서비스(원격 서비스);    qDebug() << "ConnectToService done";

    connect(socket, &.QBluetoothSocket::readyRead, this, &ChatClient::readSocket); connect(socket, &QBluetoothSocket::connected, this, QOverload<>::of(&ChatClient::connected)); connect(socket, &QBluetoothSocket::disconnected, this, &ChatClient::disconnected); connect(socket, &.QBluetoothSocket::errorOccurred, this, &ChatClient::onSocketErrorOccurred); }

이러한 연결을 사용하면 모든 형태의 데이터를 양방향으로 교환할 수 있습니다. 게임이나 두 기기에서 애플리케이션의 두 인스턴스 간의 상태를 동기화하는 데 매우 적합합니다. 서버와 클라이언트를 구성하는 방법에 대한 자세한 설명은 QBluetoothServerQBluetoothSocket 클래스의 상세 설명 섹션을 참조하세요. SPP를 시작하기에 좋은 예는 블루투스 채팅 예제입니다.

블루투스 저에너지

블루투스 스마트라고도 하는 블루투스 저에너지는 에너지 소비가 적은 디바이스가 서로 통신할 수 있도록 하는 새로운 기술입니다. 이 기술 및 관련 Qt API에 대한 자세한 내용은 저에너지 블루투스 개요에서 확인할 수 있습니다.

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.