Python bluetooth on Windows 10

Jannik picture Jannik · Jun 5, 2020 · Viewed 8k times · Source

I found multiple similar questions but all of them seemed to be either outdated or to not have a working answer, so I'm asking here again.

I want to connect to another device (in my case a RaspberryPi) via bluetooth from my laptop, which is running Windows 10.

I know that there's native support via the socket module, however that threw an error for me. After searching this up I found an answer claiming that the python bluetooth socket is not working with Windows(?).

So I continued searching and found that PyBluez was recommended a lot as a good bluetooth library. Again, this threw an OSError for me and again, I heard that PyBluez does not support Windows 10.

I found a module named pybluez-win10 but there was basically no documentation on how to make that work. Installing a precompiled build for Windows from here didn't work either, I guess that's because the latest build is for Python 3.5 while I'm using 3.7.

If you have any suggestions what to try or you know other libraries that you recommend, please let me know. Thank you!

Answer

ukBaz picture ukBaz · Jul 9, 2020

Bluetooth RFCOMM Support for Windows 10 is comming in Python 3.9

https://bugs.python.org/issue36590

I installed Python 3.9.0a6 on a Windows 10 PC and was able to connect to it from the Bluedot App. https://play.google.com/store/apps/details?id=com.stuffaboutcode.bluedot&hl=en_GB

My simple test code on the PC was:

import socket

adapter_addr = 'e4:a4:71:63:e1:69'
port = 3  # Normal port for rfcomm?
buf_size = 1024

s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((adapter_addr, port))
s.listen(1)
try:
    print('Listening for connection...')
    client, address = s.accept()
    print(f'Connected to {address}')

    while True:
        data = client.recv(buf_size)
        if data:
            print(data)
except Exception as e:
    print(f'Something went wrong: {e}')
    client.close()
    s.close()