I am having difficulty troubleshooting a code error in one of my [projects of IoT ](https://www.interviewbit.com/blog/iot-projects/. I am trying to connect two devices via Bluetooth, but keep getting an error that reads "Connection failed: Bluetooth error: connection timed out." I am using Python 3.7 and the code I am using is posted below. Any help would be much appreciated!
import bluetooth
target_name = "MyDevice"
target_address = None
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print("found target bluetooth device with address ", target_address)
else:
print("could not find target bluetooth device nearby")
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((target_address, 3))
while 1:
data = sock.recv(1024)
print(data)
sock.close()
Unpublished comment
Viết câu trả lời