当我尝试连接蓝牙时出错。我怎样才能修好它?(连接板蓝牙HC06)

2024-04-25 02:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

所以当我尝试连接蓝牙时。这是我的代码:

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

请帮我修一下。谢谢你


Tags: thenameinimporttrueportconnectcall