fix: scale auto-reconnect + reconfigure button in kiosk settings

GatewayService:
- onScanStopped() now calls scheduleReconnect() when not connected, so
  if the scale is off at startup the service keeps retrying every 8s
  instead of giving up after the first 20s scan window

SettingsActivity:
- Add 'Riconfigura bilancia' button (yellow outline) when a scale is
  already configured — clears saved device, stops the gateway service,
  and opens SetupActivity directly at step 4 (scale scan step)

SetupActivity:
- Accept 'start_step' intent extra to jump directly to a specific step
  (used by SettingsActivity reconfigure flow)
This commit is contained in:
dadaloop82
2026-05-06 11:18:38 +00:00
parent 8eba5c8573
commit 6d098a80a6
4 changed files with 34 additions and 1 deletions
@@ -58,6 +58,7 @@ class SettingsActivity : AppCompatActivity() {
val statusView = findViewById<TextView>(R.id.scaleGatewayStatus)
val deviceView = findViewById<TextView>(R.id.scaleDeviceInfo)
val btnScaleAction = findViewById<MaterialButton>(R.id.btnConfigureGateway)
val btnReconfigureScale = findViewById<MaterialButton>(R.id.btnReconfigureScale)
when {
!hasScale || deviceAddr == null -> {
@@ -83,6 +84,20 @@ class SettingsActivity : AppCompatActivity() {
GatewayService.start(this)
Toast.makeText(this, "Servizio bilancia riavviato", Toast.LENGTH_SHORT).show()
}
btnReconfigureScale.visibility = android.view.View.VISIBLE
btnReconfigureScale.setOnClickListener {
GatewayService.stop(this)
prefs.edit()
.remove("scale_device_address")
.remove("scale_device_name")
.putBoolean(KEY_HAS_SCALE, false)
.putBoolean(KEY_SETUP_COMPLETE, false)
.apply()
val intent = Intent(this, SetupActivity::class.java)
intent.putExtra("start_step", 4)
startActivity(intent)
finish()
}
// Probe WebSocket port to show live status
Thread {
val running = try {
@@ -161,9 +161,11 @@ class SetupActivity : AppCompatActivity() {
bindViews()
// Restore step from instance state (e.g. after recreate() for locale change)
val savedStep = savedInstanceState?.getInt("step", -1) ?: -1
val startStepExtra = intent.getIntExtra("start_step", -1)
val langAlreadySet = prefs.getString(KEY_LANGUAGE, null) != null
showStep(when {
savedStep >= 0 -> savedStep
startStepExtra >= 0 -> startStepExtra
langAlreadySet -> 1
else -> 0
})
@@ -183,7 +183,10 @@ class GatewayService : Service(), BleScaleListener, ServerEventListener {
scheduleReconnect()
}
override fun onScanStopped() { /* auto-reconnect handles retries */ }
override fun onScanStopped() {
// If not connected yet, schedule a retry so we keep searching after the scale powers on
if (!bleManager.isConnected) scheduleReconnect()
}
override fun onDebugEvent(message: String) {
Log.d(TAG, message)
@@ -154,6 +154,19 @@
android:textColor="#34d399"
android:layout_marginTop="12dp"
android:visibility="gone" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnReconfigureScale"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="🔄 Riconfigura bilancia"
android:textSize="13sp"
android:textAllCaps="false"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:strokeColor="#fbbf24"
android:textColor="#fbbf24"
android:layout_marginTop="8dp"
android:visibility="gone" />
</LinearLayout>
<!-- Screensaver Section -->