如何在android中通过ble发送20多个字节的数据? [英] How to send more than 20 bytes data over ble in android?
问题描述
我正在尝试使用简单循环发送超过 33 个字节的数据,是否有人知道如何通过 android ble 发送超过 20 个字节的数据.
I am trying to send more than 33 bytes using simple loops, Is anybody has idea how to send more than 20 bytes data over android ble.
if(!mConnected) return;
for (int i = 0; i<str.length;i++) {
if(str[i] == str[str.length -1]){
val = str[i]+"
";
}else {
val = str[i] + "_";
}
System.out.println(val);
mBluetoothLeService.WriteValue(val);
}
推荐答案
通过将数据拆分为 20 字节的数据包并实现短延迟(即使用 sleep()
) 之间发送每个数据包.
Sending more than 20 bytes via BLE is easily achievable by splitting your data into 20 byte packets and implementing a short delay (i.e. using sleep()
) between sending each packet.
这是我正在处理的一个项目中的一小段代码,它以 byte[]
的形式获取数据并将其拆分为相同的数组,( byte[][]
),以 20 字节的块为单位,然后将其发送到另一种方法,该方法将每个数据包一个一个地传输.
Here's a short snippet of code from a project I'm working on that takes data in the form of byte[]
and splits it into an array of the same, ( byte[][]
), in 20 byte chunks, and then sends it to another method that transmits each packet one by one.
int chunksize = 20;
byte[][] packets = new byte[packetsToSend][chunksize];
int packetsToSend = (int) Math.ceil( byteCount / chunksize);
for(int i = 0; i < packets.length; i++) {
packets[i] = Arrays.copyOfRange(source,start, start + chunksize);
start += chunksize;
}
sendSplitPackets(packets);
这里有另外两个关于如何实现这一点的非常好的解释:
Here are two other very good explanations of how to achieve this:
(Stackoverflow) Android:通过 BLE 发送 >20 字节的数据
这篇关于如何在android中通过ble发送20多个字节的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!