python:bluetootherror:(111,“连接被拒绝”)

2024-10-01 07:16:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在 大家好!在

通过蓝牙将手机连接到raspberry Pi3似乎有问题。 我想我的代码有问题。在

在 这是我的密码

import bluetooth

from bluetooth import  *

serverMAC = 'xx:xx:xx:xx:xx:xx'

port = 1

s = blutooth.BluetoothScocket(bluetooth.RFCOMM)

s.connnect((serverMAC, port)

我想收到从手机到树莓派的值。在

^{pr2}$

Tags: 代码fromimport密码portraspberry手机bluetooth
1条回答
网友
1楼 · 发布于 2024-10-01 07:16:58

在尝试下面的代码之前,请确保脚本运行设备上的BT适配器已打开,并且目标蓝牙设备处于可发现模式(它的适配器已打开,并且在发现时具有广播功能)。在

确保为目标设备使用正确的端口。您可以通过在可用设备上运行一个discover,然后将MAC机与找到的设备之一匹配,并在地址上发出find_服务。来源:Sending messages or datas with bluetooth via python

在本地计算机上尝试了此操作,请注意,Mac电脑会更改,配置文件也会更改,因此,如果您希望RFCOMM,请确保您的设备在尝试连接之前将其公开:

from bluetooth import *
devices = discover_devices()
for device in devices:
    print([_ for _ in find_service(address=device) if 'RFCOMM' in _['protocol'] ])
# now manually select the desired device or hardcode its name/mac whatever in the script
bt_addr = ...
port = [_ for _ in find_service(address=bt_addr) if 'RFCOMM' in _['protocol']][0]['port']
s = BluetoothSocket(RFCOMM)
s.connect((bt_addr, port))

相关问题 更多 >