summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKoudai Kudo <k.kudo@dangofactory.net>2026-11-15 23:07:34 +0900
committerKoudai Kudo <k.kudo@dangofactory.net>2026-11-15 23:07:34 +0900
commitb92cfcaff0ed87103c7c8bd1d6e7ddb88e9495c8 (patch)
tree1d3c40199fedba242cab06b284e362b5817d274a
parentcf42fdfb2b4a7d1abb201189c08eddc12a867489 (diff)
downloadthb-tracker-master.tar.gz
thb-tracker-master.zip
Buzzer: start and stop, do not cut the song off after 10 sHEADmaster
ringBuzzer() held the connection for 10 s and then sent 0x1A 00, so a 163 s score always stopped a tenth of the way in. The tag keeps playing on its own, so setBuzzer() now only writes the command and drops the link (with a short delay so the write-without-response is really sent). 鳴らす turns into 止める while the tag is playing; a second press stops it. Backing out of the screen no longer needs the buzzer to finish first.
-rw-r--r--android/app/src/main/java/jp/thb/tracker/ble/Ble.kt29
-rw-r--r--android/app/src/main/java/jp/thb/tracker/ui/TagConfigScreen.kt13
-rw-r--r--android/app/src/main/java/jp/thb/tracker/ui/TrackScreen.kt25
3 files changed, 44 insertions, 23 deletions
diff --git a/android/app/src/main/java/jp/thb/tracker/ble/Ble.kt b/android/app/src/main/java/jp/thb/tracker/ble/Ble.kt
index 9ee8223..4f3ad05 100644
--- a/android/app/src/main/java/jp/thb/tracker/ble/Ble.kt
+++ b/android/app/src/main/java/jp/thb/tracker/ble/Ble.kt
@@ -403,17 +403,19 @@ suspend fun provisionTag(ctx: Context, device: BluetoothDevice, key: ByteArray):
}
/**
- * Rings the tag's buzzer (same command as the web UI's "Buzzer On/Off").
- * Keeps the connection for [seconds] so it can be stopped with 0x1A 0x00.
+ * Turns the tag's buzzer on or off (same command as the web UI's "Buzzer On/Off").
+ * The tag keeps playing after the link drops, so starting does not hold the
+ * connection open: the score is 163 s and a second press stops it.
*/
-suspend fun ringBuzzer(ctx: Context, device: BluetoothDevice, seconds: Int = 10): String =
+suspend fun setBuzzer(ctx: Context, device: BluetoothDevice, on: Boolean): String =
withContext(Dispatchers.IO) {
val s = openSession(ctx, device, notifications = false)
try {
- write(s.gatt, s.cmd, byteArrayOf(0x1A, 0x01))
- delay(seconds * 1000L)
- runCatching { write(s.gatt, s.cmd, byteArrayOf(0x1A, 0x00)) }
- "ブザーを ${seconds} 秒鳴らしました"
+ write(s.gatt, s.cmd, byteArrayOf(0x1A, if (on) 0x01 else 0x00))
+ // write() only queues the request; give the stack time to send it
+ // before the link goes away.
+ delay(400)
+ if (on) "鳴らしています" else "止めました"
} finally {
closeSession(s)
}
@@ -463,11 +465,18 @@ private suspend fun <T> withTag(
}
/**
- * Rings the tag at [address]. Returns a short status line for the UI.
+ * Turns the buzzer of the tag at [address] on or off. Returns a short status
+ * line for the UI, or null when the command went through.
*/
-suspend fun findAndRing(ctx: Context, address: String, timeoutMs: Long = 60_000L): String =
+suspend fun findAndSetBuzzer(
+ ctx: Context,
+ address: String,
+ on: Boolean,
+ timeoutMs: Long = 60_000L,
+): String? =
try {
- withTag(ctx, address, timeoutMs) { ringBuzzer(ctx, it, 10) }
+ withTag(ctx, address, timeoutMs) { setBuzzer(ctx, it, on) }
+ null
} catch (e: Exception) {
val m = e.message ?: e.toString()
if (m.startsWith("見つかりません")) m else "鳴らせませんでした: $m"
diff --git a/android/app/src/main/java/jp/thb/tracker/ui/TagConfigScreen.kt b/android/app/src/main/java/jp/thb/tracker/ui/TagConfigScreen.kt
index 5349f23..1e76826 100644
--- a/android/app/src/main/java/jp/thb/tracker/ui/TagConfigScreen.kt
+++ b/android/app/src/main/java/jp/thb/tracker/ui/TagConfigScreen.kt
@@ -42,8 +42,8 @@ import jp.thb.tracker.ble.readTagName
import jp.thb.tracker.ble.resetSensorConfig
import jp.thb.tracker.ble.resetTagConfig
import jp.thb.tracker.ble.resetTagName
-import jp.thb.tracker.ble.ringBuzzer
import jp.thb.tracker.ble.sendRawCommand
+import jp.thb.tracker.ble.setBuzzer
import jp.thb.tracker.ble.setMaxMtu
import jp.thb.tracker.ble.setTagName
import jp.thb.tracker.ble.setTagTime
@@ -84,6 +84,7 @@ fun TagConfigScreen(item: ScanItem, firstFocus: FocusRequester, onBack: () -> Un
var evCnt by remember { mutableStateOf("") }
var battS by remember { mutableStateOf("") }
var histIdx by remember { mutableStateOf("") }
+ var buzzOn by remember { mutableStateOf(false) }
var name by remember { mutableStateOf(item.name.takeUnless { it == "(名称なし)" }.orEmpty()) }
var mac by remember { mutableStateOf("") }
@@ -318,9 +319,15 @@ fun TagConfigScreen(item: ScanItem, firstFocus: FocusRequester, onBack: () -> Un
modifier = Modifier.focusRing(),
) { Text("時刻を合わせる") }
OutlinedButton(
- onClick = { act("ブザー") { ringBuzzer(appCtx, item.device, 3) } },
+ onClick = {
+ val on = !buzzOn
+ act(if (on) "ブザー" else "ブザー停止") {
+ setBuzzer(appCtx, item.device, on)
+ }
+ buzzOn = on
+ },
modifier = Modifier.focusRing(),
- ) { Text("ブザー 3秒") }
+ ) { Text(if (buzzOn) "ブザー停止" else "ブザー") }
}
Section("センサー補正(K=傾き, Z=オフセット)")
diff --git a/android/app/src/main/java/jp/thb/tracker/ui/TrackScreen.kt b/android/app/src/main/java/jp/thb/tracker/ui/TrackScreen.kt
index 6aa6b5f..8c9b2f5 100644
--- a/android/app/src/main/java/jp/thb/tracker/ui/TrackScreen.kt
+++ b/android/app/src/main/java/jp/thb/tracker/ui/TrackScreen.kt
@@ -44,7 +44,7 @@ import androidx.compose.ui.unit.dp
import androidx.activity.compose.BackHandler
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
-import jp.thb.tracker.ble.findAndRing
+import jp.thb.tracker.ble.findAndSetBuzzer
import jp.thb.tracker.data.Api
import jp.thb.tracker.data.Tag
import jp.thb.tracker.ui.components.HardwareKeys
@@ -265,6 +265,7 @@ private fun TagDetail(
val scope = rememberCoroutineScope()
var ringMsg by remember { mutableStateOf<String?>(null) }
var ringing by remember { mutableStateOf(false) }
+ var ringBusy by remember { mutableStateOf(false) }
val mapRef = remember { mutableStateOf<org.osmdroid.views.MapView?>(null) }
var showMap by remember { mutableStateOf(true) }
var mapFocused by remember { mutableStateOf(false) }
@@ -289,13 +290,13 @@ private fun TagDetail(
}
BackHandler {
- if (ringing || nameBusy) return@BackHandler
+ if (nameBusy) return@BackHandler
if (showMap && (mapFocused || HardwareKeys.mapActive)) releaseMap() else onBack()
}
// クリア (KEYCODE_CLEAR, e.g. from adb or a keyboard) does the same thing.
SideEffect {
HardwareKeys.clearPressed = {
- if (!ringing && !nameBusy) {
+ if (!nameBusy) {
if (showMap) releaseMap() else onBack()
}
}
@@ -378,18 +379,22 @@ private fun TagDetail(
// Ring the tag from the registered-tag screen. The BLE address is remembered
// by the BTHome tab (lastThbAddr), because the server only knows the FindMy key.
+ // The tag plays on its own, so a second press stops it instead of waiting.
fun ringTag() {
- if (ringing) return
+ if (ringBusy) return
val addr = prefs.getString("lastThbAddr", null)
if (addr.isNullOrBlank()) {
ringMsg = "BTHome タブで一度スキャンすると鳴らせます"
return
}
- ringing = true
- ringMsg = "探しています… ($addr)"
+ val stop = ringing
+ ringBusy = true
+ ringMsg = if (stop) "止めています…" else "探しています… ($addr)"
scope.launch {
- ringMsg = findAndRing(ctx, addr)
- ringing = false
+ val err = findAndSetBuzzer(ctx, addr, !stop)
+ ringing = err == null && !stop
+ ringMsg = err ?: if (stop) "止めました" else "鳴らし中(もう一度で止める)"
+ ringBusy = false
}
}
@@ -434,12 +439,12 @@ private fun TagDetail(
// Right after 地図: from the map, クリア then DOWN reaches the buzzer.
OutlinedButton(
onClick = { ringTag() },
- enabled = !ringing,
+ enabled = !ringBusy,
modifier = Modifier.focusRing(),
contentPadding = androidx.compose.foundation.layout.PaddingValues(
horizontal = 10.dp, vertical = 4.dp,
),
- ) { Text(if (ringing) "鳴らし中…" else "鳴らす") }
+ ) { Text(if (ringBusy) "…" else if (ringing) "止める" else "鳴らす") }
OutlinedButton(
onClick = { enterHistory() },
modifier = Modifier.focusRequester(histButton).focusRing(),