@mentra/bluetooth-sdk 3.3.0 → 3.4.0-beta.1237
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.
- package/MIGRATION.md +61 -0
- package/README.md +80 -11
- package/android/build.gradle +60 -51
- package/android/gradle.properties +3 -0
- package/android/settings.gradle +47 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +85 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +35 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +154 -110
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceStore.kt +21 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +1 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +27 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/connection/ConnectionModels.kt +5 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicy.kt +64 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +925 -155
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +227 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisement.kt +19 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicy.kt +94 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachine.kt +411 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateStore.kt +124 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +54 -59
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +5 -32
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasEncoder.kt +8 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +3 -10
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +50 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +2 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/DeviceManagerOwnerLossTest.kt +146 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusAudioSetupTest.kt +51 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusClassicAudioTest.kt +2 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/WifiAdbPeerIntentTest.kt +19 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicyTest.kt +65 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveAudioSetupReadinessTest.kt +231 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +5 -1
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +368 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisementParserTest.kt +20 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicyTest.kt +136 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachineTest.kt +346 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
- package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
- package/build/BluetoothSdk.types.d.ts +201 -1
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +43 -5
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js +2 -0
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/generated/changelogCatalog.d.ts +3 -0
- package/build/generated/changelogCatalog.d.ts.map +1 -1
- package/build/generated/changelogCatalog.js +4 -0
- package/build/generated/changelogCatalog.js.map +1 -1
- package/build/generated/releaseMetadata.js +6 -6
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +9 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +76 -12
- package/ios/MentraBluetoothSDK.podspec +4 -0
- package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
- package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
- package/ios/Source/BesCrashReportWaiters.swift +64 -0
- package/ios/Source/BluetoothSdkDefaults.swift +2 -2
- package/ios/Source/Bridge.swift +11 -3
- package/ios/Source/DeviceManager.swift +131 -18
- package/ios/Source/DeviceStore.swift +27 -0
- package/ios/Source/GeneratedChangelogCatalog.swift +1 -0
- package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
- package/ios/Source/MentraBluetoothSDK.swift +40 -5
- package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
- package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
- package/ios/Source/audio/SpeechActivity.swift +277 -0
- package/ios/Source/errors/BluetoothSdkError.swift +5 -1
- package/ios/Source/events/BluetoothEvents.swift +41 -0
- package/ios/Source/internal/BleTraceLogger.swift +9 -6
- package/ios/Source/internal/BluetoothAvailability.swift +15 -5
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +186 -0
- package/ios/Source/sgcs/MentraLive.swift +1270 -189
- package/ios/Source/sgcs/MentraLivePairingAdvertisement.swift +260 -1
- package/ios/Source/sgcs/MentraLivePairingStateMachine.swift +385 -0
- package/ios/Source/sgcs/MentraLivePairingStateStore.swift +121 -0
- package/ios/Source/sgcs/Nimo.swift +10 -3
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +39 -9
- package/ios/Source/sgcs/SGCManager.swift +13 -0
- package/ios/Source/sgcs/Simulated.swift +2 -0
- package/ios/Source/status/DeviceStatus.swift +35 -0
- package/ios/Source/stt/STTTools.swift +13 -31
- package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
- package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
- package/ios/Source/stt/WakeWordDetector.swift +145 -0
- package/ios/Source/types/DeviceModels.swift +8 -1
- package/ios/Source/utils/AudioSessionMonitor.swift +158 -5
- package/ios/Source/utils/BoundedWait.swift +44 -0
- package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
- package/ios/Source/utils/SttModelTraits.swift +62 -0
- package/ios/Tests/BesCrashReportTests.swift +142 -0
- package/ios/Tests/BluetoothAudioRouteTests.swift +82 -0
- package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
- package/ios/Tests/DeviceManagerReconnectTests.swift +4 -2
- package/ios/Tests/GlassesAudioSetupPolicyTests.swift +197 -0
- package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
- package/ios/Tests/MentraLiveAudioSetupTests.swift +209 -0
- package/ios/Tests/MentraLiveConnectionOptionsTests.swift +55 -0
- package/ios/Tests/MentraLivePairingAdvertisementTests.swift +327 -0
- package/ios/Tests/MentraLivePairingStateMachineTests.swift +402 -0
- package/ios/Tests/MentraLiveSessionReadinessTests.swift +368 -26
- package/ios/Tests/NimoCanvasTransportTests.swift +58 -0
- package/ios/Tests/PcmStreamTests.swift +299 -0
- package/ios/Tests/SpeechActivityTests.swift +238 -0
- package/ios/Tests/SttModelTraitsTests.swift +62 -0
- package/ios/Tests/WifiAdbDispatchTests.swift +118 -0
- package/package.json +3 -2
- package/plugin/build/withAndroid.js +9 -11
- package/plugin/build/withKotlinVersion.d.ts +2 -0
- package/plugin/build/withKotlinVersion.js +46 -0
- package/src/BluetoothSdk.types.ts +201 -1
- package/src/_private/BluetoothSdkModule.ts +47 -5
- package/src/generated/changelogCatalog.ts +4 -0
- package/src/generated/releaseMetadata.ts +6 -6
- package/src/index.ts +22 -0
- package/android/silero/LICENSE.md +0 -17
- package/android/silero/build.gradle +0 -50
- package/android/silero/proguard-rules.pro +0 -24
- package/android/silero/src/androidTest/assets/hello.wav +0 -0
- package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
- package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
- package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
- package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
- package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -288
- /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
- /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// Process-wide owner of the `MentraLivePairingStateMachine`. It outlives each `MentraLive`
|
|
4
|
+
/// instance so a disconnect or SGC replacement keeps the pause and the rejection, persists the
|
|
5
|
+
/// record natively (never through the replayed settings), and publishes
|
|
6
|
+
/// `GlassesStatus.pairingState` plus `pairing_state_changed`. Mirrors Android
|
|
7
|
+
/// `MentraLivePairingStateStore`.
|
|
8
|
+
@MainActor
|
|
9
|
+
final class MentraLivePairingStateStore {
|
|
10
|
+
static let shared = MentraLivePairingStateStore()
|
|
11
|
+
|
|
12
|
+
/// The persisted record lives under one UserDefaults key; unknown inner keys are ignored.
|
|
13
|
+
static let defaultsKey = "mentra_live_pairing_v4"
|
|
14
|
+
|
|
15
|
+
private let defaults: UserDefaults
|
|
16
|
+
private let publishes: Bool
|
|
17
|
+
private var machine = MentraLivePairingStateMachine()
|
|
18
|
+
private var restored = false
|
|
19
|
+
|
|
20
|
+
/// `publishes == false` keeps tests away from DeviceStore and the bridge.
|
|
21
|
+
init(defaults: UserDefaults = .standard, publishes: Bool = true) {
|
|
22
|
+
self.defaults = defaults
|
|
23
|
+
self.publishes = publishes
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var state: MentraLivePairingState { machine.state }
|
|
27
|
+
var allowsAutomaticReconnect: Bool { machine.allowsAutomaticReconnect }
|
|
28
|
+
var isPaused: Bool { machine.isPaused }
|
|
29
|
+
var isRejected: Bool { machine.state == .rejected }
|
|
30
|
+
var verificationArmed: Bool { machine.verificationArmed }
|
|
31
|
+
/// A window this phone paused for ended and its advertisement has not been read yet.
|
|
32
|
+
var awaitingTakeoverCheck: Bool { machine.awaitingTakeoverCheck }
|
|
33
|
+
|
|
34
|
+
func remainingPauseMs(nowMs: Int = MentraLivePairingStateStore.nowMs()) -> Int {
|
|
35
|
+
machine.remainingPauseMs(nowMs: nowMs)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// Restore once per process. Later calls only republish the status field.
|
|
39
|
+
@discardableResult
|
|
40
|
+
func ensureRestored(savedDeviceId: String, nowMs: Int = MentraLivePairingStateStore.nowMs()) -> MentraLivePairingTransition? {
|
|
41
|
+
if restored {
|
|
42
|
+
publish(emitEvent: false)
|
|
43
|
+
return nil
|
|
44
|
+
}
|
|
45
|
+
restored = true
|
|
46
|
+
var values = defaults.dictionary(forKey: Self.defaultsKey) ?? [:]
|
|
47
|
+
for key in MentraLivePairingStateMachine.legacyKeys {
|
|
48
|
+
if let legacy = defaults.object(forKey: key) { values[key] = legacy }
|
|
49
|
+
}
|
|
50
|
+
let transition = machine.restore(values: values, savedDeviceId: savedDeviceId, nowMs: nowMs)
|
|
51
|
+
for key in MentraLivePairingStateMachine.legacyKeys {
|
|
52
|
+
defaults.removeObject(forKey: key)
|
|
53
|
+
}
|
|
54
|
+
persist()
|
|
55
|
+
publish()
|
|
56
|
+
Bridge.log("PAIRFLOW: pairing_state restore note=\(transition.note) state=\(describe())")
|
|
57
|
+
return transition
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// Run one machine input, persist the record, and publish when the public state changed.
|
|
61
|
+
@discardableResult
|
|
62
|
+
func update(
|
|
63
|
+
_ source: String,
|
|
64
|
+
_ input: (inout MentraLivePairingStateMachine) -> MentraLivePairingTransition
|
|
65
|
+
) -> MentraLivePairingTransition {
|
|
66
|
+
let before = machine.state
|
|
67
|
+
let beforeRecord = machine.persisted() as NSDictionary
|
|
68
|
+
let transition = input(&machine)
|
|
69
|
+
// Duplicate-reporting scans feed every advertisement through here.
|
|
70
|
+
if !beforeRecord.isEqual(to: machine.persisted()) { persist() }
|
|
71
|
+
if machine.state != before { publish() }
|
|
72
|
+
if transition.effect != .none || machine.state != before {
|
|
73
|
+
Bridge.log(
|
|
74
|
+
"PAIRFLOW: pairing_state source=\(source) effect=\(transition.effect) note=\(transition.note) state=\(describe())"
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
return transition
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func describe() -> String {
|
|
81
|
+
switch machine.state {
|
|
82
|
+
case .normal: return "normal"
|
|
83
|
+
case let .pairingMode(windowId, endsAtMs): return "pairing_mode(window=\(windowId), endsAtMs=\(endsAtMs))"
|
|
84
|
+
case .rejected: return "pairing_rejected"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// Drops the in-memory state as a process restart would. Persisted values stay.
|
|
89
|
+
func resetForTest() {
|
|
90
|
+
machine = MentraLivePairingStateMachine()
|
|
91
|
+
restored = false
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
nonisolated static func nowMs() -> Int {
|
|
95
|
+
Int(Date().timeIntervalSince1970 * 1000)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private func persist() {
|
|
99
|
+
var stored = defaults.dictionary(forKey: Self.defaultsKey) ?? [:]
|
|
100
|
+
for key in MentraLivePairingStateMachine.keys {
|
|
101
|
+
stored.removeValue(forKey: key)
|
|
102
|
+
}
|
|
103
|
+
for (key, value) in machine.persisted() {
|
|
104
|
+
stored[key] = value
|
|
105
|
+
}
|
|
106
|
+
if stored.isEmpty {
|
|
107
|
+
defaults.removeObject(forKey: Self.defaultsKey)
|
|
108
|
+
} else {
|
|
109
|
+
defaults.set(stored, forKey: Self.defaultsKey)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private func publish(emitEvent: Bool = true) {
|
|
114
|
+
guard publishes else { return }
|
|
115
|
+
let map = machine.state.toMap()
|
|
116
|
+
DeviceStore.shared.apply("glasses", "pairingState", map)
|
|
117
|
+
if emitEvent {
|
|
118
|
+
Bridge.sendTypedMessage("pairing_state_changed", body: ["state": map])
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -1397,11 +1397,13 @@ class Nimo: NSObject, SGCManager {
|
|
|
1397
1397
|
private func handleRxPacket(_ packet: Data) {
|
|
1398
1398
|
if packet.count >= 10, packet[8] == 7, [1, 3, 4].contains(packet[9]) {
|
|
1399
1399
|
if let response = NimoCanvasCodec.response(packet) {
|
|
1400
|
+
guard (DeviceManager.shared.sgc as? Nimo) === self else { return }
|
|
1400
1401
|
if response.key == 1 { Bridge.log("NIMO: canvas Launch response status=\(response.payload.first.map(Int.init) ?? -1)") }
|
|
1401
1402
|
canvas.response(key: response.key, payload: response.payload)
|
|
1402
1403
|
}
|
|
1403
1404
|
return
|
|
1404
1405
|
}
|
|
1406
|
+
guard (DeviceManager.shared.sgc as? Nimo) === self else { return }
|
|
1405
1407
|
receiveAssembler.cleanup()
|
|
1406
1408
|
for frame in receiveAssembler.ingest(packet) {
|
|
1407
1409
|
guard let decoded = NimoFrameCodec.decode(frame),
|
|
@@ -1590,6 +1592,7 @@ class Nimo: NSObject, SGCManager {
|
|
|
1590
1592
|
// MARK: - Mic Audio
|
|
1591
1593
|
|
|
1592
1594
|
private func handleMicPacket(_ data: Data) {
|
|
1595
|
+
guard (DeviceManager.shared.sgc as? Nimo) === self else { return }
|
|
1593
1596
|
guard let packet = NimoAudioParser.parse(data) else { return }
|
|
1594
1597
|
let now = Date()
|
|
1595
1598
|
switch packet.type {
|
|
@@ -1676,6 +1679,7 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1676
1679
|
"NIMO: Failed to connect \(peripheral.name ?? "unknown"): \(error?.localizedDescription ?? "unknown")"
|
|
1677
1680
|
)
|
|
1678
1681
|
guard self.peripheral === peripheral else { return }
|
|
1682
|
+
self.writes.connectionClosed(peripheral)
|
|
1679
1683
|
self.peripheral = nil
|
|
1680
1684
|
self.startReconnectionTimer()
|
|
1681
1685
|
}
|
|
@@ -1687,6 +1691,7 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1687
1691
|
DispatchQueue.main.async { [weak self] in
|
|
1688
1692
|
guard let self else { return }
|
|
1689
1693
|
Bridge.log("NIMO: Disconnected: \(error?.localizedDescription ?? "clean")")
|
|
1694
|
+
self.writes.connectionClosed(peripheral)
|
|
1690
1695
|
if self.isDisconnecting || self.peripheral !== peripheral { return }
|
|
1691
1696
|
|
|
1692
1697
|
self.peripheral = nil
|
|
@@ -1695,9 +1700,11 @@ extension Nimo: CBCentralManagerDelegate {
|
|
|
1695
1700
|
self.micChar = nil
|
|
1696
1701
|
self.resetSessionState()
|
|
1697
1702
|
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1703
|
+
if (DeviceManager.shared.sgc as? Nimo) === self {
|
|
1704
|
+
DeviceStore.shared.apply("glasses", "connected", false)
|
|
1705
|
+
DeviceStore.shared.apply("glasses", "fullyBooted", false)
|
|
1706
|
+
DeviceStore.shared.apply("glasses", "connectionState", ConnTypes.DISCONNECTED)
|
|
1707
|
+
}
|
|
1701
1708
|
self.startReconnectionTimer()
|
|
1702
1709
|
}
|
|
1703
1710
|
}
|
|
@@ -134,6 +134,11 @@ final class NimoWriteQueue<Characteristic: AnyObject> {
|
|
|
134
134
|
private var generation: UInt64 = 0
|
|
135
135
|
private var queue: [Write] = []
|
|
136
136
|
private var current: Write?
|
|
137
|
+
private var outstanding: [(AnyObject, Characteristic)] = []
|
|
138
|
+
private var scheduledCompletions = 0
|
|
139
|
+
var activeCount: Int { outstanding.count + scheduledCompletions }
|
|
140
|
+
var queuedCount: Int { queue.count }
|
|
141
|
+
func connectionClosed(_ closed: AnyObject) { outstanding.removeAll { $0.0 === closed } }
|
|
137
142
|
private var cancelDeadline: (() -> Void)?
|
|
138
143
|
|
|
139
144
|
init(schedule: @escaping NimoScheduler, write: @escaping (Characteristic, Data) -> Bool, failure: @escaping (String) -> Void) {
|
|
@@ -165,14 +170,18 @@ final class NimoWriteQueue<Characteristic: AnyObject> {
|
|
|
165
170
|
}
|
|
166
171
|
|
|
167
172
|
func written(_ connection: AnyObject, characteristic: Characteristic, success: Bool) {
|
|
173
|
+
if let index = outstanding.firstIndex(where: { $0.0 === connection && $0.1 === characteristic }) { outstanding.remove(at: index) }
|
|
168
174
|
guard connection === self.connection, let item = current,
|
|
169
175
|
characteristic === item.characteristic, !item.callbackReceived else { return }
|
|
170
176
|
item.callbackReceived = true
|
|
171
177
|
cancelDeadline?(); cancelDeadline = nil
|
|
172
178
|
guard success else { fail("GATT write failed"); return }
|
|
173
179
|
let expected = generation
|
|
180
|
+
scheduledCompletions += 1
|
|
174
181
|
_ = schedule(0.005) { [weak self] in
|
|
175
|
-
guard let self
|
|
182
|
+
guard let self else { return }
|
|
183
|
+
self.scheduledCompletions -= 1
|
|
184
|
+
guard self.generation == expected, self.current === item else { return }
|
|
176
185
|
self.current = nil
|
|
177
186
|
item.completed()
|
|
178
187
|
self.drain()
|
|
@@ -185,6 +194,7 @@ final class NimoWriteQueue<Characteristic: AnyObject> {
|
|
|
185
194
|
current = item
|
|
186
195
|
let expected = generation
|
|
187
196
|
guard write(item.characteristic, item.bytes) else { fail("GATT transport unavailable"); return }
|
|
197
|
+
outstanding.append((connection!, item.characteristic))
|
|
188
198
|
item.started()
|
|
189
199
|
cancelDeadline = schedule(3) { [weak self] in
|
|
190
200
|
guard let self, self.generation == expected, self.current === item, !item.callbackReceived else { return }
|
|
@@ -199,26 +209,41 @@ final class NimoWriteQueue<Characteristic: AnyObject> {
|
|
|
199
209
|
|
|
200
210
|
/// Coalesces pending encodes and fences running results on scene change, Exit and disconnect.
|
|
201
211
|
final class NimoCanvasEncoder {
|
|
212
|
+
private final class Work {
|
|
213
|
+
enum Stage { case queued, worker, delivery, settled }
|
|
214
|
+
var stage = Stage.queued
|
|
215
|
+
var cancelled = false
|
|
216
|
+
}
|
|
202
217
|
private let worker: NimoScheduler
|
|
203
218
|
private let main: NimoScheduler
|
|
204
219
|
private let failure: (Error) -> Void
|
|
220
|
+
private let lock = NSLock()
|
|
205
221
|
private var generation: UInt64 = 0
|
|
206
|
-
private var
|
|
222
|
+
private var jobs: [Work] = []
|
|
223
|
+
var workerCount: Int { lock.lock(); defer { lock.unlock() }; return jobs.filter { $0.stage == .queued || $0.stage == .worker }.count }
|
|
224
|
+
var deliveryCount: Int { lock.lock(); defer { lock.unlock() }; return jobs.filter { $0.stage == .delivery }.count }
|
|
207
225
|
|
|
208
226
|
init(main: @escaping NimoScheduler, worker: @escaping NimoScheduler, failure: @escaping (Error) -> Void) {
|
|
209
227
|
self.main = main; self.worker = worker; self.failure = failure
|
|
210
228
|
}
|
|
211
229
|
|
|
212
|
-
/// submit/invalidate and delivery run on Main; the worker only reads its captured request.
|
|
213
230
|
func submit(encode: @escaping () throws -> Data, deliver: @escaping (Data) -> Void) {
|
|
214
231
|
invalidate()
|
|
215
|
-
let expected = generation
|
|
216
|
-
|
|
217
|
-
|
|
232
|
+
let expected = generation, job = Work()
|
|
233
|
+
lock.lock(); jobs.append(job); lock.unlock()
|
|
234
|
+
_ = worker(0) { [weak self] in
|
|
218
235
|
guard let self else { return }
|
|
236
|
+
self.lock.lock()
|
|
237
|
+
guard job.stage == .queued else { self.lock.unlock(); return }
|
|
238
|
+
let shouldEncode = !job.cancelled
|
|
239
|
+
job.stage = .worker; self.lock.unlock()
|
|
240
|
+
let result: Result<Data, Error>? = shouldEncode ? Result { try encode() } : nil
|
|
241
|
+
self.lock.lock(); job.stage = .delivery; self.lock.unlock()
|
|
219
242
|
_ = self.main(0) { [weak self] in
|
|
220
|
-
guard let self
|
|
221
|
-
self.
|
|
243
|
+
guard let self else { return }
|
|
244
|
+
self.lock.lock(); job.stage = .settled; self.jobs.removeAll { $0 === job }; self.lock.unlock()
|
|
245
|
+
guard self.generation == expected else { return }
|
|
246
|
+
guard let result else { return }
|
|
222
247
|
switch result {
|
|
223
248
|
case let .success(bytes): deliver(bytes)
|
|
224
249
|
case let .failure(error): self.failure(error)
|
|
@@ -228,6 +253,11 @@ final class NimoCanvasEncoder {
|
|
|
228
253
|
}
|
|
229
254
|
|
|
230
255
|
func invalidate() {
|
|
231
|
-
generation &+= 1
|
|
256
|
+
generation &+= 1
|
|
257
|
+
lock.lock()
|
|
258
|
+
for job in jobs where job.stage == .queued { job.cancelled = true }
|
|
259
|
+
lock.unlock()
|
|
260
|
+
// Do not erase queued producers: their no-op worker and Main delivery
|
|
261
|
+
// fences must actually execute before teardown can report settlement.
|
|
232
262
|
}
|
|
233
263
|
}
|
|
@@ -50,6 +50,9 @@ protocol SGCManager {
|
|
|
50
50
|
|
|
51
51
|
var type: String { get set }
|
|
52
52
|
var hasMic: Bool { get }
|
|
53
|
+
/// The driver publishes `GlassesStatus.audioSetup` itself. Otherwise the status layer
|
|
54
|
+
/// reports `not_applicable` while connected and `idle` otherwise.
|
|
55
|
+
var publishesAudioSetup: Bool { get }
|
|
53
56
|
|
|
54
57
|
// MARK: - Audio Control
|
|
55
58
|
|
|
@@ -218,6 +221,8 @@ protocol SGCManager {
|
|
|
218
221
|
// MARK: - Incident Reporting
|
|
219
222
|
|
|
220
223
|
func sendIncidentId(_ incidentId: String, apiBaseUrl: String?) throws
|
|
224
|
+
/// Tell the glasses Cloud recorded `reportId` for a BES crash incident (Mentra Live only).
|
|
225
|
+
func sendBesCrashReported(incidentKey: String, reportId: String) throws
|
|
221
226
|
|
|
222
227
|
// MARK: - Gallery
|
|
223
228
|
|
|
@@ -278,8 +283,16 @@ extension SGCManager {
|
|
|
278
283
|
false
|
|
279
284
|
}
|
|
280
285
|
|
|
286
|
+
var publishesAudioSetup: Bool {
|
|
287
|
+
false
|
|
288
|
+
}
|
|
289
|
+
|
|
281
290
|
func replayStreamControlReady() {}
|
|
282
291
|
|
|
292
|
+
func sendBesCrashReported(incidentKey _: String, reportId _: String) throws {
|
|
293
|
+
throw BluetoothSdkError(code: "unsupported_device", message: "BES crash reporting is not supported on \(type).")
|
|
294
|
+
}
|
|
295
|
+
|
|
283
296
|
@discardableResult func requestSavedWifiNetworks(requestId _: String, sid _: String) -> Bool {
|
|
284
297
|
false
|
|
285
298
|
}
|
|
@@ -18,6 +18,8 @@ class Simulated: SGCManager {
|
|
|
18
18
|
BluetoothSdkDefaults.voiceActivityDetectionEnabled
|
|
19
19
|
)
|
|
20
20
|
DeviceStore.shared.apply("glasses", "bluetoothClassicConnected", false)
|
|
21
|
+
DeviceStore.shared.apply("glasses", "bluetoothClassicProfilesConnected", false)
|
|
22
|
+
DeviceStore.shared.apply("glasses", "bluetoothAudioRouted", false)
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
// MARK: - Device Information
|
|
@@ -124,6 +124,7 @@ struct GlassesStatus: CustomStringConvertible {
|
|
|
124
124
|
"signalStrength": -1,
|
|
125
125
|
"signalStrengthUpdatedAt": 0,
|
|
126
126
|
"g2MissingArm": NSNull(),
|
|
127
|
+
"audioSetup": GlassesAudioSetupState.idle.toMap(),
|
|
127
128
|
]))
|
|
128
129
|
}
|
|
129
130
|
|
|
@@ -151,6 +152,14 @@ struct GlassesStatus: CustomStringConvertible {
|
|
|
151
152
|
boolValue(values, "bluetoothClassicConnected") ?? false
|
|
152
153
|
}
|
|
153
154
|
|
|
155
|
+
var bluetoothClassicProfilesConnected: Bool {
|
|
156
|
+
boolValue(values, "bluetoothClassicProfilesConnected") ?? false
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
var bluetoothAudioRouted: Bool {
|
|
160
|
+
boolValue(values, "bluetoothAudioRouted") ?? false
|
|
161
|
+
}
|
|
162
|
+
|
|
154
163
|
var signalStrength: Int {
|
|
155
164
|
intValue(values["signalStrength"]) ?? -1
|
|
156
165
|
}
|
|
@@ -302,6 +311,16 @@ struct GlassesStatus: CustomStringConvertible {
|
|
|
302
311
|
intValue(values["ringSignalStrength"]) ?? -1
|
|
303
312
|
}
|
|
304
313
|
|
|
314
|
+
/// Mentra Live `MentraLivePairingState` (`{state, windowId?, endsAtMs?}`); nil for other glasses.
|
|
315
|
+
var pairingState: [String: Any]? {
|
|
316
|
+
values["pairingState"] as? [String: Any]
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/// `GlassesAudioSetupState` (`{state, action?}`). Every model publishes it.
|
|
320
|
+
var audioSetup: [String: Any]? {
|
|
321
|
+
values["audioSetup"] as? [String: Any]
|
|
322
|
+
}
|
|
323
|
+
|
|
305
324
|
var description: String {
|
|
306
325
|
values.description
|
|
307
326
|
}
|
|
@@ -688,6 +707,14 @@ struct GlassesStatusUpdate: CustomStringConvertible {
|
|
|
688
707
|
optionalBoolValue(values, "bluetoothClassicConnected")
|
|
689
708
|
}
|
|
690
709
|
|
|
710
|
+
var bluetoothClassicProfilesConnected: Bool? {
|
|
711
|
+
optionalBoolValue(values, "bluetoothClassicProfilesConnected")
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
var bluetoothAudioRouted: Bool? {
|
|
715
|
+
optionalBoolValue(values, "bluetoothAudioRouted")
|
|
716
|
+
}
|
|
717
|
+
|
|
691
718
|
var signalStrength: Int? {
|
|
692
719
|
optionalIntValue(values, "signalStrength")
|
|
693
720
|
}
|
|
@@ -848,6 +875,14 @@ struct GlassesStatusUpdate: CustomStringConvertible {
|
|
|
848
875
|
optionalIntValue(values, "ringSignalStrength")
|
|
849
876
|
}
|
|
850
877
|
|
|
878
|
+
var pairingState: [String: Any]? {
|
|
879
|
+
values["pairingState"] as? [String: Any]
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
var audioSetup: [String: Any]? {
|
|
883
|
+
values["audioSetup"] as? [String: Any]
|
|
884
|
+
}
|
|
885
|
+
|
|
851
886
|
var description: String {
|
|
852
887
|
values.description
|
|
853
888
|
}
|
|
@@ -3,46 +3,28 @@ import Foundation
|
|
|
3
3
|
class STTTools {
|
|
4
4
|
// MARK: - SherpaOnnxTranscriber / STT Model Management
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
/// Send a transcript the loaded model produced. `text` is already formatted for that model,
|
|
7
|
+
/// and `language` is the language it was loaded for.
|
|
8
|
+
static func sendTranscription(_ text: String, isFinal: Bool, language: String) {
|
|
9
|
+
guard !text.isEmpty else { return }
|
|
10
|
+
if isFinal {
|
|
11
|
+
Bridge.log("Mentra: Sending final transcription: \(text), \(language)")
|
|
12
|
+
}
|
|
13
|
+
let now = Int(Date().timeIntervalSince1970 * 1000)
|
|
11
14
|
let transcription: [String: Any] = [
|
|
12
15
|
"type": "local_transcription",
|
|
13
|
-
"text":
|
|
14
|
-
"isFinal":
|
|
15
|
-
"startTime":
|
|
16
|
-
"endTime":
|
|
16
|
+
"text": text,
|
|
17
|
+
"isFinal": isFinal,
|
|
18
|
+
"startTime": now - (isFinal ? 2000 : 1000),
|
|
19
|
+
"endTime": now,
|
|
17
20
|
"speakerId": 0,
|
|
18
|
-
"transcribeLanguage":
|
|
21
|
+
"transcribeLanguage": language,
|
|
19
22
|
"provider": "sherpa-onnx",
|
|
20
23
|
]
|
|
21
24
|
|
|
22
25
|
Bridge.sendLocalTranscription(transcription: transcription)
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
static func didReceiveFinalTranscription(_ text: String) {
|
|
26
|
-
// Send final result to server with proper formatting
|
|
27
|
-
let transcriptionLanguage =
|
|
28
|
-
UserDefaults.standard.string(forKey: "STTModelLanguageCode") ?? "en-US"
|
|
29
|
-
Bridge.log("Mentra: Sending final transcription: \(text), \(transcriptionLanguage)")
|
|
30
|
-
if !text.isEmpty {
|
|
31
|
-
let transcription: [String: Any] = [
|
|
32
|
-
"type": "local_transcription",
|
|
33
|
-
"text": transcriptionLanguage == "en-US" ? text.lowercased() : text,
|
|
34
|
-
"isFinal": true,
|
|
35
|
-
"startTime": Int(Date().timeIntervalSince1970 * 1000) - 2000, // 2 seconds ago
|
|
36
|
-
"endTime": Int(Date().timeIntervalSince1970 * 1000),
|
|
37
|
-
"speakerId": 0,
|
|
38
|
-
"transcribeLanguage": transcriptionLanguage,
|
|
39
|
-
"provider": "sherpa-onnx",
|
|
40
|
-
]
|
|
41
|
-
|
|
42
|
-
Bridge.sendLocalTranscription(transcription: transcription)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
28
|
static func setSttModelDetails(_ path: String, _ languageCode: String) {
|
|
47
29
|
UserDefaults.standard.set(path, forKey: "STTModelPath")
|
|
48
30
|
UserDefaults.standard.set(languageCode, forKey: "STTModelLanguageCode")
|