如何从蓝牙套接字中捕获错误并打印自定义消息?
How to catch error from bluetooth socket and print custom message?
我有一个问题,我是 python 的初学者,我有一个蓝牙连接脚本,我想修改它以捕获错误(如果发生错误)。
脚本(有效):
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print('Succeed!')
sock.close()
我怎么能抓住它?如果主机关闭,我会得到这个:
bluetooth.btcommon.BluetoothError: [Errno 112] Host is down
并且我只想打印我自己的错误消息。
在 Python 中通常的方法是使用 try
/except
这样的东西:
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
try:
sock.connect((bd_addr, port))
except bluetooth.btcommon.BluetoothError:
print('Host is down')
else:
print('Succeed!')
sock.close()
我有一个问题,我是 python 的初学者,我有一个蓝牙连接脚本,我想修改它以捕获错误(如果发生错误)。
脚本(有效):
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print('Succeed!')
sock.close()
我怎么能抓住它?如果主机关闭,我会得到这个:
bluetooth.btcommon.BluetoothError: [Errno 112] Host is down
并且我只想打印我自己的错误消息。
在 Python 中通常的方法是使用 try
/except
这样的东西:
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
try:
sock.connect((bd_addr, port))
except bluetooth.btcommon.BluetoothError:
print('Host is down')
else:
print('Succeed!')
sock.close()