@mentra/bluetooth-sdk 3.4.0-dev.999 → 3.4.1-beta.1409

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 (216) hide show
  1. package/MIGRATION.md +16 -7
  2. package/README.md +41 -14
  3. package/android/build.gradle +60 -51
  4. package/android/gradle.properties +3 -0
  5. package/android/settings.gradle +47 -3
  6. package/android/src/main/assets/nimo/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
  7. package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
  8. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -1
  9. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +77 -6
  10. package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +41 -0
  11. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +131 -122
  12. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +3 -2
  13. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
  14. package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +31 -10
  15. package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +57 -196
  16. package/android/src/main/java/com/mentra/bluetoothsdk/OtaManifest.kt +1 -16
  17. package/android/src/main/java/com/mentra/bluetoothsdk/debug/BleTraceLogger.kt +10 -1
  18. package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
  19. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/Ipv4Prefix.kt +51 -0
  20. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/NetworkFacts.kt +81 -0
  21. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkError.kt +108 -0
  22. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadiness.kt +103 -0
  23. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpec.kt +44 -0
  24. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkState.kt +117 -0
  25. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/ScopedSoftApNetwork.kt +598 -0
  26. package/android/src/main/java/com/mentra/bluetoothsdk/hotspot/SoftApTrace.kt +143 -0
  27. package/android/src/main/java/com/mentra/bluetoothsdk/otaserver/MentraOtaServerModule.kt +0 -7
  28. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/ClassicAudioConnectionTracker.kt +15 -0
  29. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +308 -60
  30. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +14 -5
  31. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +210 -325
  32. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +0 -35
  33. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +2 -10
  34. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdater.kt +213 -0
  35. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocol.kt +112 -0
  36. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
  37. package/android/src/main/java/com/mentra/bluetoothsdk/streaming/StreamModels.kt +17 -6
  38. package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
  39. package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
  40. package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
  41. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
  42. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
  43. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
  44. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
  45. package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
  46. package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +2 -2
  47. package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
  48. package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -0
  49. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
  50. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
  51. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
  52. package/android/src/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
  53. package/android/src/test/java/com/mentra/bluetoothsdk/StreamRequestCaptureAudioTest.kt +23 -4
  54. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/Ipv4PrefixTest.kt +59 -0
  55. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/NetworkFactsTest.kt +59 -0
  56. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedLocalNetworkPermissionTest.kt +35 -0
  57. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkErrorTest.kt +85 -0
  58. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkLifecycleTest.kt +164 -0
  59. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkReadinessTest.kt +141 -0
  60. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/ScopedNetworkRequestSpecTest.kt +56 -0
  61. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/SoftApTraceTest.kt +97 -0
  62. package/android/src/test/java/com/mentra/bluetoothsdk/hotspot/UnavailableRetryTest.kt +47 -0
  63. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/ClassicAudioConnectionTrackerTest.kt +26 -0
  64. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +141 -0
  65. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
  66. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +9 -3
  67. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveRgbLedResponseTest.kt +76 -0
  68. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
  69. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
  70. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareTargetTest.kt +15 -0
  71. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoFirmwareUpdaterTest.kt +76 -0
  72. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoOtaProtocolTest.kt +77 -0
  73. package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -0
  74. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoReadinessDriverTest.kt +31 -1
  75. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
  76. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
  77. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
  78. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
  79. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
  80. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
  81. package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
  82. package/build/BluetoothSdk.types.d.ts +103 -5
  83. package/build/BluetoothSdk.types.d.ts.map +1 -1
  84. package/build/BluetoothSdk.types.js.map +1 -1
  85. package/build/_private/BluetoothSdkModule.d.ts +42 -8
  86. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  87. package/build/_private/BluetoothSdkModule.js +1 -0
  88. package/build/_private/BluetoothSdkModule.js.map +1 -1
  89. package/build/_private/MentraLocalNetworkModule.d.ts +1 -1
  90. package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
  91. package/build/_private/MentraLocalNetworkModule.js.map +1 -1
  92. package/build/generated/changelogCatalog.d.ts +5 -2
  93. package/build/generated/changelogCatalog.d.ts.map +1 -1
  94. package/build/generated/changelogCatalog.js +6 -2
  95. package/build/generated/changelogCatalog.js.map +1 -1
  96. package/build/generated/releaseMetadata.js +6 -6
  97. package/build/generated/releaseMetadata.js.map +1 -1
  98. package/build/index.d.ts +1 -1
  99. package/build/index.d.ts.map +1 -1
  100. package/build/index.js +7 -1
  101. package/build/index.js.map +1 -1
  102. package/build/ota-server/MentraOtaServerModule.d.ts +0 -2
  103. package/build/ota-server/MentraOtaServerModule.d.ts.map +1 -1
  104. package/build/ota-server/MentraOtaServerModule.js.map +1 -1
  105. package/build/ota-server/MentraOtaServerModule.web.d.ts +0 -1
  106. package/build/ota-server/MentraOtaServerModule.web.d.ts.map +1 -1
  107. package/build/ota-server/MentraOtaServerModule.web.js +0 -3
  108. package/build/ota-server/MentraOtaServerModule.web.js.map +1 -1
  109. package/build/ota-transport/index.d.ts +3 -3
  110. package/build/ota-transport/index.d.ts.map +1 -1
  111. package/build/ota-transport/index.js +4 -4
  112. package/build/ota-transport/index.js.map +1 -1
  113. package/build/react/useGlassesConnection.d.ts +1 -1
  114. package/build/react/useGlassesConnection.d.ts.map +1 -1
  115. package/build/react/useGlassesConnection.js +3 -3
  116. package/build/react/useGlassesConnection.js.map +1 -1
  117. package/build/react/useMentraBluetooth.d.ts +1 -1
  118. package/build/react/useMentraBluetooth.d.ts.map +1 -1
  119. package/build/react/useMentraBluetooth.js +1 -1
  120. package/build/react/useMentraBluetooth.js.map +1 -1
  121. package/ios/BluetoothSdkModule.swift +73 -22
  122. package/ios/LocalIPv4.swift +0 -61
  123. package/ios/MentraBluetoothSDK.podspec +7 -1
  124. package/ios/MentraOtaServerModule.swift +0 -16
  125. package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
  126. package/ios/Resources/NimoFirmware/nimo-firmware-0.1.1.1-537cf1.bin +0 -0
  127. package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
  128. package/ios/Source/BesCrashReportWaiters.swift +64 -0
  129. package/ios/Source/BluetoothSdkDefaults.swift +4 -4
  130. package/ios/Source/Bridge.swift +14 -0
  131. package/ios/Source/DeviceManager.swift +52 -22
  132. package/ios/Source/GeneratedChangelogCatalog.swift +3 -2
  133. package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
  134. package/ios/Source/MentraBluetoothSDK.swift +35 -16
  135. package/ios/Source/OtaManifest.swift +1 -17
  136. package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
  137. package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
  138. package/ios/Source/audio/SpeechActivity.swift +277 -0
  139. package/ios/Source/events/BluetoothEvents.swift +41 -0
  140. package/ios/Source/internal/BleTraceLogger.swift +14 -1
  141. package/ios/Source/internal/BluetoothAvailability.swift +15 -5
  142. package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +5 -0
  143. package/ios/Source/sgcs/MentraLive.swift +207 -66
  144. package/ios/Source/sgcs/Nimo.swift +249 -276
  145. package/ios/Source/sgcs/NimoCanvasCoordinator.swift +9 -38
  146. package/ios/Source/sgcs/NimoCanvasSession.swift +0 -1
  147. package/ios/Source/sgcs/NimoFirmwareUpdater.swift +260 -0
  148. package/ios/Source/sgcs/NimoOtaProtocol.swift +159 -0
  149. package/ios/Source/sgcs/SGCManager.swift +6 -0
  150. package/ios/Source/streaming/StreamModels.swift +15 -6
  151. package/ios/Source/stt/STTTools.swift +13 -31
  152. package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
  153. package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
  154. package/ios/Source/stt/WakeWordDetector.swift +145 -0
  155. package/ios/Source/utils/BoundedWait.swift +44 -0
  156. package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
  157. package/ios/Source/utils/SttModelTraits.swift +62 -0
  158. package/ios/Tests/BesCrashReportTests.swift +142 -0
  159. package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
  160. package/ios/Tests/DeviceManagerReconnectTests.swift +0 -71
  161. package/ios/Tests/GlassesAudioSetupPolicyTests.swift +9 -3
  162. package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
  163. package/ios/Tests/MentraLiveAudioSetupTests.swift +6 -12
  164. package/ios/Tests/MentraLiveCommandQueueTests.swift +35 -0
  165. package/ios/Tests/MentraLiveSessionReadinessTests.swift +179 -10
  166. package/ios/Tests/NimoCanvasTransportTests.swift +0 -89
  167. package/ios/Tests/NimoOtaProtocolTests.swift +78 -0
  168. package/ios/Tests/PcmStreamTests.swift +299 -0
  169. package/ios/Tests/SpeechActivityTests.swift +238 -0
  170. package/ios/Tests/StreamRequestTests.swift +18 -0
  171. package/ios/Tests/SttModelTraitsTests.swift +62 -0
  172. package/package.json +2 -2
  173. package/plugin/build/withAndroid.js +9 -11
  174. package/plugin/build/withKotlinVersion.d.ts +2 -0
  175. package/plugin/build/withKotlinVersion.js +46 -0
  176. package/src/BluetoothSdk.types.ts +107 -5
  177. package/src/_private/BluetoothSdkModule.ts +45 -8
  178. package/src/_private/MentraLocalNetworkModule.ts +5 -1
  179. package/src/generated/changelogCatalog.ts +6 -2
  180. package/src/generated/releaseMetadata.ts +6 -6
  181. package/src/index.ts +13 -1
  182. package/src/ota-server/MentraOtaServerModule.ts +0 -2
  183. package/src/ota-server/MentraOtaServerModule.web.ts +0 -4
  184. package/src/ota-transport/index.ts +4 -5
  185. package/src/react/useGlassesConnection.ts +4 -4
  186. package/src/react/useMentraBluetooth.ts +2 -2
  187. package/android/silero/LICENSE.md +0 -17
  188. package/android/silero/build.gradle +0 -50
  189. package/android/silero/proguard-rules.pro +0 -24
  190. package/android/silero/src/androidTest/assets/hello.wav +0 -0
  191. package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
  192. package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
  193. package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
  194. package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
  195. package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
  196. package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
  197. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
  198. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
  199. package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
  200. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
  201. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
  202. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -274
  203. package/android/src/main/java/com/mentra/bluetoothsdk/ScopedNetworkReadiness.kt +0 -17
  204. package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
  205. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
  206. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +0 -16
  207. package/android/src/test/java/com/mentra/bluetoothsdk/ScopedNetworkReadinessTest.kt +0 -39
  208. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
  209. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
  210. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -851
  211. package/ios/Source/sgcs/NimoMacResourceTransport.swift +0 -217
  212. package/ios/Source/sgcs/NimoResourceFixture.swift +0 -384
  213. package/ios/Source/sgcs/NimoResourceSession.swift +0 -406
  214. package/ios/Tests/NimoResourceSessionTests.swift +0 -476
  215. /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
  216. /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
@@ -0,0 +1,108 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ /**
4
+ * Typed failures for a scoped SoftAP join.
5
+ *
6
+ * A denied local-network permission must surface as a distinct, immediate error rather than a
7
+ * join that never completes, which looks like a flaky hotspot. `ACCESS_LOCAL_NETWORK` sits in the
8
+ * nearby-devices group, so it is often already granted during development and easy to miss until a
9
+ * user without it hits it.
10
+ */
11
+ sealed class ScopedNetworkError(val code: String, message: String) : Exception(message) {
12
+
13
+ /** `ACCESS_LOCAL_NETWORK` (or the nearby-devices prerequisite) was not granted. */
14
+ class PermissionDenied(val permission: String) :
15
+ ScopedNetworkError(
16
+ CODE_PERMISSION_DENIED,
17
+ "Local network permission not granted: $permission",
18
+ )
19
+
20
+ /** The join was released by its owner before a usable network arrived. */
21
+ class Cancelled(val ssid: String) :
22
+ ScopedNetworkError(CODE_CANCELLED, "Joining $ssid was cancelled")
23
+
24
+ /**
25
+ * The framework reported it cannot provide this network at all.
26
+ *
27
+ * WifiNetworkSpecifier does not keep scanning until this arrives. If the glasses AP is
28
+ * not in the current scan — or the user dismissed the system join sheet — this arrives in
29
+ * well under a second.
30
+ */
31
+ class Unavailable(val ssid: String) :
32
+ ScopedNetworkError(
33
+ CODE_UNAVAILABLE,
34
+ "Could not join $ssid (SSID not in scan, Wi-Fi off, or the system join prompt was dismissed)",
35
+ )
36
+
37
+ /** The network was joined and then went away mid-call. */
38
+ class Lost(val ssid: String) :
39
+ ScopedNetworkError(CODE_LOST, "Lost the connection to $ssid")
40
+
41
+ /** `requestNetwork` itself threw. */
42
+ class RequestFailed(val reason: String) :
43
+ ScopedNetworkError(CODE_REQUEST_FAILED, "Scoped network request failed: $reason")
44
+
45
+ /** Joined, but no IPv4 address was assigned, so nothing can bind to the link. */
46
+ class NoLocalAddress(val ssid: String) :
47
+ ScopedNetworkError(CODE_NO_LOCAL_ADDRESS, "Joined $ssid but no IPv4 address was assigned")
48
+
49
+ /**
50
+ * Joined, but the network never became usable for ICE: a callback never reported, or the
51
+ * address never appeared in the kernel's interface table.
52
+ *
53
+ * Failing here is deliberate. Proceeding produces an ICE answer with no hotspot candidate,
54
+ * which is the same symptom as several unrelated faults and cost real time to attribute.
55
+ */
56
+ class NotReady(val ssid: String, val missing: String) :
57
+ ScopedNetworkError(
58
+ CODE_NOT_READY,
59
+ "Joined $ssid but the network never became usable: still waiting for $missing",
60
+ )
61
+
62
+ /** Phone Wi-Fi radio is off. WifiNetworkSpecifier then fails as Unavailable in under a second. */
63
+ class WifiDisabled :
64
+ ScopedNetworkError(CODE_WIFI_DISABLED, "Phone Wi-Fi is off; turn it on before joining the glasses hotspot")
65
+
66
+ /**
67
+ * A VPN owns this app's UID. netd then refuses to bind our sockets to the hotspot network
68
+ * (`EPERM`), and the glasses' TCP handshake to the WHIP listener never completes because the
69
+ * reply path is captured by the tunnel. Verified on-device: a shell-UID listener on the same
70
+ * address accepts the glasses' connection while an app-UID listener times out. The scoped join
71
+ * itself succeeds, so without this check the failure only shows up ten seconds into the camera
72
+ * step as an opaque connect timeout.
73
+ */
74
+ class VpnCapturesApp :
75
+ ScopedNetworkError(
76
+ CODE_VPN_ACTIVE,
77
+ "A VPN is routing this app's traffic, so the glasses cannot reach the phone. " +
78
+ "Exclude Mentra from the VPN (split tunneling) or turn the VPN off, then retry",
79
+ )
80
+
81
+ companion object {
82
+ const val CODE_PERMISSION_DENIED = "SOFTAP_PERMISSION_DENIED"
83
+ const val CODE_CANCELLED = "SOFTAP_JOIN_CANCELLED"
84
+ const val CODE_UNAVAILABLE = "SOFTAP_UNAVAILABLE"
85
+ const val CODE_LOST = "SOFTAP_NETWORK_LOST"
86
+ const val CODE_REQUEST_FAILED = "SOFTAP_REQUEST_FAILED"
87
+ const val CODE_NO_LOCAL_ADDRESS = "SOFTAP_NO_LOCAL_ADDRESS"
88
+ const val CODE_NOT_READY = "SOFTAP_NETWORK_NOT_READY"
89
+ const val CODE_WIFI_DISABLED = "SOFTAP_WIFI_DISABLED"
90
+ const val CODE_VPN_ACTIVE = "SOFTAP_VPN_ACTIVE"
91
+
92
+ /** Map a terminal state-machine failure onto the typed error the call path reports. */
93
+ fun from(failure: ScopedNetworkState.Failure, ssid: String): ScopedNetworkError? =
94
+ when (failure) {
95
+ ScopedNetworkState.Failure.NONE -> null
96
+ ScopedNetworkState.Failure.UNAVAILABLE -> Unavailable(ssid)
97
+ ScopedNetworkState.Failure.LOST -> Lost(ssid)
98
+ ScopedNetworkState.Failure.PERMISSION_DENIED -> PermissionDenied(LOCAL_NETWORK_PERMISSION)
99
+ ScopedNetworkState.Failure.REQUEST_FAILED -> RequestFailed("requestNetwork threw")
100
+ }
101
+
102
+ /**
103
+ * Enforced on Android 17+ for apps targeting SDK 37+. Hosts targeting older SDKs
104
+ * retain implicit access through INTERNET and must not request this permission.
105
+ */
106
+ const val LOCAL_NETWORK_PERMISSION = "android.permission.ACCESS_LOCAL_NETWORK"
107
+ }
108
+ }
@@ -0,0 +1,103 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ /**
4
+ * Decides when a joined scoped network is actually ready for ICE to gather on.
5
+ *
6
+ * `onAvailable` is not that moment. Android's own guidance is to wait for `onLinkPropertiesChanged`
7
+ * and `onCapabilitiesChanged` rather than reading properties synchronously inside `onAvailable`, and
8
+ * on device the consequence was concrete: the hotspot address can land in the kernel's interface
9
+ * table *after* `onAvailable` returns, and libwebrtc gathers from that table. Starting ingest on
10
+ * `onAvailable` therefore raced the address into existence, and losing that race is indistinguishable
11
+ * from the wiring bugs it was mistaken for.
12
+ *
13
+ * Readiness needs agreement from three places, so all three are recorded separately and the missing
14
+ * one is named. The kernel table is the odd one out: it has no callback, so the shell polls it and
15
+ * feeds the answer in through [onInterfaceTable].
16
+ *
17
+ * Pure and mutable-but-self-contained, so the awkward orderings are unit testable without a device.
18
+ */
19
+ class ScopedNetworkReadiness {
20
+
21
+ /** What each source has reported so far. Null means "not yet", not "absent". */
22
+ data class Observations(
23
+ val available: Boolean = false,
24
+ val capabilities: Boolean = false,
25
+ val linkInterface: String? = null,
26
+ val linkAddress: String? = null,
27
+ val tableOwner: String? = null,
28
+ )
29
+
30
+ sealed interface Verdict {
31
+ /**
32
+ * [owner] is the interface the kernel says holds [address]. It can differ from
33
+ * [interfaceName], which is what `LinkProperties` claimed — a disagreement worth logging,
34
+ * not worth blocking on, since ICE only ever uses the address.
35
+ */
36
+ data class Ready(
37
+ val address: String,
38
+ val interfaceName: String?,
39
+ val owner: String,
40
+ ) : Verdict
41
+
42
+ /** [missing] names the one source still to report, for the timeout message. */
43
+ data class Waiting(val missing: String) : Verdict
44
+ }
45
+
46
+ @Volatile
47
+ var observations = Observations()
48
+ private set
49
+
50
+ private val lock = Any()
51
+
52
+ fun onAvailable() = update { it.copy(available = true) }
53
+
54
+ fun onCapabilities() = update { it.copy(capabilities = true) }
55
+
56
+ /**
57
+ * Taken verbatim: `LinkProperties` carries the network's whole current state, so a later
58
+ * callback without an address means the address is gone, not that the old one still holds.
59
+ */
60
+ fun onLinkProperties(interfaceName: String?, address: String?) =
61
+ update { it.copy(linkInterface = interfaceName, linkAddress = address) }
62
+
63
+ /** Seed from the synchronous read in `onAvailable`; never clears what a callback reported. */
64
+ fun seedLinkProperties(interfaceName: String?, address: String?) =
65
+ update {
66
+ if (address == null) it else it.copy(linkInterface = interfaceName, linkAddress = address)
67
+ }
68
+
69
+ fun onInterfaceTable(owner: String?) = update { it.copy(tableOwner = owner) }
70
+
71
+ fun reset() = update { Observations() }
72
+
73
+ fun verdict(): Verdict = verdictOf(observations)
74
+
75
+ /** True once everything with a callback has reported, so only the table poll is left. */
76
+ fun callbacksSatisfied(): Boolean = callbacksSatisfiedBy(observations)
77
+
78
+ private inline fun update(transform: (Observations) -> Observations) {
79
+ synchronized(lock) { observations = transform(observations) }
80
+ }
81
+
82
+ companion object {
83
+ fun callbacksSatisfiedBy(observations: Observations): Boolean =
84
+ observations.available && observations.capabilities && observations.linkAddress != null
85
+
86
+ /** Ordered so the named blocker is the earliest unmet one, which is the useful one. */
87
+ fun verdictOf(observations: Observations): Verdict =
88
+ when {
89
+ !observations.available -> Verdict.Waiting("onAvailable")
90
+ !observations.capabilities -> Verdict.Waiting("onCapabilitiesChanged")
91
+ observations.linkAddress == null ->
92
+ Verdict.Waiting("an IPv4 address from onLinkPropertiesChanged")
93
+ observations.tableOwner == null ->
94
+ Verdict.Waiting("the kernel interface table to carry ${observations.linkAddress}")
95
+ else ->
96
+ Verdict.Ready(
97
+ observations.linkAddress,
98
+ observations.linkInterface,
99
+ observations.tableOwner,
100
+ )
101
+ }
102
+ }
103
+ }
@@ -0,0 +1,44 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ import android.net.NetworkCapabilities
4
+
5
+ /**
6
+ * Declarative description of the SoftAP network request.
7
+ *
8
+ * `NetworkRequest.Builder` cannot be constructed in a plain JVM test, so the shape of the request
9
+ * lives here as data and [ScopedSoftApNetwork] translates it into the real builder. That makes the
10
+ * one property this architecture depends on — that the request does **not** ask for
11
+ * `NET_CAPABILITY_INTERNET` — assertable without a device.
12
+ *
13
+ * Dropping the internet capability is what keeps the phone dual-homed: Android will not promote the
14
+ * glasses hotspot to the default network, so ACS traffic stays on cellular while media crosses the
15
+ * local link. The other half of that guarantee is the MTK firmware patch that stops the AP
16
+ * advertising a default route.
17
+ */
18
+ data class ScopedNetworkRequestSpec(
19
+ val ssid: String,
20
+ val passphrase: String,
21
+ val transportTypes: Set<Int>,
22
+ val removedCapabilities: Set<Int>,
23
+ ) {
24
+ /** True when the request cannot be satisfied by, or promoted to, an internet-bearing network. */
25
+ val avoidsInternetCapability: Boolean
26
+ get() = removedCapabilities.contains(NetworkCapabilities.NET_CAPABILITY_INTERNET)
27
+
28
+ val hasPassphrase: Boolean
29
+ get() = passphrase.isNotEmpty()
30
+
31
+ companion object {
32
+ /** The canonical SoftAP request. Every caller should go through this. */
33
+ fun forSoftAp(
34
+ ssid: String,
35
+ passphrase: String,
36
+ ): ScopedNetworkRequestSpec =
37
+ ScopedNetworkRequestSpec(
38
+ ssid = ssid,
39
+ passphrase = passphrase,
40
+ transportTypes = setOf(NetworkCapabilities.TRANSPORT_WIFI),
41
+ removedCapabilities = setOf(NetworkCapabilities.NET_CAPABILITY_INTERNET),
42
+ )
43
+ }
44
+ }
@@ -0,0 +1,117 @@
1
+ package com.mentra.bluetoothsdk.hotspot
2
+
3
+ /**
4
+ * Pure lifecycle state machine for a scoped SoftAP network join.
5
+ *
6
+ * Split from the Android shell so the awkward parts — duplicate callbacks, callbacks arriving after
7
+ * a cancel racing a late `onAvailable` — are unit testable without a device. The shell
8
+ * ([ScopedSoftApNetwork]) owns `ConnectivityManager` and forwards raw callbacks in here.
9
+ *
10
+ * `ConnectivityManager` gives no ordering or at-most-once guarantee: `onAvailable` can fire more
11
+ * than once, `onCapabilitiesChanged` fires repeatedly, and callbacks from a request that was already
12
+ * unregistered can still land. Every transition below is therefore idempotent and generation-guarded.
13
+ */
14
+ class ScopedNetworkState {
15
+
16
+ enum class Phase {
17
+ /** No request outstanding. */
18
+ IDLE,
19
+
20
+ /** `requestNetwork` issued, waiting for a usable network. */
21
+ REQUESTING,
22
+
23
+ /** Network joined and usable. */
24
+ AVAILABLE,
25
+
26
+ /** Terminal for this generation: released, lost, or failed. */
27
+ LOST,
28
+ }
29
+
30
+ /** Why a join ended without producing a usable network. */
31
+ enum class Failure {
32
+ NONE,
33
+ UNAVAILABLE,
34
+ LOST,
35
+ PERMISSION_DENIED,
36
+ REQUEST_FAILED,
37
+ }
38
+
39
+ var phase: Phase = Phase.IDLE
40
+ private set
41
+
42
+ var failure: Failure = Failure.NONE
43
+ private set
44
+
45
+ /** Incremented on every new request so late callbacks from an old one can be rejected. */
46
+ var generation: Int = 0
47
+ private set
48
+
49
+ val isActive: Boolean
50
+ get() = phase == Phase.REQUESTING || phase == Phase.AVAILABLE
51
+
52
+ /**
53
+ * Begin a new join. Any in-flight generation is abandoned, so its callbacks become stale.
54
+ *
55
+ * @return the generation for this request; pass it back to every callback
56
+ */
57
+ fun startRequest(): Int {
58
+ generation += 1
59
+ phase = Phase.REQUESTING
60
+ failure = Failure.NONE
61
+ return generation
62
+ }
63
+
64
+ /** True when a callback belongs to the current, still-active generation. */
65
+ fun accepts(callbackGeneration: Int): Boolean = callbackGeneration == generation && isActive
66
+
67
+ /**
68
+ * A usable network arrived.
69
+ *
70
+ * @return true if this transitioned the machine; false when stale or already available
71
+ */
72
+ fun onAvailable(callbackGeneration: Int): Boolean {
73
+ if (!accepts(callbackGeneration)) return false
74
+ if (phase == Phase.AVAILABLE) return false // duplicate onAvailable
75
+ phase = Phase.AVAILABLE
76
+ return true
77
+ }
78
+
79
+ /** The framework reported the network cannot be provided. */
80
+ fun onUnavailable(callbackGeneration: Int): Boolean {
81
+ if (!accepts(callbackGeneration)) return false
82
+ phase = Phase.LOST
83
+ failure = Failure.UNAVAILABLE
84
+ return true
85
+ }
86
+
87
+ /** The joined network went away. This is the one the call path must react to. */
88
+ fun onLost(callbackGeneration: Int): Boolean {
89
+ if (!accepts(callbackGeneration)) return false
90
+ phase = Phase.LOST
91
+ failure = Failure.LOST
92
+ return true
93
+ }
94
+
95
+ /** `requestNetwork` threw, or the local-network permission was denied. */
96
+ fun onRequestFailed(callbackGeneration: Int, permissionDenied: Boolean): Boolean {
97
+ if (callbackGeneration != generation) return false
98
+ if (!isActive) return false
99
+ phase = Phase.LOST
100
+ failure = if (permissionDenied) Failure.PERMISSION_DENIED else Failure.REQUEST_FAILED
101
+ return true
102
+ }
103
+
104
+ /** Caller-initiated teardown. Not a failure. */
105
+ fun release(): Boolean {
106
+ if (!isActive) return false
107
+ phase = Phase.LOST
108
+ failure = Failure.NONE
109
+ return true
110
+ }
111
+
112
+ /** Return to IDLE so the same instance can be reused for the next call. */
113
+ fun reset() {
114
+ phase = Phase.IDLE
115
+ failure = Failure.NONE
116
+ }
117
+ }