Python蓝牙 - 试图以访问权限禁止的方式访问插座

发布于 2025-01-17 09:51:38 字数 1582 浏览 2 评论 0原文

我成功通过蓝牙连接设备'蓝牙串行终端'到Win10 PC,我根据需要在应用中收到数据。

我已经了解到,毕色是对Python BT应用程序进行编程的方式,但是如果这是错误的,请让我知道什么是更好(更简单)的替代方案。

因此,由于我现在希望通过Python脚本连接BT设备,所以我已经安装了Pybluez和复制以下代码:

hostMACAddress = '00:07:80:e0:a4:fc'
import bluetooth
port = 2
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
    client, clientInfo = s.accept()
    while 1:
        data = client.recv(size)
        if data:
            print(data)
            client.send(data) # Echo back to client
except: 
    print("Closing socket")
    client.close()
    s.close()

当我运行它时(Visual Studio作为管理员),我会得到错误: “试图

通过更改端口号(我尝试从0到60000的一堆)尝试以禁止其访问权限禁止的方式访问插座,但是在此上下文中,请始终获得“请求的地址” '。

谷歌搜索这两种消息都会导致无数的讨论,最终都会陷入精神尾声。

我对Python一无所知,对插座,端口和网络一无所知,因此请保持基础。

我还尝试了以下

import bluetooth  
serverMACAddress = '00:07:80:e0:a4:fc' 
port = 2 
size = 1024 
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
s.connect((serverMACAddress, port)) 
try:     
    while 1:         
        data = s.recv(size)         
        if data:
             print(data) 
except:
         print("Closing socket")
     s.close()

代码不会中断,但是当我按下BT设备上的发送按钮时,什么也不会发生。要澄清:当我在bt设备上按下“发送”按钮时,在应用程序窗口中接收到蓝牙串行应用数据时

I successfully connect a device via bluetooth with the app 'Bluetooth Serial Terminal' to a Win10 PC and I receive the data in the app as desired.

I have understood that pybluez is THE way to program python BT applications, but if that is wrong please let me know what is a better (=simpler) alternative.

So since I now instead wish to connect the BT device over a Python script I have installed pybluez and copied the following code:

hostMACAddress = '00:07:80:e0:a4:fc'
import bluetooth
port = 2
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
    client, clientInfo = s.accept()
    while 1:
        data = client.recv(size)
        if data:
            print(data)
            client.send(data) # Echo back to client
except: 
    print("Closing socket")
    client.close()
    s.close()

When I run it (Visual Studio as an admin) I get the error:
'An attempt was made to access a socket in a way forbidden by its access permissions'

I tried to by changing the port number (I tried a bunch from 0 to 60000), but always get 'The requested address is not valid in this context'.

Googling both of these messages result in a myriad of discussions which all end up with me going into a mental tailspin.

I know very little about python and nothing about sockets, ports and networks so please keep it basic.

I also tried the following

import bluetooth  
serverMACAddress = '00:07:80:e0:a4:fc' 
port = 2 
size = 1024 
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
s.connect((serverMACAddress, port)) 
try:     
    while 1:         
        data = s.recv(size)         
        if data:
             print(data) 
except:
         print("Closing socket")
     s.close()

This code does not interrupt, but nothing happens when I press the send button on the BT device. To clarify: when I connect with the Bluetooth Serial App data is received in the app window when the send button is pressed on the BT device

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2025-01-24 09:51:38

我使用此代码找到在这里

import bluetooth
print("looking for nearby devices...")
nearby_devices = bluetooth.discover_devices(lookup_names = True, flush_cache = True, duration = 20)
print("found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
    print(" %s - %s" % (addr, name))
    for services in bluetooth.find_service(address = addr):
        print(" Name: %s" % (services["name"]))
        print(" Description: %s" % (services["description"]))
        print(" Protocol: %s" % (services["protocol"]))
        print(" Provider: %s" % (services["provider"]))
        print(" Port: %s" % (services["port"]))
        print(" Service id: %s" % (services["service-id"]))

:使用错误的端口。
我更改了端口,上面的第二个代码工作正常。

I used this code found here:

import bluetooth
print("looking for nearby devices...")
nearby_devices = bluetooth.discover_devices(lookup_names = True, flush_cache = True, duration = 20)
print("found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
    print(" %s - %s" % (addr, name))
    for services in bluetooth.find_service(address = addr):
        print(" Name: %s" % (services["name"]))
        print(" Description: %s" % (services["description"]))
        print(" Protocol: %s" % (services["protocol"]))
        print(" Provider: %s" % (services["provider"]))
        print(" Port: %s" % (services["port"]))
        print(" Service id: %s" % (services["service-id"]))

and it revealed to me that I was using the wrong port.
I changed the port and my second code above worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文