TypeError: must be str, not NoneType in bluetooth

44 views Asked by At

`

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

This code gives this error, can you please help?

In this code, I want python to connect to the mac address specified by bluetooth, but this code gives an error.

1

There are 1 answers

0
LITzman On

When joining items using the + operator, they have to be of the same type. That means that if bluetooth.lookup_name(i) returns a result which isn't a string (a NoneType in your case) than the concatenation fails.

You can use format string to print the result anyway -

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

This will work even if not all of the arguments of format are strings.