通过蓝牙发送数据

发布于 2024-11-19 01:01:49 字数 382 浏览 2 评论 0原文

我对如何通过蓝牙连接发送数据有点困惑。在 Android API 文档中,在蓝牙聊天示例中,类 BluetoothChat.java 构造了一个 Handler 对象。其中有一个 switch 语句和一个 MESSAGE_WRITE 案例。我是否需要实现类似的代码来通过蓝牙发送字符串?我想发送的每个字符串的 case 语句?特别是我想发送(name,value),这样我就知道发送了什么以及它的值是什么。我该如何实施?如果按照示例,我多次调用 BluetoothChatService.write(String.getBytes()) 来发送...?那么我如何知道哪些字符串与哪些名称相关联?请帮忙。

I'm a little confused about how to send data over a Bluetooth connection. In the Android API documentation, from the Bluetooth Chat example, the class BluetoothChat.java constructs a Handler object. Within there is a switch statement, and a MESSAGE_WRITE case. Do I need to implement similar code to send Strings over Bluetooth? A case statement for each String I want to send? In particular I want to send (name,value) pairs so I know what is sent and what it's value is. How do I implement this? If, following the example, I call BluetoothChatService.write(String.getBytes()) a bunch of times to send...? Then how would I know which strings are associated with which names? Please help.

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

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

发布评论

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

评论(3

留蓝 2024-11-26 01:01:49

我正在使用 Google 的 协议缓冲区 通过 Android 应用中的蓝牙连接发送结构化数据。 protobuf 负责弄清楚如何为您序列化消息,以便您只需发送一个字节值(消息的长度),然后发送序列化的消息;该库负责反序列化另一端的消息并填充自定义对象的字段。一定要看看它;它使得自定义蓝牙套接字协议的编写变得非常容易。

I'm using Google's Protocol Buffers to send structured data over bluetooth connections in my Android app. protobuf takes care of figuring out how to serialize the message for you so that you only have to send a byte value (length of the message) and then the serialized message; the library takes care of unserializing the message on the other end and populating the fields of a custom object. Definitely take a look at it; it made the writing of a custom bluetooth socket protocol quite easy.

倾城月光淡如水﹏ 2024-11-26 01:01:49

将对序列化为任何允许字节表示的格式。例如 XML 或 JSON。或者甚至是您的自定义格式,对于字符串对来说并不困难。然后发送。

Serialize pairs to any of formats which allows byte representation. Such as XML or JSON. Or even your custom format, it wouldn't be difficult for pairs of strings. And then send it.

心凉怎暖 2024-11-26 01:01:49

对于简单的字符串对(例如名称),您可以简单地使用一些字符来定义第一个字符串何时停止,下一个字符串何时开始。

例如,我使用这样的格式将一组 3 个字符串从一个设备发送到另一个设备:

String toSend = partOne + ":" + partTwo + ":" + partThree;

在另一个设备上,要获取您发送的字符串,请使用 String.split() 方法,如下所示:

String parts[] = received.split(":",3);

第二个参数是分割次数的限制。在此示例中,有 3 个字符串,因此最多拆分 3 次。

这样做的缺点是您需要使用除了最后一个字符串之外的所有字符串中永远不会出现的字符。

在我的应用程序中,我使用此方法发送有关短信的数据,前两部分是电话号码和时间戳,因此其中永远不能有 : 。对于名称,换行符可能会起作用。

如果您要发送更复杂的数据,一定要使用协议缓冲区之类的东西。

For simple pairs of strings (Such as names), you could simply use some character to define when the first string stops, and the next begins.

For example, I use a format such as this to send a set of 3 strings from one device to another:

String toSend = partOne + ":" + partTwo + ":" + partThree;

On the other device, to get the strings you sent, use the String.split() method like so:

String parts[] = received.split(":",3);

The 2nd parameter is a limit to how many times to split. In this example, there are 3 strings, so split 3 times max.

The downside to doing this is that you need to use characters that will never be in all but the last string.

In my application, I used this method to send data about text messages, and the first 2 parts are the phone number and timestamp, so there can never be a : in it. For names, a newline would probably work.

If your going to send more complex data, definitely use something like Protocol Buffers.

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