我正在通过BLE将Android应用中的图像发送到iOS应用。根据一些文章,通过BLE发送的最大大小是1 KB,因此我将我的图像分成1 KB块并将其发送到Swift应用程序,延迟时间为40毫秒。
然而,块大小似乎太大了。调试之后,我注意到我在Swift中读取的特征值只能达到187个字节。因此,当我将Android中的块大小减少到该数量或更小时,传输成功,否则它不会因为数据被切断。
有人知道它为什么限制在那个大小,尽管它应该是1 KB吗?或者,如果可配置我如何增加尺寸?
这是我的Android代码:
private void writeImageToCharacteristic() {
// determine number of chunks and checksum
final int chunkSize = 1000; // fails with 1000, succeeds with 180
int numberOfChunks = imageInByte.length / chunkSize;
numberOfChunks = imageInByte.length == numberOfChunks * chunkSize ? numberOfChunks : numberOfChunks + 1;
// get the characteristic
BluetoothGattCharacteristic imgCharacteristic = mBluetoothGattServer
.getService(SERVICE_UUID)
.getCharacteristic(IMAGE_CHARACTERISTIC_UUID);
Log.d(TAG, "Image size in byte: " + imageInByte.length);
Log.d(TAG, "Number of chunks: " + numberOfChunks);
Log.d(TAG, "Start sending...");
updateStateLabel("State: sending image data...");
// first message contains information about the image
final String msg = String.valueOf(numberOfChunks);
imgCharacteristic.setValue(msg.getBytes());
mBluetoothGattServer.notifyCharacteristicChanged(mConnectedDevice, imgCharacteristic, false);
// create the chunks (and checksums if configured)
List<byte[]> messages = new ArrayList<>();
for(int i = 0; i < numberOfChunks; ++i) {
final int start = i * chunkSize;
final int end = start + chunkSize > imageInByte.length ? imageInByte.length : start + chunkSize;
final byte[] chunk = Arrays.copyOfRange(imageInByte, start, end);
messages.add(chunk);
if(includeChecksums) {
final String chunkCheckSum = calculateCheckSum(chunk);
messages.add(chunkCheckSum.getBytes());
}
}
// send the data to the device by writing
// it to the characteristic with a delay of 40 ms
for(byte[] aMsg : messages) {
SystemClock.sleep(40);
imgCharacteristic.setValue(aMsg);
mBluetoothGattServer.notifyCharacteristicChanged(mConnectedDevice, imgCharacteristic, false);
}
Log.d(TAG, "Finished sending");
updateStateLabel("State: finished sending.");
}
以下是读取它的Swift代码:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
switch characteristic.uuid {
case Constants.ImageCharacteristicUUID:
let imgData = characteristic.value!
// here, the characteristic.value seems to be limited to 187 bytes - why?
processImageData(data: imgData!)
default:
print("Unhandled Characteristic UUID: \(characteristic.uuid)")
}
}
感谢任何帮助。