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

查看:272
本文介绍了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.

我知道套接字模块提供了本机支持,但是这给我带来了错误.经过搜索后,我找到了一个答案,声称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的预编译版本也不起作用,我猜这是因为最新版本是针对我使用3.7时的Python 3.5构建的.

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!

推荐答案

Python 3.9中已开始支持Windows 10的蓝牙RFCOMM.

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 App连接到它. https://play.google.com/store/apps/details?id = com.stuffaboutcode.bluedot& hl = zh_CN

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天全站免登陆