From 2510b5e113542f3e251ac254bd0985546cc9c805 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 3 Nov 2026 07:47:32 -0500 Subject: [PATCH] fix: keep sending input when the host never raises the packet size --- .../droiddeck/launcher/input/bt/BtGamepad.kt | 25 +++++++++++-------- .../launcher/input/bt/BtPadReports.kt | 10 ++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/input/bt/BtGamepad.kt b/app/src/main/java/com/droiddeck/launcher/input/bt/BtGamepad.kt index 9b1eb1e57..e5d2fe2db 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/bt/BtGamepad.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/bt/BtGamepad.kt @@ -24,6 +24,7 @@ import android.os.Build import android.os.Handler import android.os.HandlerThread import android.os.ParcelUuid +import android.os.SystemClock import android.os.VibrationEffect import android.os.Vibrator import android.util.Log @@ -52,7 +53,7 @@ object BtGamepad { private val pendingServices = ArrayDeque() private val reportRefs = HashMap>() private val inputChars = HashMap() - private val longReads = HashMap() + private val longReads = HashMap, ByteArray>() private val preparedWrites = HashMap() private var host: BluetoothDevice? = null private var priorityLink: BluetoothGatt? = null @@ -67,6 +68,8 @@ object BtGamepad { private var pairingUntil = 0L private var pendingHost: BluetoothDevice? = null private var fastLinkPending = false + private var notifiedAt = 0L + private const val NOTIFY_TIMEOUT_MS = 100L private const val PAIRING_WINDOW_MS = 120_000L private const val MAX_MTU = 517 private const val FAST_LINK_DELAY_MS = 1_500L @@ -130,8 +133,11 @@ object BtGamepad { stopAdvertising() h.removeCallbacks(periodic) dropFastLink() + pendingHost?.let { d -> runCatching { server?.cancelConnection(d) } } + pendingHost = null host?.let { d -> runCatching { server?.cancelConnection(d) } } host = null + longReads.clear() _status.value = Status(Phase.OFF) } } @@ -230,6 +236,7 @@ object BtGamepad { } server = null host = null + pendingHost = null notifyPending = false dirty = false reportRefs.clear() @@ -389,7 +396,8 @@ object BtGamepad { override fun onCharacteristicReadRequest(device: BluetoothDevice, requestId: Int, offset: Int, chr: BluetoothGattCharacteristic) { handler?.post { - val value = if (offset > 0) longReads[chr] ?: valueOf(chr, device) else valueOf(chr, device).also { longReads[chr] = it } + val key = device.address to chr + val value = if (offset > 0) longReads[key] ?: valueOf(chr, device) else valueOf(chr, device).also { longReads[key] = it } val reply = if (offset >= value.size) ByteArray(0) else value.copyOfRange(offset, value.size) server?.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, reply) if (chr.uuid == uuid16(0x2A4B)) attach(device) @@ -475,7 +483,7 @@ object BtGamepad { } private fun attach(device: BluetoothDevice) { - if (host != null) return + if (host != null || !running) return if (device.bondState != BluetoothDevice.BOND_BONDED) { pendingHost = device publish(Phase.CONNECTING, device) @@ -539,17 +547,13 @@ object BtGamepad { private fun sendNow() { val s = server ?: return val device = host ?: return - if (notifyPending) { + if (notifyPending && SystemClock.elapsedRealtime() - notifiedAt < NOTIFY_TIMEOUT_MS) { dirty = true return } - val chr = inputChars[encoder.inputId()] ?: return val state = current() - val value = encoder.input(state) - if (value.size > mtu - 3) { - dirty = false - return - } + val (id, value) = encoder.report(state, mtu - 3) + val chr = inputChars[id] ?: return val ok = runCatching { if (Build.VERSION.SDK_INT >= 33) { s.notifyCharacteristicChanged(device, chr, false, value) == BluetoothGatt.GATT_SUCCESS @@ -562,6 +566,7 @@ object BtGamepad { }.getOrDefault(false) dirty = !ok notifyPending = ok + notifiedAt = SystemClock.elapsedRealtime() if (ok) { sent.set(state) sentOnce = true diff --git a/app/src/main/java/com/droiddeck/launcher/input/bt/BtPadReports.kt b/app/src/main/java/com/droiddeck/launcher/input/bt/BtPadReports.kt index 0fc6bd729..8b95a854d 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/bt/BtPadReports.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/bt/BtPadReports.kt @@ -115,6 +115,11 @@ abstract class BtPadEncoder { abstract fun inputId(): Int abstract fun input(s: BtPadState): ByteArray open val periodMs = 0L + + open fun report(s: BtPadState, limit: Int): Pair { + val v = input(s) + return inputId() to if (v.size > limit) v.copyOf(limit) else v + } open fun feature(id: Int): ByteArray? = null open fun setFeature(id: Int, data: ByteArray) {} open fun output(id: Int, data: ByteArray): BtFeedback? = null @@ -267,6 +272,11 @@ private abstract class SonyEncoder : BtPadEncoder() { return intArrayOf(b0, b1, b2) } + override fun report(s: BtPadState, limit: Int): Pair { + val full = input(s) + return if (full.size <= limit) inputId() to full else 1 to simple(s) + } + protected fun simple(s: BtPadState): ByteArray { val b = buttons(s) counter = (counter + 1) and 0x3F