蓝牙库选择
pybluez
项目地址:https://github.com/pybluez/pybluez
文档地址:https://pybluez.readthedocs.io/en/latest/index.html
蓝牙支持:经典蓝牙 / BLE蓝牙【仅Linux】
平台支持:
Linux | Raspberry Pi | macOS | Windows |
---|---|---|---|
✔️ | ✔️ | ✔️ | ✔️ |
bluepy
项目地址:https://github.com/IanHarvey/bluepy
蓝牙支持:BLE蓝牙【仅Linux】
平台支持:Linux
gatt-python
项目地址:https://github.com/getsenic/gatt-python
蓝牙支持:BLE蓝牙【仅Linux】
平台支持:Linux
PySide6.QtBluetooth
Qt for Python的蓝牙,文档 https://doc.qt.io/qtforpython-6/PySide6/QtBluetooth/index.html
蓝牙支持 / 平台支持:
bleak
项目地址:https://github.com/hbldh/bleak
文档地址:https://bleak.readthedocs.io/en/latest/
蓝牙支持:BLE蓝牙【windows 10/Linux】
平台支持:
- Supports Windows 10, version 16299 (Fall Creators Update) or greater
- Supports Linux distributions with BlueZ >= 5.43 (See Linux backend for more details)
- OS X/macOS support via Core Bluetooth API, from at least OS X version 10.11
pybluez安装
pip安装
pip install pybluez
具体安装细节详见 https://github.com/pybluez/pybluez/blob/master/docs/install.rst
常见错误
错误一:使用废弃的 use_2to3 报错
安装错误
$ pip install --upgrade pybluez
Collecting pybluez
Using cached PyBluez-0.23.tar.gz (97 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
error in PyBluez setup command: use_2to3 is invalid.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
错误原因详见:PyBluez Issues#431
解决方案:使用如下命令从源代码安装
pip install git+https://github.com/pybluez/pybluez.git#egg=pybluez
错误二:源码安装没有对应的Microsoft Visual C++ 14.0库
$ pip install git+https://github.com/pybluez/pybluez.git#egg=pybluez
Collecting pybluez
Cloning https://github.com/pybluez/pybluez.git to c:\users\acer\appdata\local\temp\pip-install-mki8zoy9\pybluez_5660e54d6b4643c2a85546348792a2ee
Running command git clone --filter=blob:none --quiet https://github.com/pybluez/pybluez.git 'C:\Users\acer\AppData\Local\Temp\pip-install-mki8zoy9\pybluez_5660e54d6b4643c2a85546348792a2ee'
Resolved https://github.com/pybluez/pybluez.git to commit 4d46ce14d9e888e3b0c65d5d3ba2a703f8e5e861
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pybluez
Building wheel for pybluez (pyproject.toml) ... error
error: subprocess-exited-with-error
× Building wheel for pybluez (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [15 lines of output]
running bdist_wheel
running build
running build_py
creating build
creating build\lib.win-amd64-cpython-311
creating build\lib.win-amd64-cpython-311\bluetooth
copying bluetooth\ble.py -> build\lib.win-amd64-cpython-311\bluetooth
copying bluetooth\bluez.py -> build\lib.win-amd64-cpython-311\bluetooth
copying bluetooth\btcommon.py -> build\lib.win-amd64-cpython-311\bluetooth
copying bluetooth\macos.py -> build\lib.win-amd64-cpython-311\bluetooth
copying bluetooth\msbt.py -> build\lib.win-amd64-cpython-311\bluetooth
copying bluetooth\__init__.py -> build\lib.win-amd64-cpython-311\bluetooth
running build_ext
building 'bluetooth._msbt' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pybluez
Failed to build pybluez
ERROR: Could not build wheels for pybluez, which is required to install pyproject.toml-based projects
解决方式:从https://visualstudio.microsoft.com/visual-cpp-build-tools/
下载Microsoft C++ 生成工具
安装后重新执行指令。
$ pip install git+https://github.com/pybluez/pybluez.git#egg=pybluez
Collecting pybluez
Cloning https://github.com/pybluez/pybluez.git to c:\users\acer\appdata\local\temp\pip-install-n2w1b_hj\pybluez_e8804231d731412e974e0d8e7a4a56f4
Running command git clone --filter=blob:none --quiet https://github.com/pybluez/pybluez.git 'C:\Users\acer\AppData\Local\Temp\pip-install-n2w1b_hj\pybluez_e8804231d731412e974e0d8e7a4a56f4'
Resolved https://github.com/pybluez/pybluez.git to commit 4d46ce14d9e888e3b0c65d5d3ba2a703f8e5e861
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pybluez
Building wheel for pybluez (pyproject.toml) ... done
Created wheel for pybluez: filename=PyBluez-0.30-cp311-cp311-win_amd64.whl size=38986 sha256=a37ebad09bb1196f44d7a90900694c0ac1802c83efef5d3766a908dc6b23fd84
Stored in directory: C:\Users\acer\AppData\Local\Temp\pip-ephem-wheel-cache-dtnnjqfc\wheels\99\fe\57\0c682c42e37c808dc69125bf83b302f94976743c8cb038a845
Successfully built pybluez
Installing collected packages: pybluez
Successfully installed pybluez-0.30