我正在尝试通过BLE将数据从Android设备发送到iOS设备。我已成功将字节块发送到swift应用程序,但我现在如何使用该数据创建图像?
这是我的Android逻辑:
private void convertImgToByteArray() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageInByte = baos.toByteArray();
}
private void writeImageToCharacteristic() {
// determine number of chunks
final int chunkSize = 1000;
int numberOfChunks = imageInByte.length / chunkSize;
numberOfChunks = imageInByte.length == numberOfChunks * chunkSize ? numberOfChunks : numberOfChunks + 1;
// get the characteristic
BluetoothGattCharacteristic imgCharacteristic = mBluetoothGattServer
.getService(MY_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...");
// create the chunks and write them to characteristic
for (int i = 0; i < numberOfChunks; ++i) {
final int start = i * chunkSize;
final int end = start + chunkSize > imageInByte.length ? imageInByte.length : start + chunkSize;
// delay of 40 ms between writes
SystemClock.sleep(40);
// do the write
final byte[] chunk = Arrays.copyOfRange(imageInByte, start, end);
Log.d(TAG, "Sending " + (i+1) + ". chunk.");
imgCharacteristic.setValue(chunk);
mBluetoothGattServer.notifyCharacteristicChanged(mConnectedDevice, imgCharacteristic, false);
}
}
这是我接收字节的快速逻辑对应物:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
switch characteristic.uuid {
case Constants.ImageCharacteristicUUID:
let v = characteristic.value!
// TODO: collect byte values into array and create image out of it
default:
print("Unhandled Characteristic UUID: \(characteristic.uuid)")
}
}