fix: align NIP-BE chunk format with KoalaSat/samiz reference implementation

Reviewed the samiz BLE implementation and fixed compatibility issues:

- Chunk index: changed from 2 bytes to 1 byte (matching samiz/NIP-BE example)
- Chunk overhead: 2 bytes total (1 index + 1 total count), not 3
- chunkSize parameter now means payload size (500), not total chunk size
- Android: TX power HIGH, advertise timeout indefinite (matching samiz)
- Android: request MTU 512 and CONNECTION_PRIORITY_HIGH on connect
- Android: discover services after MTU negotiation (samiz flow)
- Android: set WRITE_TYPE_DEFAULT on write characteristic
- MTU-to-chunkSize: properly accounts for ATT overhead (3) + chunk overhead (2)

These changes ensure wire-level compatibility with samiz devices.

https://claude.ai/code/session_01Tz5E73Rj7tL48A3qUGS5DT
This commit is contained in:
Claude
2026-03-30 02:12:52 +00:00
parent fb78c1a4c4
commit 57aec95e8d
5 changed files with 44 additions and 27 deletions
@@ -103,7 +103,8 @@ class AndroidBleTransport(
.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setConnectable(true)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.setTimeout(0)
.build()
val data =
@@ -158,6 +159,7 @@ class AndroidBleTransport(
BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_WRITE,
)
writeChar.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
readCharacteristic =
BluetoothGattCharacteristic(
@@ -364,7 +366,8 @@ class AndroidBleTransport(
val peer = peerMap[peerUuid] ?: return
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices()
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH)
gatt.requestMtu(BleConfig.DEFAULT_MTU)
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
listener?.onPeerDisconnected(peer)
}
@@ -428,6 +431,8 @@ class AndroidBleTransport(
val peer = peerMap[peerUuid] ?: return
listener?.onMtuChanged(peer, mtu)
}
// Discover services after MTU negotiation (matching samiz flow)
gatt.discoverServices()
}
}