0

I tried to connect my headphone with using Bluetooth. I used the pybluez2 module, and I got this error:

OSError: [WinError 10051] A socket operation was attempted to an unreachable network

Here is my connect_to_device function:

if target_address is not None:
    print("Device found: ", target_address)
    port = 1
    sock = bluetooth.BluetoothSocket(bluetooth.Protocols.RFCOMM)
    sock.connect((target_address, port)) # This line is causing the error
    print("Device connected.")
    return True

I tried to change the port number, but it didn't seem relevant. And I also tried this:

sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)

But then I got a KeyError.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Are you sure that service you are trying to connect has port number 1? – Mike Petrichenko Mar 04 '23 at 20:53
  • Yes, I'm almost sure. But I'm kinda figured out part of my problem. The pybluez module does not work in the python version I am using. Instead I use pybluez2, which is a fork of pybluez, which causes an error. I installed python 3.5 and installed the pybluez module. Problem solved, but this version of python is incompatible with the project I'm trying to build. Anyway, in conclusion, if there is any other module you can suggest, please write. – Murabıt Akdoğan Mar 05 '23 at 22:31
  • 1
    If you need it for Windows then you can try this: https://github.com/btframework/WCL-Python-Demos – Mike Petrichenko Mar 06 '23 at 06:13
  • Thanks for your answer Mike, this works just fine! I sincerely appreciate it – Murabıt Akdoğan Mar 08 '23 at 22:33

0 Answers0