Please tell me why when I run the code I get an error? Here is the code:

import bluetooth bd_addr = "00:03:b9:b3:c1:38" port = 1 sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) sock.connect((bd_addr, port)) sock.send("hello!!") sock.close() 

But the error itself:

Traceback (most recent call last): File "D: \ Desktop \ parpar.py", line 8, in sock.connect ((bd_addr, port)) File "D: \ 2012 \ lib \ site-packages \ bluetooth \ msbt.py ", line 72, in connect bt.connect (self._sockfd, addr, port) OSError

My bluetooth adapter works and everything is fine with him. Tell me how to fix this error?


In short, I tried to change the version of the python to an older 2.7 and saw that errors at least explain something, unlike last time.

Here is my code that I launched and in which I got a result, as well as an error.

 from bluetooth import * server_sock=BluetoothSocket( RFCOMM ) port = 0 server_sock.bind(("",PORT_ANY)) server_sock.listen(1) server=BluetoothSocket( RFCOMM ) server.connect(('00:11:22:98:76:54',PORT_ANY)) server.send("hello!!") server.close() client_sock,address = server_sock.accept() client_sock.close() server_sock.close() server.close() 

Traceback (most recent call last): File "D: \ Desktop \ fhg.py", line 9, in sock.connect ((bd_addr, PORT_ANY)) File "D: \ pythonchik \ lib \ site-packages \ bluetooth \ msbt.py ", line 72, in connect bt.connect (self._sockfd, addr, port) IOError: The required address for its context is incorrect.

    1 answer 1

    It was hard to find this library, would leave a link to it. Try to connect using a different protocol and port:

     from bluetooth import * port = 0xFA0 # 4000 sock = BluetoothSocket(L2CAP) bt_addr = input('Введите адрес устройства: ') print("Попытка соединения с устройством %s" % bt_addr) try: sock.connect((bt_addr, port)) print("Соединение успешно") except: print('Connection error') while True: data = input() if(len(data) == 0): break sock.send(data) data = sock.recv(1024) print("Получено: ", data) 

    UPD

    Search for devices:

     from bluetooth.ble import DiscoveryService service = DiscoveryService() devices = service.discover(2) for address, name in devices.items(): print("name: {}, address: {}".format(name, address)) 
    • I learned that L2CAP is not supported for some reason on python 2 or python 3 for windows 7. Here is a link to the topic: stackoverflow.com/questions/24460076/pyblues-bluetooth-l2cap And in particular, it did not escape me (I have the same error as the one in the subject of the link). And by the way: the module is called pybluez. Here is a link to it: pypi.python.org/pypi/PyBluez . And please tell me if there are other solutions, but with RFCOMM for fix this error? - Kirya 005
    • @Kirya005 unfortunately, I can’t tell you exactly the solution. I do not have the opportunity to test the code. The only bluetooth device I have is a netbook, but Linux is on it. I think that you should try to reinstall the device, or check its settings in the control panel and try to at least look for devices nearby: in response (update now, taken from documentation) - kitscribe