webOS Article/2. Luna Service 활용하기

Bluetooth 사용하기 1

Oregon 2021. 9. 14. 01:48

이 포스팅은 webOS OSE 개발자 사이트 : Bluetooth Guide를 참고하여 작성되었습니다.

 

 

Before you begin


1.  시작 전 프로젝트를 위한 준비물

  • webOS OSE 장치 (Raspberry Pi)
  • Host machine for SSH connection with the webOS OSE device
  • 아래의 어플을 다운받은 android 스마트폰 ( 구글 플레이 스토어에서 다운받으세요. )
    • Bluetooth SPP Manager 

*android를 권장합니다. ios에는 적절한 bluetooth terminal app을 찾기 어렵습니다. 

 

 

SPP ( Serial Port Profile ) 란 무엇인가


1. SPP ( Serial Port Profile )이란 무엇인가? 

직렬 포트 프로파일 (Serial Port Profile : SPP)이란, 가상 시리얼 포트를 설정하고 두 개의 Bluetooth 지원 장치를 연결하는 방법을 정의합니다.


 SPP는 아래와 같이 장치 A(Central)와 장치 B(Peripheral)의 두가지 역할을 정의합니다.

  •  장치 A ( Central ) - 검색과 연결을 시도하는 역할을 하는 장치입니다.
  •  장치 B ( Peripheral ) - 검색과 연결을 대기하는 장치입니다.

* 저희가 진행하는 프로젝트에서는 스마트폰이 Central device, 라즈베리파이(webos OSE device)가 Peripheral이 되는 것입니다.

 

 

SPP ( Serial Port Profile ) 실습하기


1. Bluetooth 켜기

  • 디바이스 추가하기
    ares-setup-device
    위의 명령어를 사용해 device를 추가해줍니다. ( 노트북과 같은 ip를 공유해야합니다. )

 

  • 추가해준 target device의 shell에 접속하기
    ares-shell -d <target device>​

 

  • 블루투스 켜기
    luna-send -f -n 1 luna://com.webos.service.bluetooth2/adapter/setState '{"powered":true}'​
    위의 명령어를 사용해 블루투스를 켜줍니다.

 

2. trusted device로 설정하기 

luna-send -f -n 1 luna://com.webos.service.bluetooth2/device/setState '{"address":"74:9E:F5:AF:D0:A8", "trusted":true}'​

 

address에는 모바일 기기의 MAC address를 입력해줍니다.

 

3. webOS device 이름 설정하기

luna-send -f -n 1 luna://com.webos.service.bluetooth2/adapter/setState '{"name":"cosmos"}'

 

4. Bluetooth가 검색이 가능하도록 설정하기

luna-send -f -n 1 luna://com.webos.service.bluetooth2/adapter/setState '{"discoverable":true}'

 

 

5. 모바일 기기에서 Bluetooth 연결하기

 

6. webOS device에서 Bluetooth 연결 확인하기

luna-send -f -n 1 luna://com.webos.service.bluetooth2/device/getStatus '{ "subscribe": true }' | grep "OO의 S20+" -C 15

올바르게 연결이 되었다면, 아래와 같은 return값이 반환됩니다.

paired에 true가 뜨면 연결 성공!

 

7. SPP channel 만들기 (#1 shell )

*현재 shell 창을 #1 shell 이라고 하겠습니다.

luna-send -i -f -a "com.luna.test" luna://com.webos.service.bluetooth2/spp/createChannel '{ "name":"service1", "uuid":"00001101-0000-1000-8000-00805f9b34fb", "subscribe":true }'

uuid는 webOS device의 고유 번호이므로 그대로 사용합니다.

 

8. Bluetooth SPP Manager 앱 실행  & device 연결

모바일 기기에서 Blutooth SPP Manager 앱을 실행합니다.

그후, cosmos device를 선택해 연결합니다.

connecting established가 뜨면 연결 성공!

 

BT MESSENGER 탭에 들어가면 cosmos connected! 메시지를 확인할 수 있습니다.

 

9. #1 shell에 들어가 "channelId" 정보를 복사해놓기

#1 shell에 들어가 channelid를 복사해 놓습니다.

 

10. 새 shell 창 띄우기 (#2 shell)

다른 창에서 target device의 shell (#2 shell)에 접속한 후, 다음의 명령어를 입력합니다.

여기서 두 번째 shell은 블루투스로 연결된 기기와 메시지를 주고받을 용도로 사용됩니다.

luna-send -i -a "com.luna.test" -f luna://com.webos.service.bluetooth2/spp/readData '{ "channelId": "005","subscribe": true}'

channelid에는 이전 단계에서 복사해놓은 숫자를 사용합니다.

 

11. 모바일 기기에서 메세지 보내기

모바일 기기에서 Bluetooth SPP Manager app > BT MESSENGER > Add text 에 hello 메시지를 입력한 후 Send Message 버튼을 누릅니다.

 

12. 받은 메시지 확인하기

#2 shell 에 돌아가서 받은 메시지를 확인합니다.

data 결과 값이 화면과 같다면 성공!

화면의 data값은 저희가 전송한 'hello'라는 단어가 base64 format으로 인코딩 된 것입니다.

 

13. 메시지 보내기

Ctrl + C를 눌러 read mode에서 빠져나오고, write mode로 메시지를 전송합니다.

luna-send -f -n 1 -a "com.luna.test" luna://com.webos.service.bluetooth2/spp/writeData '{ "channelId": "005","data": "aGVsbG8NCg0K"}'

*data 값에 다른 단어의 base64 format 인코딩을 안다면 hello 말고 다른 단어들도 전송할 수 있습니다.

 

모바일 기기에서 보낸 메세지가 잘 도착했는지 확인해봅니다.

cosmos (webOS device)로 부터 hello라는 메세지가 잘 도착했음을 알 수 있습니다.

성공!!