Skip to content

Commit 92a92d4

Browse files
committed
fix write splitting
1 parent 52c74a6 commit 92a92d4

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

android/src/main/java/it/iotinga/blelibrary/RNBleManager.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ class RNBleManager(
384384
} else if (characteristic.supportsIndication()) {
385385
disableIndication(transactionId, characteristic, promise)
386386
} else {
387-
log(Log.WARN, "Characteristic ${characteristic.uuid} doesn't support indication or notification")
387+
log(
388+
Log.WARN,
389+
"Characteristic ${characteristic.uuid} doesn't support indication or notification"
390+
)
388391
promise.reject(
389392
BleError.ERROR_INVALID_ARGUMENTS.name,
390393
"Characteristic does not support notification or indication",
@@ -410,10 +413,16 @@ class RNBleManager(
410413
BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
411414
)
412415
.split({ message, index, maxLength ->
413-
message.slice((index * chunkSize..min((index + 1) * chunkSize, message.size) - 1))
414-
.toByteArray()
416+
val packetSize = min(maxLength, chunkSize)
417+
val startIndex = index * packetSize
418+
if (startIndex >= message.size) {
419+
null
420+
} else {
421+
message.slice((startIndex..min(startIndex + packetSize, message.size) - 1))
422+
.toByteArray()
423+
}
415424
}) { device, data, index ->
416-
log(Log.VERBOSE, "Wrote chunk ${index} of ${data?.size} bytes")
425+
log(Log.VERBOSE, "Wrote chunk $index of ${data?.size} bytes")
417426
written += data?.size ?: 0
418427
sendEvent(
419428
Event.PROGRESS, mapOf(

0 commit comments

Comments
 (0)