Android蓝牙串口/RFCOMM/SPP,如何更改波特率?

发布于 2024-10-30 21:38:16 字数 377 浏览 0 评论 0原文

这非常令人沮丧,因为我已经尝试了几周,甚至在互联网上都没有任何线索。

我正在开发一个在不同农业硬件上读取/写入蓝牙串行数据的项目。

大多数这些硬件都很旧,无法更改其 SPP 的波特率。

我使用 android 蓝牙聊天作为读/写的基础,我只更改了 UUID 以允许连接到我所拥有的设备,

我已经成功地以 19,200 的坏率从标签阅读器中读取,并且格式很好

但是,我现在陷入了波特率为 9,600 的秤上,并在我的应用程序中显示有趣的字符,甚至与秤应该来自的格式不接近。

这个项目之前是在旧的 Windows Mobile 上完成的,波特率可以在他们的 API 中改变

有没有办法在 Android 中做到这一点还是我找错了树?

This is very frustrating as I've been trying for weeks and not even a clue on the internet.

I am working on a project that reads/writes bluetooth serial data on different farming hardware.

most of these hardware are old and changing the baud rate of their SPP is not an option.

I'm using the android bluetooth chat as a basis to read/write, I've only changed the UUID to allow connection to the devices I have

I've managed to read from a tag reader at a bad rate of 19,200 beautifully, and formatted nicely

However, I'm stuck now with the scales which has a baud rate of 9,600 and shows funny characters in my app, not even close to the format that should come from the scales

This project was done previously on a old Windows Mobile and baud rates could be changed in their API

Is there a way to do this in Android or am I barking up the wrong tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-11-06 21:38:16

编辑:修复了一些错误。

互联网上没有关于此的良好信息 - 而且很多都是错误的 - 不要相信任何人!

特别是,以下两件事是正确的:

  • RFCOMM 和 SPP (串行端口配置文件)是同一事物的两个名称。 (然而,它们的关系非常密切。)
  • 无需设置 RFCOMM / SPP 连接的波特率。

以下是我发现的内容:

蓝牙串行连接是按照 SPP 中的描述使用 RFCOMM 进行的。 RFCOMM 是一种在 L2CAP 上运行的连接,允许在帧中发送串行数据和控制参数。它很大程度上基于名为 TS 07.10 的古老标准。 SPP 是蓝牙配置文件。

此帧还允许您发送带外数据,例如控制串行信号(DTR、CTS 等)和设置波特率

来自 SPP 规范:

DevA 可以通过远程端口协商通知 DevB RS232 端口设置
命令,直接在 DLC 建立之前。如果 RFCOMM 适配层的 API 公开了这些设置(例如波特率、奇偶校验),则需要这样做。

遗憾的是 Android 根本没有公开此功能。我想可能可以自己发送远程端口协商命令,但我很确定这需要重新实现 RFCOMM,这需要访问 L2CAP

远程端口协商命令的详细信息请参见此处第 31 页。是为所有参数分配的默认值,如果不执行协商,则选择默认值。”

因此在Android上无法根据需要设置波特率。它将使用蓝牙适配器使用的默认波特率(可能是 9600)。

另请注意,Windows 确实支持设置蓝牙适配器的波特率。

Edit: Fixed some errors.

There's no good information on the internet about this - and lots of it is wrong - don't trust anyone!

In particular, these two things are not true:

  • RFCOMM and SPP (Serial Port Profile) are two names for the same thing. (They are however very closely related.)
  • There's no need to set the baud rate of an RFCOMM / SPP connection.

Here is what I have found:

Bluetooth serial connections are made as described in the SPP using RFCOMM. RFCOMM is a connection that runs over L2CAP and allows sending serial data and control parameters in frames. It is heavily based on an ancient standard called TS 07.10. SPP is the bluetooth profile.

This framing also allows you to send out-of-band data for things like controlling serial signals (DTR, CTS, etc) and for setting the baud rate.

From the SPP specification:

DevA may inform DevB of RS232 port settings with the Remote Port Negotiation
Command, directly before DLC establishment. There is a requirement to do so if the API to the RFCOMM adaptation layer exposes those settings (e.g. baud rate, parity).

Sadly Android doesn't expose this functionality at all. I suppose it may be possible to send the Remote Port Negotiation command ourselves but I'm pretty sure that would require reimplementing RFCOMM which requires access to L2CAP that we don't have.

The Remote Port Negotiation command is detailed here page 31. Note "There are default values assigned on all parameters, if no negotiation is performed, the default value is chosen."

Therefore it is impossible to set the baud rate as desired on Android. It will use the default baud rate that your bluetooth adapter uses (probably 9600).

Also note that Windows does support setting the baud rate of bluetooth adapters.

你又不是我 2024-11-06 21:38:16

大概有一些蓝牙串行转换器连接到旧设备上?如果是这样,您应该能够对其进行一次编程以始终使用特定的比特率。那么蓝牙客户端就不需要设置比特率了......

Presumably some Bluetooth to serial converter is attached to the legacy devices? If so you should be able to program it once to always use a particular bit rate. Then there would be no need for the Bluetooth client to set the bit rate...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文