Windows下 python pybluez 安装使用记录
Posted Love丶伊卡洛斯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows下 python pybluez 安装使用记录相关的知识,希望对你有一定的参考价值。
前言
参考文章:Python蓝牙开发库–pybluez详解
系统:Windows10 企业版
IDE:pycharm2020
python版本:3.6.13 (注意我使用的版本)
环境创建: Anaconda3
电脑:台式机 外接蓝牙
pybluez github地址:https://github.com/pybluez/pybluez
官方文档:https://pybluez.readthedocs.io/en/latest/api/index.html#classes
安装
据参考博文说,3.6及以下版本可以正常安装,3.6以上就会报错Windows SDK相关错误(注意版本,实测过3.8版本 安装会报Windows sdk相关错误,解决方案网上也有很多,装sdk后修改相关参数,这里不再赘述),所以本文采用了py3.6
参考命令:pip install pybluez
测试
部分例程跑不起来,就没有列举
搜索蓝牙设备
#!/usr/bin/env python
# --*--coding=utf-8--*--
# pip install pybluez
import time
from bluetooth import *
#列表,用于存放已搜索过的蓝牙名称
alreadyFound = []
#搜索蓝牙
def findDevs():
foundDevs = discover_devices(lookup_names=True)
# 循环遍历,如果在列表中存在的就不打印
for (addr,name) in foundDevs:
if addr not in alreadyFound:
print("[*]蓝牙设备:" + str(name))
print("[+]蓝牙MAC:" + str(addr))
# 新增的设备mac地址定到列表中,用于循环搜索时过滤已打印的设备
alreadyFound.append(addr)
# 循环执行,每5秒执行一次
while True:
findDevs()
time.sleep(1)
读取本地蓝牙设备地址 read-local-bdaddr.py
官方例程:pybluez/examples/advanced/read-local-bdaddr.py
#!/usr/bin/env python3
"""PyBluez advanced example read-local-bdaddr.py
Read the local Bluetooth device address
"""
import bluetooth
if __name__ == "__main__":
print(bluetooth.read_local_bdaddr())
执行简单的设备查询,然后是每个设备的远程名称请求 发现的设备
官方例程:pybluez/examples/simple/inquiry.py
#!/usr/bin/env python3
"""PyBluez simple example inquiry.py
Performs a simple device inquiry followed by a remote name request of each
discovered device
Author: Albert Huang <albert@csail.mit.edu>
$Id: inquiry.py 401 2006-05-05 19:07:48Z albert $
"""
import bluetooth
print("Performing inquiry...")
nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True,
flush_cache=True, lookup_class=False)
print("Found devices".format(len(nearby_devices)))
for addr, name in nearby_devices:
try:
print(" - ".format(addr, name))
except UnicodeEncodeError:
print(" - ".format(addr, name.encode("utf-8", "replace")))
使用RFCOMM套接字的服务器应用程序的简单演示 rfcomm-server.py
官方例程:pybluez/examples/simple/rfcomm-server.py
#!/usr/bin/env python3
"""PyBluez simple example rfcomm-server.py
Simple demonstration of a server application that uses RFCOMM sockets.
Author: Albert Huang <albert@csail.mit.edu>
$Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $
"""
import bluetooth
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
# protocols=[bluetooth.OBEX_UUID]
)
print("Waiting for connection on RFCOMM channel", port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from", client_info)
try:
while True:
data = client_sock.recv(1024)
if not data:
break
print("Received", data)
except OSError:
pass
print("Disconnected.")
client_sock.close()
server_sock.close()
print("All done.")
ps:rfcomm-client.py 运行报错。。。
API
官方文档:https://pybluez.readthedocs.io/en/latest/api/index.html
以上是关于Windows下 python pybluez 安装使用记录的主要内容,如果未能解决你的问题,请参考以下文章
Windows下 python pybluez 安装使用记录
Python 连接蓝牙 python3.8 安装 pybluez