feat(gateway): auto-reconnect to scale after disconnect (scale auto-off)

When the scale turns off by itself (auto-off after inactivity), onDisconnected()
now automatically restarts BLE scan after 5 s, with enableAutoConnect() set so
the saved scale is connected as soon as it starts advertising again.
The hint text shows '🔄 Reconnecting to saved scale in 5 s…' during the wait.
This commit is contained in:
dadaloop82
2026-04-15 21:17:34 +00:00
parent 951ef1d64f
commit 1c686fa842
@@ -243,6 +243,19 @@ class MainActivity : AppCompatActivity(), BleScaleListener, ServerEventListener
override fun onDisconnected() { override fun onDisconnected() {
wsServer?.publishStatus("disconnected", null, null) wsServer?.publishStatus("disconnected", null, null)
updateUiDisconnected() updateUiDisconnected()
// Auto-reconnect: if a saved device exists, restart scan after a short delay.
// This handles the scale turning off by itself (auto-off) — when it powers
// back on it will start advertising again and we will pick it up.
if (bleManager.getSavedDeviceAddress() != null && bleManager.hasRequiredPermissions()) {
binding.tvScanHint.visibility = View.VISIBLE
binding.tvScanHint.text = "🔄 Reconnecting to saved scale in 5 s…"
binding.root.postDelayed({
if (!bleManager.isConnected) {
bleManager.enableAutoConnect()
bleManager.startScan()
}
}, 5_000L)
}
} }
override fun onWeightReceived(reading: WeightReading) { override fun onWeightReceived(reading: WeightReading) {