aboutsummaryrefslogtreecommitdiff
path: root/android/app/src/main/java/jp/thb/tracker/ble
diff options
context:
space:
mode:
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"