@@ -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