From f69da211abacea0b8e8572903dee79aaef3b578a Mon Sep 17 00:00:00 2001 From: Guoguo Date: Tue, 12 May 2026 06:28:36 -0700 Subject: [PATCH] fix: BLE connection leak on page unload and retry timer cleanup - onUnload disconnects BLE if not in 'done' state (prevents connection leak) - onUnload clears retry timer to prevent setData on dead page - Store retry setTimeout ref for proper cleanup --- miniprogram/pages/ble-connect/ble-connect.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/miniprogram/pages/ble-connect/ble-connect.js b/miniprogram/pages/ble-connect/ble-connect.js index b334f53..41573fb 100644 --- a/miniprogram/pages/ble-connect/ble-connect.js +++ b/miniprogram/pages/ble-connect/ble-connect.js @@ -34,8 +34,15 @@ Page({ onUnload: function () { clearTimeout(this._scanTimer) + clearTimeout(this._retryTimer) ble.stopScan() - if (this._onBindResult) ble.off('bind_result', this._onBindResult) + if (this._onBindResult) { + ble.off('bind_result', this._onBindResult) + this._onBindResult = null + } + if (this.data.state !== 'done') { + ble.disconnect() + } }, startBleConnect: function () { @@ -98,7 +105,7 @@ Page({ self._onBindResult = null } ble.disconnect() - setTimeout(function () { + self._retryTimer = setTimeout(function () { self.startBleConnect() }, 1000) },