@mentra/bluetooth-sdk 3.2.0-dev.181 → 3.2.0-dev.187

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +22 -0
  2. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +15 -0
  3. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +8 -0
  4. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
  5. package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +9 -0
  6. package/android/src/main/java/com/mentra/bluetoothsdk/NativeNotifications.kt +42 -0
  7. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/EvenFileService.kt +101 -0
  8. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/EvenNotificationProtocol.kt +113 -0
  9. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/G2.kt +378 -24
  10. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NotificationIds.kt +19 -0
  11. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +11 -0
  12. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/EvenFileServiceTest.kt +153 -0
  13. package/build/BluetoothSdk.types.d.ts +50 -0
  14. package/build/BluetoothSdk.types.d.ts.map +1 -1
  15. package/build/BluetoothSdk.types.js.map +1 -1
  16. package/build/_private/BluetoothSdkModule.d.ts +5 -1
  17. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  18. package/build/_private/BluetoothSdkModule.js.map +1 -1
  19. package/build/generated/releaseMetadata.js +5 -5
  20. package/build/generated/releaseMetadata.js.map +1 -1
  21. package/build/index.d.ts +1 -0
  22. package/build/index.d.ts.map +1 -1
  23. package/build/index.js +4 -0
  24. package/build/index.js.map +1 -1
  25. package/ios/BluetoothSdkModule.swift +23 -0
  26. package/ios/Source/BluetoothSdkDefaults.swift +1 -1
  27. package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
  28. package/ios/Source/MentraBluetoothSDK.swift +10 -0
  29. package/ios/Source/NativeNotifications.swift +52 -0
  30. package/ios/Source/sgcs/G2.swift +131 -21
  31. package/ios/Source/sgcs/SGCManager.swift +22 -0
  32. package/ios/Tests/NativeNotificationTests.swift +20 -0
  33. package/package.json +1 -1
  34. package/src/BluetoothSdk.types.ts +54 -0
  35. package/src/_private/BluetoothSdkModule.ts +8 -0
  36. package/src/generated/releaseMetadata.ts +5 -5
  37. package/src/index.ts +6 -0
package/README.md CHANGED
@@ -295,6 +295,28 @@ await BluetoothSdk.clearDefaultDevice()
295
295
  await saveDeviceToYourAppStorage(null)
296
296
  ```
297
297
 
298
+ ## Native notification controls
299
+
300
+ G2 exposes firmware-owned notification history and popups. `getNativeNotificationStatus()` reports support, source (`phone` on Android or `ancs` on iOS), authorization, the desired controls, and submission/failure state. `submitted` means the controls were queued for the glasses, not that firmware acknowledged them. Unsupported drivers reject configuration instead of silently succeeding.
301
+
302
+ ```ts
303
+ await BluetoothSdk.configureNativeNotifications({
304
+ enabled: true,
305
+ autoDisplay: true, // false keeps history without automatic popups
306
+ durationSeconds: 5, // 1–30
307
+ doNotDisturb: false,
308
+ blockedApps: [], // Android package names; iOS requires an empty list
309
+ })
310
+ const status = await BluetoothSdk.getNativeNotificationStatus()
311
+ const subscription = BluetoothSdk.addListener('native_notification_status', handleStatus)
312
+ ```
313
+
314
+ The native SDKs expose the same configuration/status methods and `NativeNotificationConfig` type. Engine hosts should use the shared `engine.phoneNotifications` presentation policy and settings rather than competing with it through direct SDK configuration.
315
+
316
+ Android content uploads use the existing phone listener and a bounded G2 file-transfer queue. `native_notification_delivery` reports delivered, failed, cancelled, or dropped notifications by id without their content. Delivered means all three file phases acknowledged success. Disable, settings changes, and disconnect invalidate pending work. An interrupted or timed-out upload requires reconnect before another upload because the verified ACK format has no transaction id.
317
+
318
+ iOS G2 receives notifications directly through ANCS. Authorization is reported from the connected accessory and controls are reapplied after reconnect or authorization changes. No iOS content upload or full title/body relay is exposed. iOS keeps the firmware's existing app filter and rejects a nonempty `blockedApps` list. Phone dismissals are not yet synchronized to G2 history; the verified protocol does not establish a removal command.
319
+
298
320
  ## Common Commands
299
321
 
300
322
  ```ts
@@ -367,6 +367,8 @@ class BluetoothSdkModule : Module() {
367
367
  "audio_disconnected",
368
368
  "save_setting",
369
369
  "phone_notification",
370
+ "native_notification_status",
371
+ "native_notification_delivery",
370
372
  "phone_notification_dismissed",
371
373
  "ws_text",
372
374
  "ws_bin",
@@ -573,6 +575,19 @@ class BluetoothSdkModule : Module() {
573
575
  sdk?.sendIncidentId(incidentId, apiBaseUrl)
574
576
  }
575
577
 
578
+ // MARK: - Native Notification Centre
579
+
580
+ // Public controls use the SDK facade; phone payload delivery uses DeviceManager.
581
+ SdkAsyncFunction("configureNativeNotifications") { config: Map<String, Any> ->
582
+ requireSdk().configureNativeNotifications(NativeNotificationConfig.fromMap(config))
583
+ }
584
+ SdkAsyncFunction("getNativeNotificationStatus") { ->
585
+ requireSdk().getNativeNotificationStatus().toMap()
586
+ }
587
+ AsyncFunction("sendPhoneNotification") { notification: Map<String, Any> ->
588
+ deviceManager?.sendPhoneNotification(notification)
589
+ }
590
+
576
591
  // MARK: - WiFi Commands
577
592
 
578
593
  SdkCoroutineFunction("requestWifiScan") { -> requireSdk().requestWifiScan().map { it.toMap() } }
@@ -1789,6 +1789,14 @@ class DeviceManager {
1789
1789
  sgc?.sendIncidentId(incidentId, apiBaseUrl)
1790
1790
  }
1791
1791
 
1792
+ /** Push a notification into the glasses' own notification centre; rejects unsupported or disconnected devices. */
1793
+ fun sendPhoneNotification(notification: Map<String, Any>) {
1794
+ // Package only - never the notification text.
1795
+ Bridge.log("MAN: sendPhoneNotification from ${notification["packageName"]}")
1796
+ val driver = sgc ?: throw IllegalStateException("Glasses are not connected")
1797
+ driver.sendPhoneNotification(notification)
1798
+ }
1799
+
1792
1800
  fun sendWifiCredentials(ssid: String, password: String) {
1793
1801
  Bridge.log("MAN: Sending wifi credentials: $ssid")
1794
1802
  sgc?.sendWifiCredentials(ssid, password)
@@ -3,9 +3,9 @@ package com.mentra.bluetoothsdk
3
3
  /** Generated by release CI. Do not edit in a release checkout. */
4
4
  internal object GeneratedReleaseMetadata {
5
5
  const val FAMILY_BASE_VERSION: String = "3.2.0"
6
- const val RELEASE_IDENTITY: String = "3.2.0-dev.181"
7
- const val RELEASE_SET_ID: String = "mentra-3.2.0-dev.181"
8
- const val SOURCE_COMMIT: String = "89a4792321ce95bd16aadd721bc3dbc20e338861"
9
- const val OTA_MANIFEST_URL: String = "https://github.com/Mentra-Community/MentraOS/releases/download/mentra-builds-v3.2.0/mentra-live-ota-3.2.0-dev.181.json"
10
- const val OTA_MANIFEST_SHA256: String = "d9a100cc81733d02848a86c7617eff154f4cd9d714d1d36407789aeebbf71251"
6
+ const val RELEASE_IDENTITY: String = "3.2.0-dev.187"
7
+ const val RELEASE_SET_ID: String = "mentra-3.2.0-dev.187"
8
+ const val SOURCE_COMMIT: String = "d8b34892f11593baa3c075b94380253b06eb874d"
9
+ const val OTA_MANIFEST_URL: String = "https://github.com/Mentra-Community/MentraOS/releases/download/mentra-builds-v3.2.0/mentra-live-ota-3.2.0-dev.187.json"
10
+ const val OTA_MANIFEST_SHA256: String = "cf21f4ab6347acb12eefa38d2f0a3b433bc0e05428c527bcda57a8693d7a72f9"
11
11
  }
@@ -506,6 +506,15 @@ class MentraBluetoothSdk private constructor(
506
506
  deviceManager.displayEvent(request.toMap())
507
507
  }
508
508
 
509
+ /** Configure native notification presentation on a supported connected driver. */
510
+ fun configureNativeNotifications(config: NativeNotificationConfig) {
511
+ val driver = deviceManager.sgc ?: throw IllegalStateException("Glasses are not connected")
512
+ driver.configureNativeNotifications(config)
513
+ }
514
+
515
+ fun getNativeNotificationStatus(): NativeNotificationStatus =
516
+ deviceManager.sgc?.getNativeNotificationStatus() ?: NativeNotificationStatus()
517
+
509
518
  fun clearDisplay() {
510
519
  deviceManager.clearDisplay()
511
520
  }
@@ -0,0 +1,42 @@
1
+ package com.mentra.bluetoothsdk
2
+
3
+ /** Desired notification-centre controls. Completion means submitted, not firmware-confirmed. */
4
+ data class NativeNotificationConfig(
5
+ val enabled: Boolean = false,
6
+ val autoDisplay: Boolean = true,
7
+ val durationSeconds: Int = 5,
8
+ val doNotDisturb: Boolean = false,
9
+ val blockedApps: List<String> = emptyList(),
10
+ ) {
11
+ init { require(durationSeconds in 1..30) { "durationSeconds must be between 1 and 30" } }
12
+
13
+ fun toMap(): Map<String, Any> = mapOf(
14
+ "enabled" to enabled, "autoDisplay" to autoDisplay,
15
+ "durationSeconds" to durationSeconds, "doNotDisturb" to doNotDisturb, "blockedApps" to blockedApps,
16
+ )
17
+
18
+ companion object {
19
+ fun fromMap(value: Map<String, Any>) = NativeNotificationConfig(
20
+ enabled = value["enabled"] as? Boolean ?: false,
21
+ autoDisplay = value["autoDisplay"] as? Boolean ?: true,
22
+ durationSeconds = (value["durationSeconds"] as? Number)?.toInt() ?: 5,
23
+ doNotDisturb = value["doNotDisturb"] as? Boolean ?: false,
24
+ blockedApps = (value["blockedApps"] as? List<*>)?.filterIsInstance<String>() ?: emptyList(),
25
+ )
26
+ }
27
+ }
28
+
29
+ /** Availability is independent from notification content access and firmware acknowledgement. */
30
+ data class NativeNotificationStatus(
31
+ val supported: Boolean = false,
32
+ val source: String = "unsupported",
33
+ val authorization: String = "unknown",
34
+ val state: String = "unavailable",
35
+ val config: NativeNotificationConfig = NativeNotificationConfig(),
36
+ val error: String = "",
37
+ ) {
38
+ fun toMap(): Map<String, Any> = mapOf(
39
+ "supported" to supported, "source" to source, "authorization" to authorization,
40
+ "state" to state, "config" to config.toMap(), "error" to error,
41
+ )
42
+ }
@@ -0,0 +1,101 @@
1
+ package com.mentra.bluetoothsdk.sgcs
2
+
3
+ import java.io.IOException
4
+ import kotlinx.coroutines.CompletableDeferred
5
+ import kotlinx.coroutines.sync.Mutex
6
+ import kotlinx.coroutines.sync.withLock
7
+ import kotlinx.coroutines.withTimeoutOrNull
8
+
9
+ /** A single in-flight Even file upload. The wire ACK has no transaction id. */
10
+ internal class EvenFileService(
11
+ private val timeoutMs: Long = 15_000,
12
+ private val send: suspend (Byte, ByteArray) -> Unit,
13
+ ) {
14
+ private val mutex = Mutex()
15
+ private data class Pending(val generation: Long, val cid: Int, val result: CompletableDeferred<Int>)
16
+ @Volatile private var generation = 0L
17
+ @Volatile private var pending: Pending? = null
18
+ @Volatile var needsReconnect = false
19
+ private set
20
+
21
+ /** Only a new physical connection can clear an ambiguous in-flight transfer. */
22
+ @Synchronized fun resetConnection() {
23
+ generation++
24
+ pending?.result?.cancel()
25
+ pending = null
26
+ needsReconnect = false
27
+ }
28
+
29
+ fun acceptFrame(frame: ByteArray) {
30
+ decodeAckFrame(frame)?.let(::acceptAck)
31
+ }
32
+
33
+ /** Reject old-connection callbacks before calling this method. */
34
+ @Synchronized fun acceptAck(payload: ByteArray) {
35
+ if (payload.size != 2) return
36
+ val current = pending ?: return
37
+ if (current.generation == generation && current.cid == (payload[0].toInt() and 0xff)) {
38
+ current.result.complete(payload[1].toInt() and 0xff)
39
+ }
40
+ }
41
+
42
+ private suspend fun phase(epoch: Long, cid: Int, write: suspend () -> Unit): Int {
43
+ val ack = synchronized(this) {
44
+ check(epoch == generation) { "connection_changed" }
45
+ CompletableDeferred<Int>().also { pending = Pending(epoch, cid, it) }
46
+ }
47
+ try {
48
+ write()
49
+ return withTimeoutOrNull(timeoutMs) { ack.await() } ?: throw IOException("ack_timeout")
50
+ } finally {
51
+ synchronized(this) { if (pending?.result === ack) pending = null }
52
+ }
53
+ }
54
+
55
+ suspend fun transfer(fileType: Int, filename: String, bytes: ByteArray): Int = mutex.withLock {
56
+ check(!needsReconnect) { "needs_reconnect" }
57
+ val epoch = generation
58
+ try {
59
+ val start = phase(epoch, FileService.CID_SEND_START) {
60
+ send(0xC4.toByte(), FileService.sendStart(fileType, bytes.size, crc32(bytes), filename))
61
+ }
62
+ if (start != 0) return@withLock start
63
+ val data = phase(epoch, FileService.CID_SEND_DATA) {
64
+ send(0xC4.toByte(), FileService.sendData())
65
+ send(0xC5.toByte(), bytes)
66
+ }
67
+ if (data != 0) return@withLock data
68
+ phase(epoch, FileService.CID_SEND_RESULT_CHECK) {
69
+ send(0xC4.toByte(), FileService.resultCheck())
70
+ }
71
+ } catch (error: Exception) {
72
+ // No abort ACK or transaction id exists in the verified protocol. Reusing
73
+ // this channel after a timeout/cancel could accept the old transfer's ACK.
74
+ synchronized(this) { if (epoch == generation) needsReconnect = true }
75
+ throw error
76
+ }
77
+ }
78
+
79
+ companion object {
80
+ /** Verified ACK shape: one frame carrying cid/status plus payload CRC16. */
81
+ fun decodeAckFrame(frame: ByteArray): ByteArray? {
82
+ if (frame.size != 12 || frame[0] != 0xAA.toByte() || frame[3] != 4.toByte() ||
83
+ frame[4] != 1.toByte() || frame[5] != 1.toByte() ||
84
+ (frame[6] != 0xC4.toByte() && frame[6] != 0xC5.toByte()) ||
85
+ ((frame[7].toInt() shr 1) and 0x0F) != 0) return null
86
+ val payload = frame.copyOfRange(8, 10)
87
+ val crc = (frame[10].toInt() and 0xff) or ((frame[11].toInt() and 0xff) shl 8)
88
+ return payload.takeIf { g2Crc16(it) == crc }
89
+ }
90
+
91
+ /** CRC-32/Castagnoli, MSB-first, zero seed and no final xor (Even wire format). */
92
+ fun crc32(bytes: ByteArray): Int {
93
+ var crc = 0
94
+ for (byte in bytes) {
95
+ crc = crc xor ((byte.toInt() and 0xff) shl 24)
96
+ repeat(8) { crc = if (crc < 0) (crc shl 1) xor 0x1EDC6F41 else crc shl 1 }
97
+ }
98
+ return crc
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,113 @@
1
+ package com.mentra.bluetoothsdk.sgcs
2
+
3
+ import java.io.ByteArrayOutputStream
4
+
5
+ // ---------- Even File Service (service 0xC4 cmd / 0xC5 data) ----------
6
+
7
+ /**
8
+ * The glasses' generic file-push channel, and how notification content gets across: the body is a
9
+ * JSON document pushed as a file. Unlike every other G2 service this is **not** protobuf —
10
+ * SEND_START is a fixed 93-byte struct and the data phase writes raw bytes.
11
+ *
12
+ * Each phase is acked with a 2-byte `[cid][status]`:
13
+ * START(fileType, length, crc32, filename) → ack → DATA → raw bytes on 0xC5 → ack
14
+ * → RESULT_CHECK → ack
15
+ */
16
+ internal object FileService {
17
+ // eEvenFileSendServiceCID — first byte of every 0xC4 payload
18
+ const val CID_SEND_START = 0
19
+ const val CID_SEND_DATA = 1
20
+ const val CID_SEND_RESULT_CHECK = 2
21
+
22
+ const val TYPE_ANDROID_MSG_JSON_NOTIFICATION = 1
23
+
24
+ // Notification bodies and the whitelist share this path: the firmware has one filename
25
+ // constant (`BleG2GlassesFilePath.notifyWhitelist`) and discriminates on `fileType`.
26
+ const val PATH_NOTIFY = "user/notify_whitelist.json"
27
+
28
+ const val FILENAME_FIELD_LEN = 80
29
+ const val SEND_START_LEN = 93 // 1 + 4 + 4 + 4 + 80
30
+
31
+ // eEvenFileServiceRsp
32
+ fun statusName(status: Int): String =
33
+ when (status) {
34
+ 0 -> "SUCCESS"
35
+ 1 -> "START_ERR"
36
+ 2 -> "DATA_CRC_ERR"
37
+ 3 -> "FLASH_WRITE_ERR"
38
+ 4 -> "TIMEOUT"
39
+ 5 -> "NO_RESOURCES"
40
+ 6 -> "RESULT_CHECK_FAIL"
41
+ 7 -> "FAIL"
42
+ 8 -> "CANCEL"
43
+ else -> "status_$status"
44
+ }
45
+
46
+ private fun ByteArrayOutputStream.writeU32LE(value: Int) {
47
+ write(value and 0xFF)
48
+ write((value ushr 8) and 0xFF)
49
+ write((value ushr 16) and 0xFF)
50
+ write((value ushr 24) and 0xFF)
51
+ }
52
+
53
+ /** 93-byte fixed struct: cid | fileType u32 | fileLength u32 | fileCrc32 u32 | filename[80]. */
54
+ fun sendStart(fileType: Int, fileLength: Int, fileCrc32: Int, filename: String): ByteArray {
55
+ val out = ByteArrayOutputStream()
56
+ out.write(CID_SEND_START)
57
+ out.writeU32LE(fileType)
58
+ out.writeU32LE(fileLength)
59
+ out.writeU32LE(fileCrc32)
60
+
61
+ val nameBytes = filename.toByteArray(Charsets.US_ASCII)
62
+ require(nameBytes.size < FILENAME_FIELD_LEN) { "filename too long: $filename" }
63
+ out.write(nameBytes)
64
+ repeat(FILENAME_FIELD_LEN - nameBytes.size) { out.write(0) } // NUL-padded to 80
65
+
66
+ return out.toByteArray().also { check(it.size == SEND_START_LEN) }
67
+ }
68
+
69
+ fun sendData(): ByteArray = byteArrayOf(CID_SEND_DATA.toByte())
70
+
71
+ fun resultCheck(): ByteArray = byteArrayOf(CID_SEND_RESULT_CHECK.toByte())
72
+ }
73
+
74
+ // ---------- Notification payload JSON ----------
75
+
76
+ /**
77
+ * The JSON document the glasses expect on the file service — these nine fields, exactly. Schema
78
+ * confirmed against a BLE capture of the Even app; see `notes/g2-notification-service.md`.
79
+ *
80
+ * `time_s` is UTC epoch seconds while `date` is **device-local** wall time; formatting `date` in
81
+ * UTC would skew every displayed timestamp by the device's offset.
82
+ */
83
+ internal object NotificationJson {
84
+ fun androidNotification(
85
+ msgId: Int,
86
+ action: Int,
87
+ appIdentifier: String,
88
+ title: String,
89
+ subtitle: String,
90
+ message: String,
91
+ postTimeMs: Long,
92
+ displayName: String
93
+ ): ByteArray {
94
+ val dateFmt = java.text.SimpleDateFormat("yyyyMMdd'T'HHmmss", java.util.Locale.US)
95
+ val body =
96
+ org.json.JSONObject()
97
+ .put("msg_id", msgId)
98
+ .put("action", action)
99
+ .put("app_identifier", appIdentifier)
100
+ .put("title", title)
101
+ .put("subtitle", subtitle)
102
+ .put("message", message)
103
+ .put("time_s", postTimeMs / 1000)
104
+ .put("date", dateFmt.format(java.util.Date(postTimeMs)))
105
+ .put("display_name", displayName)
106
+
107
+ return org.json.JSONObject()
108
+ .put("android_notification", body)
109
+ .toString()
110
+ .toByteArray(Charsets.UTF_8)
111
+ }
112
+ }
113
+