한국어
Qt
 

RFCOMM 프로토콜은 아래 그림과 같이 L2CAP 프로토콜을 기반으로 직렬포트를 에뮬레이트 한 것이고 Bluetooth SPP(serial port profile)는 RFCOMM 프로토콜을 기반으로한다.

BTProtocolStack.png

 

Qt Bluetooth API는 Bluetooth 직렬 포트 프로파일 (SPP)을 지원하므로 블루투스를 이용해서 시리얼 통신으로 데이터를 송,수신 할 수있다.

 

Qt기본 예제 응용프로그램을 이용해서 간단한 테스트를 해볼 것이다. 테스트는 Qt Bluetooth API를 사용하는 안드로이드 응용프로그램과 PC(Windows10) COM (Serial Port)를 열어서 데이터를 주고받는다.

 

1. 먼저 윈도우즈에서 안드로이드 Qt 개발환경을 준비한다. (안드로이드 Qt 프로그래밍 포스트를 참고한다.)

 

2. QtCreator를 열고 예제 중 Bluetooth Chat Example을 열고 chatserver.cpp에서 serviceUuid를 다음과 같이 수정한다.

(이 UUID는 SerialPort에 해당하는 기본 UUID이다. 더많은 정보는 이곳을 방문하여 얻을 수 있다.)

static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB");

 

3. 수정 후 안드로이드 장치에 배포하기전에 windows pc와 블루투스 페어링 및 연결한다.

 

4. 블루투스 연결 후 안드로이드 장치를 호스트에 USB로연결하고 컴파일 및 배포하면 아래 화면처럼 실행된다.

photo_2019-02-17_23-09-48.jpg

 

안드로이드 응용프로그램은 QBluetoothServer를 생성하고 listen함으로써 Chat 서버로 동작되며 요청에 응답할 수있다. 

rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
bool result = rfcommServer->listen(localAdapter);

 

시리얼 통신 데이터를 쓰거나 읽기위해서 QBluetoothSocket 클래스를 사용하며 아래는 Chat서버의 쓰기 구현 코드이다.

void ChatServer::sendMessage(const QString &message)
{
    QByteArray text = message.toUtf8() + '\n';

    for (QBluetoothSocket *socket : qAsConst(clientSockets))
        socket->write(text);
}

 

아래는 수신된 데이터를 읽기 위한 예제소스코드이다. QBluetoothSocket 클래스의 readyRead 시그널을 사용한다.

QBluetoothSocket *socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);
connect(socket, &QBluetoothSocket::readyRead, this, [=]() {
    while (socket->canReadLine()) {
        QByteArray line = socket->readLine();
    }
});

socket->connectToService(QBluetoothAddress(addr),QBluetoothUuid(serviceUuid));

 

1. 이제 블루투스 연결된 Windows PC의 블루투스 설정 - 추가 Bluetooth 옵션을 연다.

 

2. COM 포트 탭으로 이동 후 추가를 클릭한다.

 

송신을 선택하고 COM 포트를 사용할 장치를 클릭하면 해당 장치가보일 것이다.

Add_COM_Port.png

 

"Bt Chat Server"이름의 서비스를 확인할 수 있다.

 

확인을 클릭하면 아래 화면처럼 COM포트를 확인할 수 있다. 이제 확인을 클릭하여 추가를 완료한다.

Add_COM_Port_1.png

 

3. 다음으로 터미널 프로그램을 실행하고 위에서 확인한 COM 포트를 연다. (필자는 테라텀을 이용하였다)

Open_COM_Port.png

 

이제 준비가 다 되었으므로 안드로이드 응용프로그램에서 문자열을 입력 후 Send한다.

photo_2019-02-17_23-26-59.jpg

 

그러면 터미널 응용프로그램에서 아래 화면과 같이 문자열이 출력된다.

Received_Serial.png

 

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 87470
179 Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들 file makersweb 2019.10.13 356
178 Qt 6.4에 추가될 Qt Quick 3D Physics file makersweb 2022.08.07 427
177 Qt 응용프로그램에 Web 구성 요소를 표시 with Servo file makersweb 2024.04.27 448
176 HTTPS URL을 연결할 때 SslHandshakeFailedError 오류 makersweb 2022.07.31 455
175 Qt Android 앱에 AdMob 배너달기 file makersweb 2021.12.04 473
174 그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software) j2doll 2020.12.25 496
173 Binding 타입으로 객체 속성 간 묶기 makersweb 2022.03.04 508
172 Qt Quick Controls 2에 네이티브 데스크탑 스타일 추가 file makersweb 2020.11.23 569
171 VirtualKeyboard 스타일 커스터 마이징 makersweb 2022.03.13 585
170 성능 고려 및 제안 사항 makersweb 2022.03.07 588
169 안드로이드용 Qt 6.2 makersweb 2021.10.02 597
168 앱을 종료할 때 QML 바인딩 오류를 피하는 방법 makersweb 2021.08.08 601
167 Qt Safe Renderer 개요 file makersweb 2022.09.08 623
166 응용프로그램 자동실행 설정 (on Windows) makersweb 2021.05.08 649
165 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available) j2doll 2020.06.21 671
164 Qt Marketplace 발표 makersweb 2019.12.02 676
163 QML과 코루틴(Coroutines) makersweb 2020.11.03 681
162 clazy 로 13개의 시그널, 슬롯 오류 해결 makersweb 2022.08.23 681
161 QProcess 보안 권고 리뷰 file makersweb 2022.09.18 682
160 Qt Creator 에서 GitHub Copilot 사용하기 file makersweb 2024.04.13 701