通过python发送带有蓝牙的消息或数据 [英] Sending messages or datas with bluetooth via python

查看:765
本文介绍了通过python发送带有蓝牙的消息或数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过python通过蓝牙发送消息而没有类型类型的密钥验证?

How can i send messages over bluetooth via python without key authentification like type numbers ?

我用过pybluez 但是我遇到了这个错误:

i used pybluez but i got this error:

File "./send", line 12, in <module>
    connect()
File "./send", line 8, in connect
    sock.connect((bd_addr, port))
File "<string>", line 5, in connect
    bluetooth.btcommon.BluetoothError: (111, 'Connection refused')

这是代码

#!/usr/bin/python

import bluetooth

def connect ():
    bd_addr = "x:x:x:x:x:x"
    port = 1
    sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    sock.connect((bd_addr, port))
    sock.send("hello!!")
    sock.close()

connect()

推荐答案

正如@TJD所说,您需要确保为所需的服务绑定正确的端口.

As @TJD said, you need to ensure you bind with the correct port for the service you want.

>>> from bluetooth import *
>>> from pprint import pprint
>>>
>>> devices = discover_devices()
>>> devices
['xx:yy:tt:zz:44:BD', '00:yy:72:zz:bb:aa']

然后作为第二步,尝试在您要连接的设备上找到服务.

Then as the second step try to find the service on the device you want to connect to.

>>> service = find_service(address='00:yy:72:zz:bb:aa')
>>> pprint(service)
[{'description': None,
  'host': '00:yy:72:zz:bb:aa',
  'name': 'Headset Audio Gateway',
  'port': 12,
  'profiles': [('1108', 258)],
  ...},
 {'description': None,
  'host': '00:yy:72:zz:bb:aa',
  'name': 'Dial-Up Networking',
  'port': 1,
  'profiles': [('1103', 256)],
  'protocol': 'RFCOMM',
  ...}]

根据此信息,您可以连接到设备上运行的服务.根据服务/配置文件规范,您发送特定于服务的命令并从设备取回信息.例如.在上面的列表中,您会看到"Headset Audio Gateway"和配置文件列表,其中的数字为"1108",这是该服务的简称.现在,您可以查找此配置文件的命令,它应该可以正常工作.

Based on this information you can connect to a service running on a device. According to the service/profile specification you send service specific commands and get back information from the device. E.g. in the list above you see the 'Headset Audio Gateway' and the profile list with the number '1108', which is the short uuid for the service. You can now lookup the commands for this profile and it should work.

这篇关于通过python发送带有蓝牙的消息或数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆