import bluetooth
from bluetooth import *
serverMAC = 'xx:xx:xx:xx:xx:xx'
port = 1
s = blutooth.BluetoothScocket(bluetooth.RFCOMM)
s.connnect((serverMAC, port)
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))
在尝试下面的代码之前,请确保脚本运行设备上的BT适配器已打开,并且目标蓝牙设备处于可发现模式(它的适配器已打开,并且在发现时具有广播功能)。在
确保为目标设备使用正确的端口。您可以通过在可用设备上运行一个discover,然后将MAC机与找到的设备之一匹配,并在地址上发出find_服务。来源:Sending messages or datas with bluetooth via python
在本地计算机上尝试了此操作,请注意,Mac电脑会更改,配置文件也会更改,因此,如果您希望RFCOMM,请确保您的设备在尝试连接之前将其公开:
相关问题 更多 >
编程相关推荐