Windows 10 上的 Python 蓝牙 [英] Python bluetooth on Windows 10

查看:220
本文介绍了Windows 10 上的 Python 蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了多个类似的问题,但它们似乎都已经过时或没有有效的答案,所以我再次在这里提问.

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.

我想通过蓝牙从运行 Windows 10 的笔记本电脑连接到另一台设备(在我的例子中是 RaspberryPi).

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

我知道通过 socket 模块有本地支持,但是这对我来说是一个错误.搜索完之后,我找到了一个答案,声称 python 蓝牙套接字不适用于 Windows(?).

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(?).

于是我继续搜索,发现PyBluez作为一个很好的蓝牙库被推荐了很多.再次,这为我抛出了一个 OSError,我听说 PyBluez 不支持 Windows 10.

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.

我发现了一个名为 pybluez-win10 的模块,但基本上没有关于如何使其工作的文档.从 此处 为 Windows 安装预编译版本也不起作用,我猜这是因为最新版本是针对 Python 3.5 而我使用的是 3.7.

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!

推荐答案

Bluetooth RFCOMM Support for Windows 10 is come in Python 3.9

Bluetooth RFCOMM Support for Windows 10 is comming in Python 3.9

https://bugs.python.org/issue36590

我在 Windows 10 PC 上安装了 Python 3.9.0a6,并且能够从 Bluedot 应用程序连接到它.https://play.google.com/store/apps/details?id=com.stuffaboutcode.bluedot&hl=en_GB

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

我在 PC 上的简单测试代码是:

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()

这篇关于Windows 10 上的 Python 蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆