aboutsummaryrefslogtreecommitdiff
path: root/android/app/src/main/java/jp/thb/tracker/ble
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 /android/app/src/main/java/jp/thb/tracker/ble
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.
Diffstat (limited to 'android/app/src/main/java/jp/thb/tracker/ble')
-rw-r--r--android/app/src/main/java/jp/thb/tracker/ble/Ble.kt29
1 files changed, 19 insertions, 10 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"