如何在 android 中通过 ble 发送超过 20 字节的数据?
How to send more than 20 bytes data over ble in android?
我正在尝试使用简单循环发送超过 33 个字节,有人知道如何通过 android ble 发送超过 20 个字节的数据吗?
if(!mConnected) return;
for (int i = 0; i<str.length;i++) {
if(str[i] == str[str.length -1]){
val = str[i]+"\n";
}else {
val = str[i] + "_";
}
System.out.println(val);
mBluetoothLeService.WriteValue(val);
}
通过 BLE 发送超过 20 个字节很容易实现,方法是将您的数据拆分为 20 个字节的数据包,并在发送每个数据包之间实现短暂的延迟(即使用 sleep()
)。
这是我正在处理的一个项目的一小段代码,它以 byte[]
的形式获取数据并将其拆分为相同的数组( byte[][]
),在20 byte chunks,然后将其发送给另一种方法,将每个数据包一个一个地传输。
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);
这里有两个关于如何实现这一点的很好的解释:
(Whosebug) Android: Sending data >20 bytes by BLE
一些嵌入式蓝牙 LE 实现将特征的大小限制为 20 个字节。我知道莱尔德 BL600 系列就是这样做的。这是 Laird 模块的限制,即使 BLE 规范要求最大长度更长。其他嵌入式 BLE 解决方案也有类似的限制。我怀疑这是您遇到的限制。
我只是为我的应用程序找到了一种更好、更有效的方法来发送超过 20 位的数据,而不是对每个块都使用睡眠。
数据包将在 onCharacteristicWrite() 触发后发送。我刚刚发现此方法将在外围设备(BluetoothGattServer)发送 sendResponse() 方法后自动触发。
首先我们必须使用这个函数将数据包数据转换成块:
public void sendData(byte [] data){
int chunksize = 20; //20 byte chunk
packetSize = (int) Math.ceil( data.length / (double)chunksize); //make this variable public so we can access it on the other function
//this is use as header, so peripheral device know ho much packet will be received.
characteristicData.setValue(packetSize.toString().getBytes());
mGatt.writeCharacteristic(characteristicData);
mGatt.executeReliableWrite();
packets = new byte[packetSize][chunksize];
packetInteration =0;
Integer start = 0;
for(int i = 0; i < packets.length; i++) {
int end = start+chunksize;
if(end>data.length){end = data.length;}
packets[i] = Arrays.copyOfRange(data,start, end);
start += chunksize;
}
在我们的数据准备好后,我将迭代放在这个函数上:
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if(packetInteration<packetSize){
characteristicData.setValue(packets[packetInteration]);
mGatt.writeCharacteristic(characteristicData);
packetInteration++;
}
}
您可以发送超过 20 个字节的数据,而无需分成块并包括延迟。您尝试写入的每个特征都分配了一个 MTU 值。这是一次可以写入的字节数。
在连接期间交换 MTU 值,您可以一次写入那么多字节。您可以增加服务器端的 mtu 值(最大 512 字节)并一次性发送那么多字节。
对于 Android,您可能希望在使用
连接到服务器后手动请求 mtu
requestMtu(int mtu)
根据您发送的 mtu 值,这 return 是对还是错。它将回调到 onMtuChanged,其中 Android 设备和服务器协商最大可能的 MTU 值。
onMtuChanged(BluetoothGatt gatt、int mtu、int status)
并且可以在这个函数中设置MTU值,一次可以发送20多个字节
我正在尝试使用简单循环发送超过 33 个字节,有人知道如何通过 android ble 发送超过 20 个字节的数据吗?
if(!mConnected) return;
for (int i = 0; i<str.length;i++) {
if(str[i] == str[str.length -1]){
val = str[i]+"\n";
}else {
val = str[i] + "_";
}
System.out.println(val);
mBluetoothLeService.WriteValue(val);
}
通过 BLE 发送超过 20 个字节很容易实现,方法是将您的数据拆分为 20 个字节的数据包,并在发送每个数据包之间实现短暂的延迟(即使用 sleep()
)。
这是我正在处理的一个项目的一小段代码,它以 byte[]
的形式获取数据并将其拆分为相同的数组( byte[][]
),在20 byte chunks,然后将其发送给另一种方法,将每个数据包一个一个地传输。
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);
这里有两个关于如何实现这一点的很好的解释:
(Whosebug) Android: Sending data >20 bytes by BLE
一些嵌入式蓝牙 LE 实现将特征的大小限制为 20 个字节。我知道莱尔德 BL600 系列就是这样做的。这是 Laird 模块的限制,即使 BLE 规范要求最大长度更长。其他嵌入式 BLE 解决方案也有类似的限制。我怀疑这是您遇到的限制。
我只是为我的应用程序找到了一种更好、更有效的方法来发送超过 20 位的数据,而不是对每个块都使用睡眠。
数据包将在 onCharacteristicWrite() 触发后发送。我刚刚发现此方法将在外围设备(BluetoothGattServer)发送 sendResponse() 方法后自动触发。
首先我们必须使用这个函数将数据包数据转换成块:
public void sendData(byte [] data){
int chunksize = 20; //20 byte chunk
packetSize = (int) Math.ceil( data.length / (double)chunksize); //make this variable public so we can access it on the other function
//this is use as header, so peripheral device know ho much packet will be received.
characteristicData.setValue(packetSize.toString().getBytes());
mGatt.writeCharacteristic(characteristicData);
mGatt.executeReliableWrite();
packets = new byte[packetSize][chunksize];
packetInteration =0;
Integer start = 0;
for(int i = 0; i < packets.length; i++) {
int end = start+chunksize;
if(end>data.length){end = data.length;}
packets[i] = Arrays.copyOfRange(data,start, end);
start += chunksize;
}
在我们的数据准备好后,我将迭代放在这个函数上:
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if(packetInteration<packetSize){
characteristicData.setValue(packets[packetInteration]);
mGatt.writeCharacteristic(characteristicData);
packetInteration++;
}
}
您可以发送超过 20 个字节的数据,而无需分成块并包括延迟。您尝试写入的每个特征都分配了一个 MTU 值。这是一次可以写入的字节数。 在连接期间交换 MTU 值,您可以一次写入那么多字节。您可以增加服务器端的 mtu 值(最大 512 字节)并一次性发送那么多字节。
对于 Android,您可能希望在使用
连接到服务器后手动请求 mturequestMtu(int mtu)
根据您发送的 mtu 值,这 return 是对还是错。它将回调到 onMtuChanged,其中 Android 设备和服务器协商最大可能的 MTU 值。
onMtuChanged(BluetoothGatt gatt、int mtu、int status)
并且可以在这个函数中设置MTU值,一次可以发送20多个字节