0

I am trying to send a data from an Android device to an iOS device over BLE. I'm successfully sending over the byte chunks to the swift app, however how am I now creating an image out of that data?

Here's my Android logic:

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);
    }
}

And here is my swift logic counterpart that is receiving the bytes:

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)")
    }
}
picciano
  • 22,341
  • 9
  • 69
  • 82
phoebus
  • 1,280
  • 1
  • 16
  • 36

0 Answers0