python - 尝试连接蓝牙时出现 OSError。我该如何解决?(连接板蓝牙HC-06)
问题描述
所以当我尝试连接蓝牙时。这是我的代码:
import subprocess
import bluetooth
nearby_devices = bluetooth.discover_devices(duration=4, lookup_names=True, flush_cache=True,
lookup_class=False)
name = "HC - 06" # Device name
addr = "20:17:11:20:28:17" # Device Address
port = 1 # RFCOMM port
passkey = "1234" # passkey of the device you want to connect
# kill any "bluetooth-agent" process that is already running
subprocess.call("kill -9 `pidof bluetooth-agent`",shell=True)
# Start a new "bluetooth-agent" process where XXXX is the passkey
status = subprocess.call("bluetooth-agent " + passkey + " &",shell=True)
# Now, connect in the same way as always with PyBlueZ
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
except bluetooth.btcommon.BluetoothError as err:
# Error handler
pass
它有这样的错误:
Traceback (most recent call last):
File "e:/Code/number.py", line 20, in <module>
s.connect((addr,port))
File "C:\Users\huuhu\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bluetooth\msbt.py", line 96, in connect
bt.connect (self._sockfd, addr, port)
OSError: A
请帮我修复它。谢谢 !!!
解决方案
推荐阅读
- javascript - 带有 data-url href 的元素不会显示在 svg 中
- oauth - 使用 passport-auth0-openidconnect 时,如何获取完整的访问令牌来调用 /api/v2/users/?
- google-sheets - 基于跨列和跨行的单个单元格的条件格式
- visual-studio - 由于 CRC 错误导致 nuget 配置错误,.NET 框架解决方案开始失败
- python - 无法在反向 shell 中执行 cd 命令
- python - 如何在 FloatLayout,Python + Kivy 的按钮下添加方法
- sql-server - 为什么 Microsoft.SqlServer.Management.SqlParser.Parser 为此查询创建的 AST 包含 SqlTableValuedFunctionRefExpression?
- python - 类型提示数组
- r - 在为选定列分配变量后,如何删除一些具有 NA 值的行?
- c - 根据条件从二叉搜索树中删除元素