Skip to content

Pybluez

Python extension module allowing access to system Bluetooth resources.

Installation

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]

Device Reader

RFCOMM 연결

from bluetooth import BluetoothSocket, RFCOMM
from time import sleep

socket = BluetoothSocket(RFCOMM)
result = socket.connect(("98:DA:60:05:EC:2F", 1))
print(f"Connect result: {result}")

while True:
    data = socket.recv(1024)
    print(f"receive data: {data}")
    sleep(1)
    if data == "q":
        print(quit)
        break

socket.close()

Troubleshooting

use_2to3 is invalid

Collecting PyBluez==0.23
Using cached PyBluez-0.23.tar.gz (97 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
error in PyBluez setup command: use_2to3 is invalid.

It looks like setuptools>=58 breaks support for use_2to3:

setuptools changelog for v58

So you should update setuptools to setuptools<58 or avoid using packages with use_2to3 in the setup parameters.

I was having the same problem, pip==19.3.1

또는 다음과 같이 setuptools 버전은 낮춘다.

pip install "setuptools<58.0.0"

See also

Favorite site