简体   繁体   English

通过Ubuntu中的蓝牙发送二进制数据

[英]Send binary data through bluetooth in ubuntu

I am working in a project where I need to send/read data to/from a RFID reader through bluetooth in Ubuntu. 我在一个项目中需要通过Ubuntu中的蓝牙向RFID阅读器发送数据/从RFID阅读器读取数据。

I am able to connect to the reader using rfcomm connect , but now I need to send/receive binary data to/from the reader through bluetooth. 我可以使用rfcomm connect连接到阅读器,但是现在我需要通过蓝牙向/从阅读器发送/接收二进制数据。 Looking for information I did not found the way to do it in Ubuntu. 在寻找信息时,我没有找到在Ubuntu中执行此操作的方法。 I found how to send an entire file using minicom, but that is not what I need, since I need to send/receive binary data from C/C++/Python. 我发现了如何使用minicom发送整个文件,但这不是我所需要的,因为我需要从C / C ++ / Python发送/接收二进制数据。

Does somebody knows how to send/receive binary data through bluetooth? 有人知道如何通过蓝牙发送/接收二进制数据吗?

Since Python is an option, check out the PyBluez or PyOBEX Python modules. 由于可以选择使用Python,因此请检查PyBluezPyOBEX Python模块。 Both allow you to transfer any type of data via Bluetooth and are well documented. 两者都允许您通过蓝牙传输任何类型的数据,并且都有据可查。 PyOBEX only works if your client supports the OBEX protocol. PyOBEX仅在您的客户端支持OBEX协议时才有效。

Answering to my own question, the problem was that I implemented an obsolete communications protocol for my RFID reader, and it just responds when it "understand" the message received. 在回答我自己的问题时,问题是我为我的RFID阅读器实施了一个过时的通信协议,并且当它“理解”收到的消息时它才响应。 It means, a message encoded with the obsolete protocol has no response, and the program seems to be blocked, but just because it has no response. 这意味着,用过时的协议编码的消息没有响应,并且程序似乎已被阻止,只是因为它没有响应。

As it would be useful for somebody, the piece of code for bidirectional communication via Bluetooth with a CAEN RFID Reader using PyBluez I used is: 因为这对某人有用,所以使用我使用的PyBluez通过CAEN RFID阅读器通过蓝牙进行双向通信的代码段是:

uuid = "00001101-0000-1000-8000-00805f9b34fb"
addr = "00:12:F3:20:D7:E7"
service_matches = find_service( uuid = uuid, address = addr )

if len(service_matches) == 0:
     print("couldn't find the SampleServer service =(")
     sys.exit(0)

first_match = service_matches[0]
port = first_match["port"]
host = first_match["host"]

sock=BluetoothSocket( RFCOMM )
sock.connect((host, port))

string_set_protocol = "\x80\x01\x00\x00\x00\x00\x53\x58\x00\x1C\x00\x00\x00\x08\x00\x01\x00\x74\x00\x00\x00\x0A\x00\x54\x00\x00\x00\x03"

sock.send(string_set_protocol)
received_str = sock.recv(1024)
received_binary = ":".join("{:02x}".format(ord(c)) for c in received_str) #Print each byte in hexadecimal separated by ":"
print(received_binary)

And it worked perfectly 而且效果很好

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

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