简体   繁体   English

python:蓝牙错误:(111,'拒绝连接')

[英]python: bluetootherror: (111, 'connection refused')

Hi everyone! 嗨,大家好!

There seems to be a problem with connecting the cellphone to the raspberry pi 3 via Bluetooth. 通过蓝牙将手机连接到树莓派3似乎存在问题。 I think have a problem with my code. 我认为我的代码有问题。

Here is my code 这是我的代码

import bluetooth

from bluetooth import  *

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

port = 1

s = blutooth.BluetoothScocket(bluetooth.RFCOMM)

s.connnect((serverMAC, port)

I would like to receive the value from the cell phone to Raspberry Pi. 我想从手机上获得Raspberry Pi的价值。

bluetooth.btcommon.BluetoothError: (111, 'Connection refused')

Before trying the code below, make sure the BT adapter on your script-running device is turned on and the target Bluetooth Device is in Discoverable mode (it's adapter is turned on and it's broadcasting capabilities on discovery). 在尝试下面的代码之前,请确保脚本运行设备上的BT适配器已打开,并且目标Bluetooth设备处于可发现模式(它的适配器已打开并且在发现时具有广播功能)。

Make sure you use the correct port for the target device. 确保为目标设备使用正确的端口。 You can do that by running a discover on your available devices, then matching your MAC to one of the found devices and issuing a find_service on the address. 为此,您可以在可用设备上运行发现,然后将MAC与找到的设备之一匹配,然后在该地址上发出find_service。 Source: Sending messages or datas with bluetooth via python 来源: 通过python发送带有蓝牙的消息或数据

Tried this on a local machine, mind the MACs will change, so will the profiles, so if you want RFCOMM make sure your device exposes it before trying to connect: 在本地计算机上尝试过此操作时,请注意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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM