TypeError:必须是str,而不是蓝牙中的NoneType



`

def arduino_connect():
global sock
print("Cihazlar axtarılır....")
nearby_devices = bluetooth.discover_devices()
num = 0
for i in nearby_devices:
num+=1
print(str(num)+":"+bluetooth.lookup_name(i)+" MAC: "+i)
if i=="00:21:13:00:EF:19":
selection = num-1
bd_addr = nearby_devices[selection]
port = 1
print("Sən seçdin:" +bluetooth.lookup_name(bd_addr))
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr,port))

`

Traceback (most recent call last):
File "ordubot.py", line 92, in <module>
test(wake)
File "ordubot.py", line 81, in test
response(voice)
File "ordubot.py", line 57, in response
arduino_connect()
File "ordubot.py", line 38, in arduino_connect
print(str(num)+":"+bluetooth.lookup_name(i)+" MAC: "+i)
TypeError: must be str, not NoneType

这个代码给出了这个错误,你能帮忙吗?

在这段代码中,我希望python连接到蓝牙指定的mac地址,但这段代码给出了一个错误。

使用+运算符联接项时,它们必须是相同的类型。这意味着,如果bluetooth.lookup_name(i)返回的结果不是字符串(在您的情况下是NoneType),则串联将失败。

你可以使用格式字符串打印结果无论如何-

print(f"{}:{} MAC: {}".format(num, bluetooth.lookup_name(i), i)

即使不是format的所有参数都是字符串,这也会起作用。

相关内容

最新更新