Python pybluez
socket
파이썬에 기본 내장되어있는 socket 모듈을 통해 간단한 블루투스 통신을 구축 할 수 있습니다.
단 참고할 문서가 거의 없기 때문에 단순 통신 기능 외에는 프로그램 개발이 어려울 수 있습니다.
pybluez 설치
sudo apt install -y bluez bluetooth libbluetooth-dev python3-pip \
&& python3 -m pip install pybluez
BLE 추가 설치
sudo apt install -y pkg-config libboost-python-dev \
libboost-thread-dev libglib2.0-dev python3-dev \
&& python3 -m pip install pybluez\[ble\]
bluetooth.service 변경
실행 옵션으로 -C(--compat)를 추가합니다.
$ sudo vim /lib/systemd/system/bluetooth.service
...
ExecStart=/usr/lib/bluetooth/bluetoothd -C
...
SDP: Service Discovery Protocol
echo -e '\nsudo chmod 777 /var/run/sdp' >> ~/.bashrc
sudo reboot
블루투스 컨트롤러
아래 명령을 통해 블루투스 장치명과 MAC 주소를 확인합니다.
$ hcitool dev
Devices:
hci0 00:1A:7D:DA:71:13
아래 명령을 통해 블루투스 장치인 hci0의 스캔을 허용할 수 있습니다.
sudo hciconfig hci0 piscan
bluetoothctl
bluetoothctl 명령어를 통해 블루투스 장치를 제어할 수 있습니다.
$ bluetoothctl
[NEW] Controller 18:1D:EA:36:DA:89 hhk7734 [default]
Agent registered
[bluetooth]# help
Menu main:
Available commands:
-------------------
advertise Advertise Options Submenu
scan Scan Options Submenu
gatt Generic Attribute Submenu
list List available controllers
show [ctrl] Controller information
select <ctrl> Select default controller
devices List available devices
paired-devices List paired devices
system-alias <name> Set controller alias
reset-alias Reset controller alias
power <on/off> Set controller power
pairable <on/off> Set controller pairable mode
discoverable <on/off> Set controller discoverable mode
agent <on/off/capability> Enable/disable agent with given capability
default-agent Set agent as the default one
advertise <on/off/type> Enable/disable advertising with given type
set-alias <alias> Set device alias
scan <on/off> Scan for devices
info [dev] Device information
pair [dev] Pair with device
trust [dev] Trust device
untrust [dev] Untrust device
block [dev] Block device
unblock [dev] Unblock device
remove <dev> Remove device
connect <dev> Connect device
disconnect [dev] Disconnect device
menu <name> Select submenu
version Display version
quit Quit program
exit Quit program
help Display help about this program
[bluetooth]#
정보
정보가 안나오면 sudo bluetoothctl 명령어를 통해 블루투스 장치를 제어할 수 있습니다.