通过蓝牙发送数据 [英] Sending Data via Bluetooth

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

问题描述

我是如何通过蓝牙连接发送的数据有点困惑。在Android API文档,从蓝牙聊天的例子,在类BluetoothChat.java 构造Handler对象。在有一个switch语句,以及 MESSAGE_WRITE 情况。我需要实现类似code到通过蓝牙发送字符串?每串我想发出一个case语句?我特别想发送(名称,值)让我知道什么是发送和它的价值。如何实现这一点?如果下面的例子中,我称之为 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.

推荐答案

我使用的是谷歌的的 Protocol Buffers的通过蓝牙连接,在我的Andr​​oid应用程序发送的结构化数据。 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.

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

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