@mentra/bluetooth-sdk 3.3.0-dev.944 → 3.4.0-dev.1003
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 +78 -0
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +274 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +13 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +8 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +79 -8
- 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 +22 -4
- package/android/src/main/java/com/mentra/bluetoothsdk/TouchGesture.kt +19 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/connection/ConnectionModels.kt +5 -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 +832 -134
- 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 +337 -39
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +8 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasEncoder.kt +8 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +1 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +50 -0
- package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoResourceDiagnostics.kt +16 -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/TouchGestureCommandTest.kt +112 -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/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/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +564 -1
- package/build/BluetoothSdk.types.d.ts +110 -2
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +6 -2
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js +1 -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 +4 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +24 -4
- package/ios/Source/BluetoothSdkDefaults.swift +1 -1
- package/ios/Source/Bridge.swift +11 -3
- package/ios/Source/DeviceManager.swift +103 -7
- 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 +41 -7
- package/ios/Source/TouchGesture.swift +13 -0
- package/ios/Source/errors/BluetoothSdkError.swift +5 -1
- package/ios/Source/events/BluetoothEvents.swift +4 -0
- package/ios/Source/internal/BleTraceLogger.swift +9 -6
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +181 -0
- package/ios/Source/sgcs/MentraLive.swift +1196 -173
- 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 +278 -38
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +77 -18
- package/ios/Source/sgcs/NimoCanvasSession.swift +1 -0
- package/ios/Source/sgcs/NimoMacResourceTransport.swift +217 -0
- package/ios/Source/sgcs/NimoResourceFixture.swift +384 -0
- package/ios/Source/sgcs/NimoResourceSession.swift +406 -0
- package/ios/Source/sgcs/SGCManager.swift +7 -0
- package/ios/Source/sgcs/Simulated.swift +2 -0
- package/ios/Source/status/DeviceStatus.swift +35 -0
- package/ios/Source/types/DeviceModels.swift +8 -1
- package/ios/Source/utils/AudioSessionMonitor.swift +158 -5
- package/ios/Tests/BluetoothAudioRouteTests.swift +82 -0
- package/ios/Tests/DeviceManagerReconnectTests.swift +75 -2
- package/ios/Tests/GlassesAudioSetupPolicyTests.swift +191 -0
- package/ios/Tests/MentraLiveAudioSetupTests.swift +215 -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 +328 -26
- package/ios/Tests/NimoCanvasTransportTests.swift +147 -0
- package/ios/Tests/NimoResourceSessionTests.swift +476 -0
- package/ios/Tests/TouchGestureCommandTests.swift +87 -0
- package/ios/Tests/WifiAdbDispatchTests.swift +118 -0
- package/package.json +3 -2
- package/src/BluetoothSdk.types.ts +115 -1
- package/src/_private/BluetoothSdkModule.ts +7 -1
- package/src/generated/changelogCatalog.ts +4 -0
- package/src/generated/releaseMetadata.ts +6 -6
- package/src/index.ts +12 -0
|
@@ -728,28 +728,30 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
728
728
|
guard let self else { return }
|
|
729
729
|
switch state {
|
|
730
730
|
case .poweredOn:
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
731
|
+
self.pairFlow("central", "state=powered_on")
|
|
732
|
+
if self.pairingStore.isPaused {
|
|
733
|
+
self.startPairingWatchScan(reason: "powered_on")
|
|
734
|
+
} else if !self.automaticConnectBlocked(),
|
|
735
|
+
let savedDeviceName = UserDefaults.standard.string(forKey: PREFS_DEVICE_NAME),
|
|
736
|
+
!savedDeviceName.isEmpty
|
|
735
737
|
{
|
|
736
|
-
self.
|
|
738
|
+
self.attachKnownOrScan(savedDeviceName)
|
|
737
739
|
}
|
|
738
740
|
|
|
739
741
|
case .poweredOff:
|
|
740
|
-
|
|
742
|
+
self.pairFlow("central", "state=powered_off")
|
|
741
743
|
self.updateConnectionState(ConnTypes.DISCONNECTED)
|
|
742
744
|
|
|
743
745
|
case .unauthorized:
|
|
744
|
-
|
|
746
|
+
self.pairFlow("central", "state=unauthorized")
|
|
745
747
|
self.updateConnectionState(ConnTypes.DISCONNECTED)
|
|
746
748
|
|
|
747
749
|
case .unsupported:
|
|
748
|
-
|
|
750
|
+
self.pairFlow("central", "state=unsupported")
|
|
749
751
|
self.updateConnectionState(ConnTypes.DISCONNECTED)
|
|
750
752
|
|
|
751
753
|
default:
|
|
752
|
-
|
|
754
|
+
self.pairFlow("central", "state=\(state.rawValue)")
|
|
753
755
|
}
|
|
754
756
|
}
|
|
755
757
|
}
|
|
@@ -759,7 +761,14 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
759
761
|
advertisementData: [String: Any] = [:],
|
|
760
762
|
rssi: NSNumber? = nil
|
|
761
763
|
) {
|
|
762
|
-
|
|
764
|
+
// The first iOS advertisement often has the name only in the packet, not on
|
|
765
|
+
// CBPeripheral. Dropping that packet hides glasses that are already in pairing mode.
|
|
766
|
+
guard let name = discoveredAdvertisementName(peripheral, advertisementData: advertisementData) else {
|
|
767
|
+
if manualDiscoveryActive, loggedUnnamedDiscoveries.insert(peripheral.identifier).inserted {
|
|
768
|
+
pairFlow("discover", "unnamed id=\(peripheral.identifier.uuidString)")
|
|
769
|
+
}
|
|
770
|
+
return
|
|
771
|
+
}
|
|
763
772
|
|
|
764
773
|
// Check for compatible device names
|
|
765
774
|
if name == "Xy_A" || name.hasPrefix("XyBLE_") || name.hasPrefix("MENTRA_LIVE_BLE")
|
|
@@ -767,60 +776,112 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
767
776
|
{
|
|
768
777
|
let savedDeviceName = UserDefaults.standard.string(forKey: PREFS_DEVICE_NAME)
|
|
769
778
|
let isReconnectTarget = savedDeviceName != nil && savedDeviceName == name
|
|
779
|
+
let discoveredAtMs = Self.monotonicNowMs()
|
|
780
|
+
if advertisesPairingFlag(advertisementData) {
|
|
781
|
+
Self.secureTrailerMemory.recordSecureTrailer(peripheral.identifier, nowMs: discoveredAtMs)
|
|
782
|
+
}
|
|
770
783
|
if !isReconnectTarget && advertisesPairingFlag(advertisementData)
|
|
771
784
|
&& !isPairingDiscoverable(advertisementData)
|
|
772
785
|
{
|
|
773
786
|
// Keep nearby secure units visible with pairing-mode guidance, while RN blocks
|
|
774
787
|
// the connection until a pairable advertisement arrives.
|
|
775
|
-
|
|
776
|
-
"
|
|
788
|
+
pairFlow(
|
|
789
|
+
"discover",
|
|
790
|
+
"name=\(name) id=\(peripheral.identifier.uuidString) saved=\(savedDeviceName ?? "-") pairingMode=false secure=true connect=false"
|
|
777
791
|
)
|
|
778
792
|
discoveredPeripherals[name] = peripheral
|
|
779
|
-
|
|
793
|
+
rememberPeripheral(peripheral, name: name)
|
|
794
|
+
if cacheAdvPairing(
|
|
780
795
|
name,
|
|
781
796
|
pairingMode: false,
|
|
782
797
|
pairingCode: nil,
|
|
783
798
|
securePairingCapable: true
|
|
784
|
-
)
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
799
|
+
) {
|
|
800
|
+
emitDiscoveredDeviceFromCache(
|
|
801
|
+
name,
|
|
802
|
+
identifier: peripheral.identifier.uuidString,
|
|
803
|
+
rssi: rssi?.intValue
|
|
804
|
+
)
|
|
805
|
+
}
|
|
790
806
|
return
|
|
791
807
|
}
|
|
792
808
|
|
|
793
809
|
let glassType = name == "Xy_A" ? "Standard" : "K900"
|
|
794
810
|
let trailer = parseSecurePairingTrailer(advertisementData)
|
|
811
|
+
// A packet without the trailer from a peripheral that just showed it is a partial
|
|
812
|
+
// advertisement, not legacy firmware. Do not publish it: the scan list would show a
|
|
813
|
+
// secure unit that is not in pairing mode as pairable, with no caption.
|
|
814
|
+
let partialAdvertisement = Self.secureTrailerMemory.isPartialAdvertisement(
|
|
815
|
+
peripheral.identifier,
|
|
816
|
+
secureCapable: trailer.secureCapable,
|
|
817
|
+
nowMs: discoveredAtMs
|
|
818
|
+
)
|
|
795
819
|
Bridge.log(
|
|
796
820
|
trailer.secureCapable
|
|
797
821
|
? "Found compatible \(glassType) glasses device: \(name) (pairingMode=\(trailer.pairingMode))"
|
|
798
|
-
:
|
|
822
|
+
: partialAdvertisement
|
|
823
|
+
? "Found compatible \(glassType) glasses device: \(name) (partial advertisement, secure trailer seen recently)"
|
|
824
|
+
: "Found compatible \(glassType) glasses device: \(name) (legacy firmware, pairable)"
|
|
799
825
|
)
|
|
800
826
|
|
|
801
827
|
// Store the peripheral
|
|
802
828
|
discoveredPeripherals[name] = peripheral
|
|
829
|
+
rememberPeripheral(peripheral, name: name)
|
|
803
830
|
|
|
804
|
-
cacheAdvPairing(
|
|
831
|
+
if !partialAdvertisement, cacheAdvPairing(
|
|
805
832
|
name,
|
|
806
833
|
// Field firmware has no pairing flag. Treat it as pairable so the
|
|
807
834
|
// new Mentra App can still find old glasses.
|
|
808
835
|
pairingMode: trailer.secureCapable ? trailer.pairingMode : true,
|
|
809
836
|
pairingCode: trailer.pairingCode,
|
|
810
837
|
securePairingCapable: trailer.secureCapable
|
|
838
|
+
) {
|
|
839
|
+
emitDiscoveredDeviceFromCache(
|
|
840
|
+
name,
|
|
841
|
+
identifier: peripheral.identifier.uuidString,
|
|
842
|
+
rssi: rssi?.intValue
|
|
843
|
+
)
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
let isSavedGlasses = savedDeviceName == name
|
|
847
|
+
if isSavedGlasses, trailer.secureCapable {
|
|
848
|
+
observeSavedGlassesAdvertisement(trailer)
|
|
849
|
+
}
|
|
850
|
+
if isSavedGlasses, automaticConnectBlocked() {
|
|
851
|
+
// Paused for a pairing window or rejected: observe only. The watch scan keeps
|
|
852
|
+
// reading the advertisement so a cleared pairing flag can end the pause.
|
|
853
|
+
if !pairingWatchScanActive {
|
|
854
|
+
pairFlow(
|
|
855
|
+
"discover",
|
|
856
|
+
"name=\(name) id=\(peripheral.identifier.uuidString) action=wait state=\(pairingStore.describe())"
|
|
857
|
+
)
|
|
858
|
+
}
|
|
859
|
+
return
|
|
860
|
+
}
|
|
861
|
+
let waitForWindow = MentraLiveConnectionAttemptPolicy.shouldStandDownInsteadOfReconnect(
|
|
862
|
+
pairingMode: trailer.secureCapable && trailer.pairingMode,
|
|
863
|
+
savedGlasses: isSavedGlasses,
|
|
864
|
+
explicitPairing: isExplicitPairingAttempt()
|
|
811
865
|
)
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
866
|
+
if waitForWindow {
|
|
867
|
+
// The saved glasses are in a pairing window this phone never heard about.
|
|
868
|
+
// Connecting would race the new phone for the single BLE slot; keep watching
|
|
869
|
+
// and connect once the flag clears.
|
|
870
|
+
if !pairingWatchScanActive {
|
|
871
|
+
pairFlow(
|
|
872
|
+
"discover",
|
|
873
|
+
"name=\(name) id=\(peripheral.identifier.uuidString) action=wait_pairing_mode"
|
|
874
|
+
)
|
|
875
|
+
}
|
|
876
|
+
startPairingWatchScan(reason: "saved_glasses_pairing_mode")
|
|
877
|
+
return
|
|
878
|
+
}
|
|
879
|
+
let willConnect = isSavedGlasses
|
|
880
|
+
pairFlow(
|
|
881
|
+
"discover",
|
|
882
|
+
"name=\(name) id=\(peripheral.identifier.uuidString) saved=\(savedDeviceName ?? "-") pairingMode=\(trailer.pairingMode) secure=\(trailer.secureCapable) connect=\(willConnect)"
|
|
816
883
|
)
|
|
817
|
-
|
|
818
|
-
// Check if this is the device we want to connect to
|
|
819
|
-
if let savedDeviceName = UserDefaults.standard.string(forKey: PREFS_DEVICE_NAME),
|
|
820
|
-
savedDeviceName == name
|
|
821
|
-
{
|
|
822
|
-
Bridge.log("Found our remembered device by name, connecting: \(name)")
|
|
823
|
-
// stopScan()
|
|
884
|
+
if willConnect {
|
|
824
885
|
centralManager?.stopScan()
|
|
825
886
|
isScanning = false
|
|
826
887
|
connectToDevice(peripheral)
|
|
@@ -842,7 +903,7 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
842
903
|
guard let self else { return }
|
|
843
904
|
let matchesActiveAttempt = self.connectingPeripheral === peripheral
|
|
844
905
|
guard MentraLiveConnectionAttemptPolicy.shouldAcceptDidConnect(
|
|
845
|
-
pairingYieldActive: self.
|
|
906
|
+
pairingYieldActive: self.automaticConnectBlocked(),
|
|
846
907
|
matchesActiveAttempt: matchesActiveAttempt
|
|
847
908
|
) else {
|
|
848
909
|
Bridge.log(
|
|
@@ -859,7 +920,11 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
859
920
|
self.centralManager?.cancelPeripheralConnection(peripheral)
|
|
860
921
|
return
|
|
861
922
|
}
|
|
862
|
-
|
|
923
|
+
self.pairFlow(
|
|
924
|
+
"gatt_connect",
|
|
925
|
+
"id=\(peripheral.identifier.uuidString) name=\(peripheral.name ?? "-")"
|
|
926
|
+
)
|
|
927
|
+
Self.clearStaleIosBondPrompt()
|
|
863
928
|
|
|
864
929
|
self.stopConnectionTimeout()
|
|
865
930
|
self.clearPendingReconnectTracking()
|
|
@@ -913,11 +978,19 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
913
978
|
let isCurrent =
|
|
914
979
|
self.connectedPeripheral === peripheral || self.connectingPeripheral === peripheral
|
|
915
980
|
if !isCurrent {
|
|
916
|
-
|
|
981
|
+
self.pairFlow("gatt_disconnect", "ignored id=\(peripheral.identifier.uuidString)")
|
|
917
982
|
return
|
|
918
983
|
}
|
|
919
984
|
self.connectingPeripheral = nil
|
|
920
985
|
self.clearPendingReconnectTracking()
|
|
986
|
+
let nsError = error as NSError?
|
|
987
|
+
self.pairFlow(
|
|
988
|
+
"gatt_disconnect",
|
|
989
|
+
"id=\(peripheral.identifier.uuidString) name=\(peripheral.name ?? "-") domain=\(nsError?.domain ?? "-") code=\(nsError?.code ?? 0)"
|
|
990
|
+
)
|
|
991
|
+
Bridge.log(
|
|
992
|
+
"PAIRFLOW: ble_disconnect id=\(peripheral.identifier.uuidString) domain=\(nsError?.domain ?? "none") code=\(nsError?.code ?? 0) killed=\(self.isKilled) explicitPairing=\(self.isExplicitPairingAttempt())"
|
|
993
|
+
)
|
|
921
994
|
Bridge.log("LIVE: Disconnected from GATT server")
|
|
922
995
|
|
|
923
996
|
self.isConnecting = false
|
|
@@ -941,9 +1014,18 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
941
1014
|
// Clean up characteristics
|
|
942
1015
|
self.txCharacteristic = nil
|
|
943
1016
|
self.rxCharacteristic = nil
|
|
1017
|
+
let waitingForOwnerCommit = self.awaitingSecurePairingConfirmation
|
|
1018
|
+
self.resetSecurePairingSession()
|
|
944
1019
|
|
|
945
|
-
if self.
|
|
946
|
-
|
|
1020
|
+
if self.stopAfterConnectFailure(error) {
|
|
1021
|
+
return
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// The system pairing sheet was dismissed, or the bond never finished.
|
|
1025
|
+
// Reconnecting would leave the loading screen up with no failure.
|
|
1026
|
+
if waitingForOwnerCommit && self.isExplicitPairingAttempt() {
|
|
1027
|
+
Bridge.log("LIVE: secure pairing link dropped before owner commit")
|
|
1028
|
+
Bridge.sendPairFailureEvent("errors:pairingCouldNotStart")
|
|
947
1029
|
return
|
|
948
1030
|
}
|
|
949
1031
|
|
|
@@ -962,11 +1044,15 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
962
1044
|
let isCurrent =
|
|
963
1045
|
self.connectedPeripheral === peripheral || self.connectingPeripheral === peripheral
|
|
964
1046
|
if !isCurrent {
|
|
965
|
-
|
|
1047
|
+
self.pairFlow("gatt_connect_fail", "ignored id=\(peripheral.identifier.uuidString)")
|
|
966
1048
|
return
|
|
967
1049
|
}
|
|
968
1050
|
self.connectingPeripheral = nil
|
|
969
|
-
|
|
1051
|
+
let nsError = error as NSError?
|
|
1052
|
+
self.pairFlow(
|
|
1053
|
+
"gatt_connect_fail",
|
|
1054
|
+
"id=\(peripheral.identifier.uuidString) name=\(peripheral.name ?? "-") domain=\(nsError?.domain ?? "-") code=\(nsError?.code ?? 0) \(errorDescription)"
|
|
1055
|
+
)
|
|
970
1056
|
|
|
971
1057
|
self.stopConnectionTimeout()
|
|
972
1058
|
self.clearPendingReconnectTracking()
|
|
@@ -975,8 +1061,7 @@ extension MentraLive: CBCentralManagerDelegate {
|
|
|
975
1061
|
self.connectedPeripheral = nil
|
|
976
1062
|
self.updateConnectionState(ConnTypes.DISCONNECTED)
|
|
977
1063
|
|
|
978
|
-
if self.
|
|
979
|
-
self.clearSavedGlassesAfterOwnerLoss(reason: "ios_auth_fail")
|
|
1064
|
+
if self.stopAfterConnectFailure(error) {
|
|
980
1065
|
return
|
|
981
1066
|
}
|
|
982
1067
|
|
|
@@ -1078,7 +1163,10 @@ extension MentraLive: CBPeripheralDelegate {
|
|
|
1078
1163
|
|
|
1079
1164
|
// Check if we have both characteristics
|
|
1080
1165
|
if self.txCharacteristic != nil, let rx = self.rxCharacteristic {
|
|
1081
|
-
|
|
1166
|
+
self.pairFlow(
|
|
1167
|
+
"gatt_ready",
|
|
1168
|
+
"id=\(peripheral.identifier.uuidString) name=\(peripheral.name ?? "-") mtu=\(max(23, min(peripheral.maximumWriteValueLength(for: .withResponse) + 3, peripheral.maximumWriteValueLength(for: .withoutResponse) + 3)))"
|
|
1169
|
+
)
|
|
1082
1170
|
Bridge.log("LIVE: 🔄 Waiting for glasses SOC to become ready...")
|
|
1083
1171
|
|
|
1084
1172
|
// Don't set connected=true here - wait for SOC to be ready (fullyBooted=true)
|
|
@@ -1152,13 +1240,14 @@ extension MentraLive: CBPeripheralDelegate {
|
|
|
1152
1240
|
}
|
|
1153
1241
|
|
|
1154
1242
|
nonisolated func peripheral(
|
|
1155
|
-
_: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?
|
|
1243
|
+
_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?
|
|
1156
1244
|
) {
|
|
1245
|
+
guard let token = notificationIngress.capture(peripheral) else { return }
|
|
1157
1246
|
let uuid = characteristic.uuid
|
|
1158
1247
|
let data = characteristic.value
|
|
1159
1248
|
let errorDescription = error?.localizedDescription
|
|
1160
1249
|
DispatchQueue.main.async { [weak self] in
|
|
1161
|
-
guard let self else { return }
|
|
1250
|
+
guard let self, self.connectedPeripheral === peripheral, self.notificationIngress.current(token) else { return }
|
|
1162
1251
|
// Bridge.log("LIVE: DEBUG: didUpdateValueFor CALLED - characteristic: \(uuid), dataSize: \(data?.count ?? 0)")
|
|
1163
1252
|
// Log raw hex for debugging glasses_ready issue
|
|
1164
1253
|
if let data {
|
|
@@ -1195,7 +1284,7 @@ extension MentraLive: CBPeripheralDelegate {
|
|
|
1195
1284
|
}
|
|
1196
1285
|
|
|
1197
1286
|
// Handle regular data (JSON messages, file transfers, etc.)
|
|
1198
|
-
self.
|
|
1287
|
+
self.receiveBleNotification(data, token: token)
|
|
1199
1288
|
}
|
|
1200
1289
|
}
|
|
1201
1290
|
|
|
@@ -1221,16 +1310,25 @@ extension MentraLive: CBPeripheralDelegate {
|
|
|
1221
1310
|
let uuid = characteristic.uuid
|
|
1222
1311
|
let isNotifying = characteristic.isNotifying
|
|
1223
1312
|
let errorDescription = error?.localizedDescription
|
|
1313
|
+
let notifyError = error
|
|
1224
1314
|
DispatchQueue.main.async { [weak self] in
|
|
1225
1315
|
guard let self else { return }
|
|
1226
1316
|
if let errorDescription {
|
|
1227
1317
|
Bridge.log("LIVE: Error updating notification state: \(errorDescription)")
|
|
1318
|
+
// Insufficient authentication/encryption on the RX subscribe is the glasses
|
|
1319
|
+
// refusing this phone's keys.
|
|
1320
|
+
if uuid == self.RX_CHAR_UUID, Self.isPairingAuthFailure(notifyError),
|
|
1321
|
+
self.recordAuthRejection(reason: "ios_notify_auth_\((notifyError as NSError?)?.code ?? 0)")
|
|
1322
|
+
{
|
|
1323
|
+
return
|
|
1324
|
+
}
|
|
1228
1325
|
} else {
|
|
1229
1326
|
Bridge.log("Notification state updated for \(uuid): \(isNotifying ? "ON" : "OFF")")
|
|
1230
1327
|
|
|
1231
1328
|
if uuid == self.RX_CHAR_UUID, isNotifying {
|
|
1232
1329
|
Bridge.log("LIVE: 🔔 Ready to receive data via notifications")
|
|
1233
1330
|
self.enableAncsRelayIfAuthorized()
|
|
1331
|
+
self.sendPairingSessionMessages(reason: "gatt_ready")
|
|
1234
1332
|
}
|
|
1235
1333
|
}
|
|
1236
1334
|
}
|
|
@@ -1263,12 +1361,25 @@ enum MentraLiveConnectionState {
|
|
|
1263
1361
|
}
|
|
1264
1362
|
|
|
1265
1363
|
enum MentraLiveConnectionOptions {
|
|
1266
|
-
|
|
1364
|
+
/// `enableTransportBridging` asks iOS to bring up the Classic profiles (A2DP/HFP)
|
|
1365
|
+
/// after the BLE link is up (CBConnectPeripheralOptionEnableTransportBridgingKey,
|
|
1366
|
+
/// iOS 13+). The product default is on; this helper still omits the key unless
|
|
1367
|
+
/// the caller passes true.
|
|
1368
|
+
static func coreBluetoothOptions(
|
|
1369
|
+
requiresAncs: Bool,
|
|
1370
|
+
enableTransportBridging: Bool = false
|
|
1371
|
+
) -> [String: Any]? {
|
|
1267
1372
|
#if os(macOS)
|
|
1268
1373
|
return nil
|
|
1269
1374
|
#else
|
|
1270
|
-
|
|
1271
|
-
|
|
1375
|
+
var options = [String: Any]()
|
|
1376
|
+
if requiresAncs {
|
|
1377
|
+
options[CBConnectPeripheralOptionRequiresANCS] = true
|
|
1378
|
+
}
|
|
1379
|
+
if enableTransportBridging {
|
|
1380
|
+
options[CBConnectPeripheralOptionEnableTransportBridgingKey] = true
|
|
1381
|
+
}
|
|
1382
|
+
return options.isEmpty ? nil : options
|
|
1272
1383
|
#endif
|
|
1273
1384
|
}
|
|
1274
1385
|
}
|
|
@@ -1278,6 +1389,29 @@ typealias JSONObject = [String: Any]
|
|
|
1278
1389
|
|
|
1279
1390
|
// MARK: - Main Manager Class
|
|
1280
1391
|
|
|
1392
|
+
/// Captures CoreBluetooth callback provenance before its MainActor hop.
|
|
1393
|
+
final class MentraLiveNotificationIngress: @unchecked Sendable {
|
|
1394
|
+
struct Token: Equatable, Sendable { let source: ObjectIdentifier; let epoch: UInt64 }
|
|
1395
|
+
private let lock = NSLock()
|
|
1396
|
+
private var epoch: UInt64 = 0
|
|
1397
|
+
private var source: ObjectIdentifier?
|
|
1398
|
+
func begin(_ peer: AnyObject?) -> Token? {
|
|
1399
|
+
lock.lock(); defer { lock.unlock() }
|
|
1400
|
+
epoch &+= 1
|
|
1401
|
+
source = peer.map(ObjectIdentifier.init)
|
|
1402
|
+
return source.map { Token(source: $0, epoch: epoch) }
|
|
1403
|
+
}
|
|
1404
|
+
func capture(_ peer: AnyObject) -> Token? {
|
|
1405
|
+
lock.lock(); defer { lock.unlock() }
|
|
1406
|
+
guard source == ObjectIdentifier(peer) else { return nil }
|
|
1407
|
+
return Token(source: ObjectIdentifier(peer), epoch: epoch)
|
|
1408
|
+
}
|
|
1409
|
+
func current(_ token: Token) -> Bool {
|
|
1410
|
+
lock.lock(); defer { lock.unlock() }
|
|
1411
|
+
return source == token.source && epoch == token.epoch
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1281
1415
|
@MainActor
|
|
1282
1416
|
class MentraLive: NSObject, SGCManager {
|
|
1283
1417
|
// Feature Flags
|
|
@@ -1304,16 +1438,23 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1304
1438
|
let pairingMode: Bool
|
|
1305
1439
|
let pairingCode: String?
|
|
1306
1440
|
let secureCapable: Bool
|
|
1441
|
+
let pairingGeneration: Int?
|
|
1442
|
+
let ownerChanged: Bool
|
|
1307
1443
|
}
|
|
1308
1444
|
|
|
1309
1445
|
private func parseSecurePairingTrailer(_ advertisementData: [String: Any]) -> SecurePairingTrailer {
|
|
1310
1446
|
guard let advertisement = pairingAdvertisement(advertisementData) else {
|
|
1311
|
-
return SecurePairingTrailer(
|
|
1447
|
+
return SecurePairingTrailer(
|
|
1448
|
+
pairingMode: false, pairingCode: nil, secureCapable: false, pairingGeneration: nil,
|
|
1449
|
+
ownerChanged: false
|
|
1450
|
+
)
|
|
1312
1451
|
}
|
|
1313
1452
|
return SecurePairingTrailer(
|
|
1314
1453
|
pairingMode: advertisement.pairingMode,
|
|
1315
1454
|
pairingCode: advertisement.pairingCode,
|
|
1316
|
-
secureCapable: true
|
|
1455
|
+
secureCapable: true,
|
|
1456
|
+
pairingGeneration: advertisement.pairingGeneration,
|
|
1457
|
+
ownerChanged: advertisement.ownerChanged
|
|
1317
1458
|
)
|
|
1318
1459
|
}
|
|
1319
1460
|
|
|
@@ -1321,11 +1462,25 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1321
1462
|
pairingAdvertisement(advertisementData) != nil
|
|
1322
1463
|
}
|
|
1323
1464
|
|
|
1465
|
+
private nonisolated let notificationIngress = MentraLiveNotificationIngress()
|
|
1466
|
+
private var receivingNotification: MentraLiveNotificationIngress.Token?
|
|
1467
|
+
private var wifiAdbPeerIdentity: (mac: String, token: MentraLiveNotificationIngress.Token)?
|
|
1468
|
+
private final class WifiAdbAdmission {
|
|
1469
|
+
let identity: (mac: String, token: MentraLiveNotificationIngress.Token)
|
|
1470
|
+
let wireEpoch: Int
|
|
1471
|
+
var queued = 0
|
|
1472
|
+
var refused = false
|
|
1473
|
+
init(identity: (mac: String, token: MentraLiveNotificationIngress.Token), wireEpoch: Int) {
|
|
1474
|
+
self.identity = identity; self.wireEpoch = wireEpoch
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
private var wifiAdbAdmission: WifiAdbAdmission?
|
|
1324
1478
|
var connectionState: String = ConnTypes.DISCONNECTED
|
|
1325
1479
|
|
|
1326
1480
|
/// Mirrors Android `updateConnectionState` — RN home reads `glasses.connectionState` for reconnecting UI.
|
|
1327
1481
|
private func updateConnectionState(_ state: String) {
|
|
1328
1482
|
if state == ConnTypes.DISCONNECTED {
|
|
1483
|
+
endBleNotificationSession()
|
|
1329
1484
|
// Device identity is session-bound. Clear it on disconnect so a previous pair's
|
|
1330
1485
|
// identifiers cannot be associated with the next connection. Connect must NOT clear
|
|
1331
1486
|
// them: DeviceManager.disconnect already wipes them before any new connection, and
|
|
@@ -1338,6 +1493,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1338
1493
|
DeviceStore.shared.apply("glasses", "hotspotOtaVersion", 0)
|
|
1339
1494
|
}
|
|
1340
1495
|
connectionState = state
|
|
1496
|
+
pairFlow(
|
|
1497
|
+
"conn_state",
|
|
1498
|
+
"state=\(state) gatt=\(txCharacteristic != nil) connecting=\(isConnecting) killed=\(isKilled) scanning=\(isScanning) id=\(connectedPeripheral?.identifier.uuidString ?? connectingPeripheral?.identifier.uuidString ?? "-") name=\(connectedPeripheral?.name ?? connectingPeripheral?.name ?? "-")"
|
|
1499
|
+
)
|
|
1341
1500
|
DeviceStore.shared.apply("glasses", "connectionState", state)
|
|
1342
1501
|
// Drop OTA caches when fully disconnected — avoids leaking session/step state from
|
|
1343
1502
|
// a previous pairing into the next one (would otherwise surface as wrong overall_percent
|
|
@@ -1354,6 +1513,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1354
1513
|
DeviceStore.shared.apply("glasses", "signalStrength", -1)
|
|
1355
1514
|
DeviceStore.shared.apply("glasses", "signalStrengthUpdatedAt", 0)
|
|
1356
1515
|
resetOtaCache()
|
|
1516
|
+
endAudioSetupSession()
|
|
1357
1517
|
}
|
|
1358
1518
|
}
|
|
1359
1519
|
|
|
@@ -1388,9 +1548,12 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1388
1548
|
|
|
1389
1549
|
func forget() {
|
|
1390
1550
|
Bridge.log("LIVE: Forgetting Mentra Live glasses")
|
|
1551
|
+
Self.secureTrailerMemory.clear()
|
|
1391
1552
|
|
|
1392
1553
|
// Clear saved device name so the pairing filter is not bypassed on rescan
|
|
1393
1554
|
UserDefaults.standard.removeObject(forKey: PREFS_DEVICE_NAME)
|
|
1555
|
+
UserDefaults.standard.removeObject(forKey: PREFS_PERIPHERAL_IDS)
|
|
1556
|
+
pairFlow("forget", "cleared_saved_peripheral")
|
|
1394
1557
|
|
|
1395
1558
|
// Stop scanning first
|
|
1396
1559
|
if isScanning {
|
|
@@ -1413,6 +1576,9 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1413
1576
|
|
|
1414
1577
|
var type = "Mentra Live"
|
|
1415
1578
|
var hasMic = true
|
|
1579
|
+
var publishesAudioSetup: Bool {
|
|
1580
|
+
true
|
|
1581
|
+
}
|
|
1416
1582
|
|
|
1417
1583
|
func setMicEnabled(_ enabled: Bool) {
|
|
1418
1584
|
Bridge.log("LIVE: 🎤 Microphone state change requested: \(enabled)")
|
|
@@ -1520,6 +1686,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1520
1686
|
|
|
1521
1687
|
/// Device Settings Keys
|
|
1522
1688
|
private let PREFS_DEVICE_NAME = "MentraLiveLastConnectedDeviceName"
|
|
1689
|
+
private let PREFS_PERIPHERAL_IDS = "MentraLivePeripheralIds"
|
|
1523
1690
|
|
|
1524
1691
|
// MARK: - Properties
|
|
1525
1692
|
|
|
@@ -1530,7 +1697,9 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1530
1697
|
/// BLE Properties
|
|
1531
1698
|
private var centralManager: CBCentralManager?
|
|
1532
1699
|
|
|
1533
|
-
private var connectedPeripheral: CBPeripheral?
|
|
1700
|
+
private var connectedPeripheral: CBPeripheral? {
|
|
1701
|
+
didSet { _ = beginBleNotificationSession(source: connectedPeripheral) }
|
|
1702
|
+
}
|
|
1534
1703
|
/// Peripheral for the in-flight connect; used to ignore stale disconnect/fail callbacks.
|
|
1535
1704
|
private var connectingPeripheral: CBPeripheral?
|
|
1536
1705
|
private var txCharacteristic: CBCharacteristic?
|
|
@@ -1543,10 +1712,49 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1543
1712
|
private var manualDiscoveryActive = false
|
|
1544
1713
|
private var isConnecting = false
|
|
1545
1714
|
private var isKilled = false
|
|
1546
|
-
///
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1715
|
+
/// Pairing protocol this phone speaks (`phone_hello.pairing_protocol`). The glasses send
|
|
1716
|
+
/// window notices only to phones that announced protocol 4 or later.
|
|
1717
|
+
static let pairingProtocolVersion = 4
|
|
1718
|
+
/// Pause, takeover and rejection state for the saved glasses. Outlives this manager.
|
|
1719
|
+
private let pairingStore = MentraLivePairingStateStore.shared
|
|
1720
|
+
/// Ends the pairing-mode pause at its deadline.
|
|
1721
|
+
private var pairingPauseWorkItem: DispatchWorkItem?
|
|
1722
|
+
/// The running scan is the duplicate-reporting watch scan, not a connect scan.
|
|
1723
|
+
private var pairingWatchScanActive = false
|
|
1724
|
+
private var phoneHelloSentThisSession = false
|
|
1725
|
+
private var pairSelectSentThisSession = false
|
|
1726
|
+
/// `pairing_info` arrived on this BLE session, so the glasses run secure firmware.
|
|
1727
|
+
private var pairingInfoSeenThisSession = false
|
|
1728
|
+
/// `pairing_info.auto_audio` on this BLE session. Old firmware never sends it.
|
|
1729
|
+
private var pairingInfoAutoAudio = false
|
|
1730
|
+
/// Peripherals that advertised the secure-pairing trailer in the last few seconds.
|
|
1731
|
+
/// CoreBluetooth sometimes reports an advertisement without the manufacturer data that
|
|
1732
|
+
/// carries the trailer, so a packet without it right after one with it is partial, not
|
|
1733
|
+
/// legacy firmware. Nothing is persisted: glasses rolled back to legacy firmware must list
|
|
1734
|
+
/// as legacy again.
|
|
1735
|
+
private static var secureTrailerMemory = MentraLiveSecureTrailerMemory()
|
|
1736
|
+
/// Retired persisted name set that hid rolled-back glasses for good. Removed on startup.
|
|
1737
|
+
private static let retiredSecurePairingNamesKey = "mentra_live_secure_pairing_names"
|
|
1738
|
+
/// glasses_ready arrived for a secure pairing target before pairing_info confirmed the bond.
|
|
1739
|
+
private var awaitingSecurePairingConfirmation = false
|
|
1740
|
+
/// pairing_info from secure firmware arrived on this BLE session.
|
|
1741
|
+
private var securePairingConfirmed = false
|
|
1742
|
+
private var securePairingWaitWorkItem: DispatchWorkItem?
|
|
1743
|
+
/// glasses_ready arrived for an explicit pair whose capability is unknown; waiting for
|
|
1744
|
+
/// pairing_info to tell secure from legacy firmware.
|
|
1745
|
+
private var awaitingPairingInfoForUnknownCapability = false
|
|
1746
|
+
private var unknownCapabilityWaitWorkItem: DispatchWorkItem?
|
|
1747
|
+
/// Marks the saved glasses as legacy firmware when a ready session gets no pairing_info.
|
|
1748
|
+
private var secureFirmwareRefreshWorkItem: DispatchWorkItem?
|
|
1749
|
+
/// Internal so tests can shorten the waits.
|
|
1750
|
+
var unknownCapabilityPairingInfoWaitMs = MentraLiveConnectionAttemptPolicy.unknownCapabilityPairingInfoWaitMs
|
|
1751
|
+
var secureFirmwareRefreshMs = MentraLiveConnectionAttemptPolicy.secureFirmwareRefreshMs
|
|
1752
|
+
/// `GlassesStatus.audioSetup` for this driver (see `GlassesAudioSetupPolicy`).
|
|
1753
|
+
private var audioSetup = GlassesAudioSetupTracker()
|
|
1754
|
+
private var audioSetupDeadlineWorkItem: DispatchWorkItem?
|
|
1755
|
+
private var audioSetupStoreListenerId: String?
|
|
1756
|
+
/// Monotonic milliseconds. Internal so tests can drive the audio-setup clock.
|
|
1757
|
+
var audioSetupNowMs: () -> Int = { MentraLive.monotonicNowMs() }
|
|
1550
1758
|
/// Hold GATT teardown until the unpair write can leave the phone.
|
|
1551
1759
|
private var unpairFlushPending = false
|
|
1552
1760
|
private var unpairFlushWorkItem: DispatchWorkItem?
|
|
@@ -1666,6 +1874,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1666
1874
|
private var pendingReconnectSettleWorkItem: DispatchWorkItem?
|
|
1667
1875
|
private var reconnectionWorkItem: DispatchWorkItem?
|
|
1668
1876
|
private var requiresAncs = true
|
|
1877
|
+
/// Experimental iOS transport bridging (Classic profiles after BLE connect). Off by default.
|
|
1878
|
+
/// Static so it can be set from `ConnectOptions` without threading a parameter
|
|
1879
|
+
/// through `DeviceManager`; it applies to every connect issued after it is set.
|
|
1880
|
+
static var enableTransportBridging = true
|
|
1669
1881
|
|
|
1670
1882
|
// MARK: - Initialization
|
|
1671
1883
|
|
|
@@ -1685,6 +1897,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1685
1897
|
} else {
|
|
1686
1898
|
Bridge.log("LIVE: 🎵 Phone audio monitor disabled (BLOCK_AUDIO_DUPLEX=false)")
|
|
1687
1899
|
}
|
|
1900
|
+
|
|
1901
|
+
UserDefaults.standard.removeObject(forKey: Self.retiredSecurePairingNamesKey)
|
|
1902
|
+
restorePairingState()
|
|
1903
|
+
observeAudioSetupInputs()
|
|
1688
1904
|
}
|
|
1689
1905
|
|
|
1690
1906
|
deinit {
|
|
@@ -1705,12 +1921,15 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1705
1921
|
// MARK: - React Native Interface
|
|
1706
1922
|
|
|
1707
1923
|
private var discoveredPeripherals = [String: CBPeripheral]() // name -> peripheral
|
|
1924
|
+
/// Advertisements that arrived with no name. Logged once so a pairing scan that
|
|
1925
|
+
/// sees the glasses but cannot list them is visible in the phone log.
|
|
1926
|
+
private var loggedUnnamedDiscoveries = Set<UUID>()
|
|
1708
1927
|
/// Last advertisement pairing trailer for a discovered name. Re-emits (already-
|
|
1709
1928
|
/// connected / scan replay) must not drop these flags: omitting them makes a
|
|
1710
1929
|
/// secure idle unit look like pairable legacy firmware.
|
|
1711
1930
|
private var discoveredAdvPairing = [String: CachedAdvPairing]()
|
|
1712
1931
|
|
|
1713
|
-
private struct CachedAdvPairing {
|
|
1932
|
+
private struct CachedAdvPairing: Equatable {
|
|
1714
1933
|
let pairingMode: Bool
|
|
1715
1934
|
let pairingCode: String?
|
|
1716
1935
|
let securePairingCapable: Bool
|
|
@@ -1739,20 +1958,67 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1739
1958
|
}
|
|
1740
1959
|
}
|
|
1741
1960
|
|
|
1742
|
-
|
|
1743
|
-
|
|
1961
|
+
/// Automatic connects wait: the saved glasses are in a pairing window or no longer accept
|
|
1962
|
+
/// this phone. An explicit pair is never blocked.
|
|
1963
|
+
private func automaticConnectBlocked() -> Bool {
|
|
1964
|
+
!pairingStore.allowsAutomaticReconnect && !isExplicitPairingAttempt()
|
|
1744
1965
|
}
|
|
1745
1966
|
|
|
1746
|
-
private func
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1967
|
+
private func applyPairingTransition(_ transition: MentraLivePairingTransition) {
|
|
1968
|
+
switch transition.effect {
|
|
1969
|
+
case .none:
|
|
1970
|
+
break
|
|
1971
|
+
case .pause:
|
|
1972
|
+
enterPairingPause()
|
|
1973
|
+
case .resume:
|
|
1974
|
+
resumeAfterPairingPause()
|
|
1975
|
+
case .reject:
|
|
1976
|
+
stopAfterPairingRejected()
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
/// The glasses opened a pairing window. Leave the link, keep the saved glasses and the bonds,
|
|
1981
|
+
/// and make no automatic connection until the window ends. Probing GATT during the window
|
|
1982
|
+
/// races the new phone for the single BLE slot. The watch scan only reads the advertisement.
|
|
1983
|
+
private func enterPairingPause() {
|
|
1984
|
+
leaveGlassesLink(reason: "pairing_mode")
|
|
1985
|
+
armPairingPauseTimer()
|
|
1986
|
+
startPairingWatchScan(reason: "pairing_pause")
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
/// The window is over. Reconnect through a scan so the post-window advertisement
|
|
1990
|
+
/// (`OWNER_CHANGED`) is read before any GATT connect.
|
|
1991
|
+
private func resumeAfterPairingPause() {
|
|
1992
|
+
cancelPairingPauseTimer()
|
|
1993
|
+
if isScanning, !manualDiscoveryActive {
|
|
1994
|
+
centralManager?.stopScan()
|
|
1995
|
+
isScanning = false
|
|
1996
|
+
}
|
|
1997
|
+
guard !isKilled, connectedPeripheral == nil else { return }
|
|
1998
|
+
Bridge.log("LIVE: Pairing pause ended — resuming reconnect to saved glasses")
|
|
1999
|
+
reconnectAttempts = 0
|
|
2000
|
+
handleReconnection()
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
/// The glasses no longer accept this phone. Stop reconnecting, keep the saved glasses so the
|
|
2004
|
+
/// app can offer Pair again or forget. The manager stays alive; every automatic path checks
|
|
2005
|
+
/// the pairing state.
|
|
2006
|
+
private func stopAfterPairingRejected() {
|
|
2007
|
+
cancelPairingPauseTimer()
|
|
2008
|
+
if isExplicitPairingAttempt() { return }
|
|
2009
|
+
leaveGlassesLink(reason: "pairing_rejected")
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
/// Drop the BLE link and every session flag without destroying the manager.
|
|
2013
|
+
private func leaveGlassesLink(reason: String) {
|
|
2014
|
+
cancelPendingReconnect(reason: reason)
|
|
2015
|
+
cancelSecurePairingWait()
|
|
2016
|
+
if isScanning, !manualDiscoveryActive {
|
|
2017
|
+
centralManager?.stopScan()
|
|
2018
|
+
isScanning = false
|
|
2019
|
+
}
|
|
2020
|
+
// Publish the disconnect immediately. didDisconnectPeripheral may lag or race with
|
|
2021
|
+
// cancelPeripheralConnection, and is ignored once connectedPeripheral is nil.
|
|
1756
2022
|
isConnecting = false
|
|
1757
2023
|
clearPhysicalReadiness()
|
|
1758
2024
|
connected = false
|
|
@@ -1760,6 +2026,9 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1760
2026
|
streamControlVersion = 0
|
|
1761
2027
|
readinessCompletedThisBleSession = false
|
|
1762
2028
|
rgbLedAuthorityClaimed = false
|
|
2029
|
+
lastSentMicTuningBody = nil
|
|
2030
|
+
wearTuningQueriedThisLink = false
|
|
2031
|
+
resetSecurePairingSession()
|
|
1763
2032
|
stopAllTimers()
|
|
1764
2033
|
closeL2capFileChannel()
|
|
1765
2034
|
txCharacteristic = nil
|
|
@@ -1770,66 +2039,282 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1770
2039
|
}
|
|
1771
2040
|
connectedPeripheral = nil
|
|
1772
2041
|
connectingPeripheral = nil
|
|
1773
|
-
|
|
1774
|
-
// phone for the single BLE slot and is exactly what entering_pairing_mode forbids.
|
|
2042
|
+
}
|
|
1775
2043
|
|
|
2044
|
+
private func armPairingPauseTimer() {
|
|
2045
|
+
cancelPairingPauseTimer()
|
|
2046
|
+
guard pairingStore.isPaused else { return }
|
|
2047
|
+
let remaining = pairingStore.remainingPauseMs()
|
|
1776
2048
|
let work = DispatchWorkItem { [weak self] in
|
|
1777
2049
|
guard let self else { return }
|
|
1778
|
-
|
|
1779
|
-
self.
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
if
|
|
1783
|
-
self.
|
|
2050
|
+
self.pairingPauseWorkItem = nil
|
|
2051
|
+
let transition = self.pairingStore.update("pause_deadline") {
|
|
2052
|
+
$0.onTick(nowMs: MentraLivePairingStateStore.nowMs())
|
|
2053
|
+
}
|
|
2054
|
+
if transition.effect == .none, self.pairingStore.isPaused {
|
|
2055
|
+
self.armPairingPauseTimer()
|
|
2056
|
+
return
|
|
1784
2057
|
}
|
|
2058
|
+
self.applyPairingTransition(transition)
|
|
1785
2059
|
}
|
|
1786
|
-
|
|
1787
|
-
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(
|
|
2060
|
+
pairingPauseWorkItem = work
|
|
2061
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(max(remaining, 0)), execute: work)
|
|
1788
2062
|
}
|
|
1789
2063
|
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
2064
|
+
private func cancelPairingPauseTimer() {
|
|
2065
|
+
pairingPauseWorkItem?.cancel()
|
|
2066
|
+
pairingPauseWorkItem = nil
|
|
2067
|
+
}
|
|
2068
|
+
|
|
2069
|
+
/// Duplicate-reporting scan that only observes the saved glasses' advertisement, so a
|
|
2070
|
+
/// cleared pairing flag is seen while the phone waits.
|
|
2071
|
+
private func startPairingWatchScan(reason: String) {
|
|
2072
|
+
if centralManager == nil {
|
|
2073
|
+
centralManager = CBCentralManager(
|
|
2074
|
+
delegate: self, queue: bluetoothQueue,
|
|
2075
|
+
options: ["CBCentralManagerOptionShowPowerAlertKey": 0]
|
|
2076
|
+
)
|
|
2077
|
+
return
|
|
2078
|
+
}
|
|
2079
|
+
guard !isKilled, let central = centralManager, central.state == .poweredOn else { return }
|
|
2080
|
+
if isScanning, pairingWatchScanActive { return }
|
|
2081
|
+
if isScanning {
|
|
2082
|
+
central.stopScan()
|
|
2083
|
+
}
|
|
2084
|
+
isScanning = true
|
|
2085
|
+
pairingWatchScanActive = true
|
|
2086
|
+
central.scanForPeripherals(
|
|
2087
|
+
withServices: nil,
|
|
2088
|
+
options: [CBCentralManagerScanOptionAllowDuplicatesKey: true]
|
|
2089
|
+
)
|
|
2090
|
+
pairFlow("scan", "start trigger=pairing_watch reason=\(reason) state=\(pairingStore.describe())")
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
private func observeSavedGlassesAdvertisement(_ trailer: SecurePairingTrailer) {
|
|
2094
|
+
let wasArmed = pairingStore.verificationArmed
|
|
2095
|
+
let transition = pairingStore.update("advert") {
|
|
2096
|
+
$0.onAdvertisement(
|
|
2097
|
+
pairingMode: trailer.pairingMode,
|
|
2098
|
+
ownerChanged: trailer.ownerChanged,
|
|
2099
|
+
generation: trailer.pairingGeneration,
|
|
2100
|
+
nowMs: MentraLivePairingStateStore.nowMs()
|
|
2101
|
+
)
|
|
2102
|
+
}
|
|
2103
|
+
if !wasArmed, pairingStore.verificationArmed {
|
|
2104
|
+
pairFlow("takeover_check", "owner_changed window=\(trailer.pairingGeneration ?? -1) action=verify_once")
|
|
2105
|
+
}
|
|
2106
|
+
applyPairingTransition(transition)
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
/// `phone_hello` on every secure session (the glasses send window notices only to protocol
|
|
2110
|
+
/// 4 phones) and `pair_select` once on an explicit attempt, as soon as GATT is ready. Both
|
|
2111
|
+
/// callers run on a live link: the RX subscribe and a received `pairing_info`.
|
|
2112
|
+
private func sendPairingSessionMessages(reason: String) {
|
|
2113
|
+
let secureTarget = isSecurePairingTarget()
|
|
2114
|
+
// A trailer this scan just saw is a hint to speak the protocol at all; pair_select below
|
|
2115
|
+
// follows only the selected target.
|
|
2116
|
+
let recentSecureTrailer = connectedPeripheral.map {
|
|
2117
|
+
Self.secureTrailerMemory.hasRecentSecureTrailer($0.identifier, nowMs: Self.monotonicNowMs())
|
|
2118
|
+
} ?? false
|
|
2119
|
+
guard secureTarget || isSavedGlassesSecureFirmware() || pairingInfoSeenThisSession || recentSecureTrailer
|
|
2120
|
+
else { return }
|
|
2121
|
+
if !phoneHelloSentThisSession {
|
|
2122
|
+
phoneHelloSentThisSession = true
|
|
2123
|
+
pairFlow("phone_hello", "protocol=\(Self.pairingProtocolVersion) trigger=\(reason)")
|
|
2124
|
+
sendJson(["type": "phone_hello", "pairing_protocol": Self.pairingProtocolVersion], wakeUp: false, requireAck: false)
|
|
2125
|
+
}
|
|
2126
|
+
if !pairSelectSentThisSession,
|
|
2127
|
+
MentraLiveConnectionAttemptPolicy.shouldSendPairSelect(
|
|
2128
|
+
explicitPairing: isExplicitPairingAttempt(),
|
|
2129
|
+
securePairingTarget: secureTarget
|
|
2130
|
+
)
|
|
2131
|
+
{
|
|
2132
|
+
pairSelectSentThisSession = true
|
|
2133
|
+
pairFlow("pair_select", "trigger=\(reason)")
|
|
2134
|
+
sendJson(["type": "pair_select"], wakeUp: false, requireAck: false)
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
private func resetSecurePairingSession() {
|
|
2139
|
+
securePairingConfirmed = false
|
|
2140
|
+
awaitingSecurePairingConfirmation = false
|
|
2141
|
+
cancelSecurePairingWait()
|
|
2142
|
+
cancelUnknownCapabilityWait()
|
|
2143
|
+
cancelSecureFirmwareRefresh()
|
|
2144
|
+
phoneHelloSentThisSession = false
|
|
2145
|
+
pairSelectSentThisSession = false
|
|
2146
|
+
pairingInfoSeenThisSession = false
|
|
2147
|
+
pairingInfoAutoAudio = false
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
/// Owner-loss destroy marks the manager dead. A later connect must build a new one.
|
|
2151
|
+
/// A live manager already holds the peripheral found during the pairing scan.
|
|
2152
|
+
var isManagerKilled: Bool {
|
|
2153
|
+
isKilled
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2156
|
+
/// Pairing/auth failures: CBError 14 or insufficient authentication/encryption.
|
|
2157
|
+
/// Connection timeouts and the glasses' 0x14 refusal are not.
|
|
2158
|
+
nonisolated static func isPairingAuthFailure(_ error: Error?) -> Bool {
|
|
1793
2159
|
guard let error = error as NSError? else { return false }
|
|
1794
2160
|
if error.domain == CBATTErrorDomain {
|
|
1795
2161
|
return error.code == CBATTError.insufficientAuthentication.rawValue
|
|
1796
2162
|
|| error.code == CBATTError.insufficientEncryption.rawValue
|
|
1797
2163
|
}
|
|
1798
2164
|
if error.domain == CBErrorDomain {
|
|
1799
|
-
return error
|
|
2165
|
+
return Self.isPeerRemovedPairingInformation(error)
|
|
1800
2166
|
}
|
|
1801
2167
|
return false
|
|
1802
2168
|
}
|
|
1803
2169
|
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
2170
|
+
nonisolated static func isPeerRemovedPairingInformation(_ error: Error?) -> Bool {
|
|
2171
|
+
guard let error = error as NSError?, error.domain == CBErrorDomain else { return false }
|
|
2172
|
+
return error.code == CBError.peerRemovedPairingInformation.rawValue
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
static let staleIosBondPromptedAtKey = "mentra_live_stale_ios_bond_prompted_at"
|
|
2176
|
+
|
|
2177
|
+
/// A new pairing attempt should be allowed to show the prompt again if it still fails.
|
|
2178
|
+
static func clearStaleIosBondPrompt() {
|
|
2179
|
+
UserDefaults.standard.removeObject(forKey: staleIosBondPromptedAtKey)
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
/// What a connect or link failure means for the next attempt.
|
|
2183
|
+
private enum ConnectFailureOutcome {
|
|
2184
|
+
/// Not a pairing failure: retry as usual.
|
|
2185
|
+
case retry
|
|
2186
|
+
/// An auth failure that did not settle anything yet: retry through the scan backoff,
|
|
2187
|
+
/// because a pending direct connect would repeat the failure without delay.
|
|
2188
|
+
case backoff
|
|
2189
|
+
/// Stop: the user must act, or the glasses rejected this phone.
|
|
2190
|
+
case stop
|
|
2191
|
+
}
|
|
2192
|
+
|
|
2193
|
+
private func classifyConnectFailure(_ error: Error?) -> ConnectFailureOutcome {
|
|
2194
|
+
// iOS keeps keys the glasses no longer have. On an explicit attempt only the user can
|
|
2195
|
+
// fix that, in Settings > Bluetooth.
|
|
2196
|
+
if Self.isPeerRemovedPairingInformation(error), isExplicitPairingAttempt() {
|
|
2197
|
+
promptToForgetStaleIosBond()
|
|
2198
|
+
return .stop
|
|
2199
|
+
}
|
|
2200
|
+
if Self.isPairingAuthFailure(error) {
|
|
2201
|
+
let code = (error as NSError?)?.code ?? 0
|
|
2202
|
+
return recordAuthRejection(reason: "ios_auth_\(code)") ? .stop : .backoff
|
|
2203
|
+
}
|
|
2204
|
+
pairingStore.update("connect_failed") { $0.onConnectAttemptFailed() }
|
|
2205
|
+
return .retry
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
/// Internal so native tests can feed CoreBluetooth errors.
|
|
2209
|
+
/// - Returns: true when the caller must not reconnect.
|
|
2210
|
+
func stopAfterConnectFailure(_ error: Error?) -> Bool {
|
|
2211
|
+
switch classifyConnectFailure(error) {
|
|
2212
|
+
case .retry:
|
|
2213
|
+
return false
|
|
2214
|
+
case .backoff:
|
|
2215
|
+
if !isKilled { handleReconnection() }
|
|
2216
|
+
return true
|
|
2217
|
+
case .stop:
|
|
2218
|
+
return true
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
/// Error 14 on an explicit attempt: show the forget-in-Settings screen once per attempt.
|
|
2223
|
+
private func promptToForgetStaleIosBond() {
|
|
2224
|
+
cancelPendingReconnect(reason: "stale_ios_bond")
|
|
2225
|
+
let now = Date().timeIntervalSince1970
|
|
2226
|
+
let promptedAt = UserDefaults.standard.double(forKey: Self.staleIosBondPromptedAtKey)
|
|
2227
|
+
guard MentraLiveConnectionAttemptPolicy.shouldPromptToForgetStaleIosBond(
|
|
2228
|
+
peerRemovedPairingInformation: true,
|
|
2229
|
+
promptedAt: promptedAt,
|
|
2230
|
+
now: now
|
|
2231
|
+
) else {
|
|
2232
|
+
pairFlow("stale_ios_bond", "prompt=suppressed")
|
|
2233
|
+
return
|
|
2234
|
+
}
|
|
2235
|
+
UserDefaults.standard.set(now, forKey: Self.staleIosBondPromptedAtKey)
|
|
2236
|
+
pairFlow("stale_ios_bond", "prompt=forget_bluetooth code=\(MentraLiveConnectionAttemptPolicy.iosForgetPairingCode)")
|
|
2237
|
+
Bridge.sendPairFailureEvent(
|
|
2238
|
+
"errors:pairingCouldNotStart",
|
|
2239
|
+
code: MentraLiveConnectionAttemptPolicy.iosForgetPairingCode
|
|
2240
|
+
)
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
/// The saved glasses run secure-pairing firmware, learned from their last `pairing_info`.
|
|
2244
|
+
/// Legacy firmware never rejects a non-owner on purpose, so its failures are not owner loss.
|
|
2245
|
+
private func isSavedGlassesSecureFirmware() -> Bool {
|
|
2246
|
+
DeviceStore.shared.get("bluetooth", "mentra_live_secure_firmware") as? Bool == true
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
/// Remember what the connected glasses reported so later rejections can be read correctly.
|
|
2250
|
+
private func rememberSecureFirmware(_ secure: Bool) {
|
|
2251
|
+
guard isSavedGlassesSecureFirmware() != secure else { return }
|
|
1811
2252
|
Task { @MainActor in
|
|
1812
|
-
DeviceStore.shared.apply("bluetooth", "
|
|
1813
|
-
|
|
1814
|
-
|
|
2253
|
+
DeviceStore.shared.apply("bluetooth", "mentra_live_secure_firmware", secure)
|
|
2254
|
+
}
|
|
2255
|
+
Bridge.saveSetting("mentra_live_secure_firmware", secure)
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
/// One authentication-type failure. The state machine decides whether it settles a takeover
|
|
2259
|
+
/// (the one verification after `OWNER_CHANGED`) or completes the 3-strike streak.
|
|
2260
|
+
/// - Returns: true when this phone is now `pairing_rejected`.
|
|
2261
|
+
@discardableResult
|
|
2262
|
+
private func recordAuthRejection(reason: String) -> Bool {
|
|
2263
|
+
let explicitAttempt = isExplicitPairingAttempt()
|
|
2264
|
+
let secureFirmware = isSavedGlassesSecureFirmware()
|
|
2265
|
+
let transition = pairingStore.update(reason) {
|
|
2266
|
+
$0.onAuthRejection(
|
|
2267
|
+
secureFirmware: secureFirmware,
|
|
2268
|
+
explicitAttempt: explicitAttempt,
|
|
2269
|
+
nowMs: MentraLivePairingStateStore.nowMs()
|
|
2270
|
+
)
|
|
2271
|
+
}
|
|
2272
|
+
Bridge.log("LIVE: Auth rejection (\(reason)) — \(transition.note)")
|
|
2273
|
+
applyPairingTransition(transition)
|
|
2274
|
+
return transition.effect == .reject
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
/// A restarted app keeps the pause until its deadline and keeps a rejection.
|
|
2278
|
+
private func restorePairingState() {
|
|
2279
|
+
let savedName = UserDefaults.standard.string(forKey: PREFS_DEVICE_NAME) ?? ""
|
|
2280
|
+
guard let transition = pairingStore.ensureRestored(savedDeviceId: savedName) else {
|
|
2281
|
+
if pairingStore.isPaused {
|
|
2282
|
+
armPairingPauseTimer()
|
|
2283
|
+
startPairingWatchScan(reason: "manager_init")
|
|
2284
|
+
}
|
|
2285
|
+
return
|
|
2286
|
+
}
|
|
2287
|
+
if transition.effect == .pause {
|
|
2288
|
+
armPairingPauseTimer()
|
|
2289
|
+
startPairingWatchScan(reason: "restored_pause")
|
|
1815
2290
|
}
|
|
1816
|
-
Bridge.saveSetting("default_wearable", "")
|
|
1817
|
-
Bridge.saveSetting("device_name", "")
|
|
1818
|
-
Bridge.saveSetting("device_address", "")
|
|
1819
|
-
Bridge.sendTypedMessage("owner_replaced", body: ["reason": reason])
|
|
1820
2291
|
}
|
|
1821
2292
|
|
|
1822
2293
|
func connectById(_ deviceName: String) {
|
|
1823
|
-
|
|
1824
|
-
|
|
2294
|
+
// A destroyed manager stays inert; DeviceManager builds a new one for the next connect.
|
|
2295
|
+
if isKilled {
|
|
2296
|
+
pairFlow("connect_by_id", "blocked reason=manager_killed name=\(deviceName)")
|
|
1825
2297
|
return
|
|
1826
2298
|
}
|
|
1827
|
-
|
|
2299
|
+
if !isExplicitPairingAttempt() {
|
|
2300
|
+
pairingStore.update("connect_by_id") { $0.bindDevice(deviceName) }
|
|
2301
|
+
}
|
|
2302
|
+
if automaticConnectBlocked() {
|
|
2303
|
+
// Paused for a pairing window or rejected. Keep the saved glasses; a pause watches
|
|
2304
|
+
// the advertisement and resumes on its own, a rejection waits for Pair again.
|
|
2305
|
+
UserDefaults.standard.set(deviceName, forKey: PREFS_DEVICE_NAME)
|
|
2306
|
+
pairFlow("connect_by_id", "deferred state=\(pairingStore.describe()) name=\(deviceName)")
|
|
2307
|
+
if pairingStore.isPaused {
|
|
2308
|
+
armPairingPauseTimer()
|
|
2309
|
+
startPairingWatchScan(reason: "connect_by_id")
|
|
2310
|
+
}
|
|
2311
|
+
return
|
|
2312
|
+
}
|
|
2313
|
+
pairFlow("connect_by_id", "name=\(deviceName)")
|
|
1828
2314
|
cancelPendingReconnect(reason: "connect_by_id")
|
|
1829
2315
|
// Save the device name for future reconnection
|
|
1830
2316
|
UserDefaults.standard.set(deviceName, forKey: PREFS_DEVICE_NAME)
|
|
1831
2317
|
|
|
1832
|
-
// Start scanning to find this specific device
|
|
1833
2318
|
if centralManager == nil {
|
|
1834
2319
|
centralManager = CBCentralManager(
|
|
1835
2320
|
delegate: self, queue: bluetoothQueue,
|
|
@@ -1837,37 +2322,111 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1837
2322
|
)
|
|
1838
2323
|
}
|
|
1839
2324
|
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
2325
|
+
attachKnownOrScan(deviceName)
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
/// Connect the peripheral this phone already found. After the BLE bond the
|
|
2329
|
+
/// glasses stop general advertising, so a new name scan never sees them.
|
|
2330
|
+
private func attachKnownOrScan(_ deviceName: String) {
|
|
2331
|
+
if isConnecting {
|
|
2332
|
+
pairFlow("attach", "decision=already_connecting name=\(deviceName)")
|
|
2333
|
+
return
|
|
2334
|
+
}
|
|
2335
|
+
if txCharacteristic != nil, connectedPeripheral?.name == deviceName {
|
|
2336
|
+
pairFlow(
|
|
2337
|
+
"attach",
|
|
2338
|
+
"decision=live_gatt id=\(connectedPeripheral?.identifier.uuidString ?? "-") name=\(deviceName)"
|
|
2339
|
+
)
|
|
2340
|
+
return
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
let poweredOn = centralManager?.state == .poweredOn
|
|
2344
|
+
let cached = discoveredPeripherals[deviceName]
|
|
2345
|
+
let savedId = savedPeripheralId(for: deviceName)
|
|
2346
|
+
let systemMatch = poweredOn ? systemConnectedPeripheral(deviceName: deviceName, savedId: savedId) : nil
|
|
2347
|
+
let route = MentraLiveKnownConnectRoute.decide(
|
|
2348
|
+
bluetoothPoweredOn: poweredOn,
|
|
2349
|
+
hasCachedPeripheral: cached != nil,
|
|
2350
|
+
hasSavedIdentifier: savedId != nil,
|
|
2351
|
+
systemConnectedMatch: systemMatch != nil
|
|
2352
|
+
)
|
|
2353
|
+
pairFlow(
|
|
2354
|
+
"attach",
|
|
2355
|
+
"decision=\(route.rawValue) name=\(deviceName) cached=\(cached != nil) savedId=\(savedId?.uuidString ?? "-") system=\(systemMatch?.identifier.uuidString ?? "-") central=\(centralManager?.state.rawValue ?? -1)"
|
|
2356
|
+
)
|
|
2357
|
+
|
|
2358
|
+
switch route {
|
|
2359
|
+
case .waitForPower:
|
|
2360
|
+
return
|
|
2361
|
+
case .cachedPeripheral:
|
|
2362
|
+
if let cached {
|
|
2363
|
+
connectToDevice(cached)
|
|
2364
|
+
}
|
|
2365
|
+
case .savedIdentifier:
|
|
2366
|
+
if let savedId,
|
|
2367
|
+
let peripheral = centralManager?.retrievePeripherals(withIdentifiers: [savedId]).first
|
|
1849
2368
|
{
|
|
1850
|
-
|
|
1851
|
-
discoveredPeripherals[
|
|
1852
|
-
|
|
2369
|
+
rememberPeripheral(peripheral, name: deviceName)
|
|
2370
|
+
discoveredPeripherals[deviceName] = peripheral
|
|
2371
|
+
connectToDevice(peripheral)
|
|
2372
|
+
} else {
|
|
2373
|
+
pairFlow("attach", "decision=saved_identifier_missing id=\(savedId?.uuidString ?? "-") fallback=scan")
|
|
2374
|
+
startScan(reason: "saved_identifier_missing")
|
|
2375
|
+
}
|
|
2376
|
+
case .alreadyConnected:
|
|
2377
|
+
if let systemMatch {
|
|
2378
|
+
let matchedName = systemMatch.name ?? deviceName
|
|
2379
|
+
discoveredPeripherals[matchedName] = systemMatch
|
|
2380
|
+
rememberPeripheral(systemMatch, name: deviceName)
|
|
2381
|
+
connectToDevice(systemMatch)
|
|
2382
|
+
}
|
|
2383
|
+
case .scan:
|
|
2384
|
+
if isScanning {
|
|
2385
|
+
pairFlow("attach", "decision=already_scanning name=\(deviceName)")
|
|
2386
|
+
return
|
|
2387
|
+
}
|
|
2388
|
+
startScan(reason: "no_known_peripheral")
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
1853
2391
|
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
2392
|
+
private func savedPeripheralId(for name: String) -> UUID? {
|
|
2393
|
+
let map = UserDefaults.standard.dictionary(forKey: PREFS_PERIPHERAL_IDS) as? [String: String]
|
|
2394
|
+
guard let raw = map?[name] else {
|
|
2395
|
+
return nil
|
|
2396
|
+
}
|
|
2397
|
+
return UUID(uuidString: raw)
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
private func rememberPeripheral(_ peripheral: CBPeripheral, name: String) {
|
|
2401
|
+
var map = UserDefaults.standard.dictionary(forKey: PREFS_PERIPHERAL_IDS) as? [String: String] ?? [:]
|
|
2402
|
+
map[name] = peripheral.identifier.uuidString
|
|
2403
|
+
UserDefaults.standard.set(map, forKey: PREFS_PERIPHERAL_IDS)
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
private func systemConnectedPeripheral(deviceName: String, savedId: UUID?) -> CBPeripheral? {
|
|
2407
|
+
let connected = centralManager?.retrieveConnectedPeripherals(withServices: [SERVICE_UUID]) ?? []
|
|
2408
|
+
pairFlow("system_connected", "count=\(connected.count)")
|
|
2409
|
+
for peripheral in connected {
|
|
2410
|
+
let name = peripheral.name ?? "-"
|
|
2411
|
+
let matches = MentraLivePendingPairingTarget.matches(
|
|
2412
|
+
connectedName: peripheral.name ?? "",
|
|
2413
|
+
connectedIdentifier: peripheral.identifier.uuidString,
|
|
2414
|
+
pendingName: deviceName,
|
|
2415
|
+
pendingIdentifier: savedId?.uuidString ?? ""
|
|
2416
|
+
)
|
|
2417
|
+
pairFlow(
|
|
2418
|
+
"system_connected",
|
|
2419
|
+
"name=\(name) id=\(peripheral.identifier.uuidString) match=\(matches)"
|
|
2420
|
+
)
|
|
2421
|
+
if matches {
|
|
2422
|
+
return peripheral
|
|
1866
2423
|
}
|
|
1867
2424
|
}
|
|
2425
|
+
return nil
|
|
2426
|
+
}
|
|
1868
2427
|
|
|
1869
|
-
|
|
1870
|
-
|
|
2428
|
+
private func pairFlow(_ event: String, _ detail: String) {
|
|
2429
|
+
Bridge.log("PAIRFLOW: \(event) \(detail)")
|
|
1871
2430
|
}
|
|
1872
2431
|
|
|
1873
2432
|
func getConnectedBluetoothName() -> String? {
|
|
@@ -1879,7 +2438,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
1879
2438
|
Bridge.log("LIVE: disconnect deferred — waiting for unpair write")
|
|
1880
2439
|
return
|
|
1881
2440
|
}
|
|
1882
|
-
|
|
2441
|
+
pairFlow(
|
|
2442
|
+
"disconnect",
|
|
2443
|
+
"name=\(connectedPeripheral?.name ?? "-") id=\(connectedPeripheral?.identifier.uuidString ?? "-") scanning=\(isScanning) connecting=\(isConnecting)"
|
|
2444
|
+
)
|
|
1883
2445
|
|
|
1884
2446
|
// if rgbLedAuthorityClaimed {
|
|
1885
2447
|
// sendRgbLedControlAuthority(false)
|
|
@@ -2193,11 +2755,14 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2193
2755
|
return commands.removeFirst()
|
|
2194
2756
|
}
|
|
2195
2757
|
|
|
2758
|
+
var count: Int { commands.count }
|
|
2196
2759
|
func removeAll() {
|
|
2197
2760
|
commands.removeAll()
|
|
2198
2761
|
}
|
|
2199
2762
|
}
|
|
2200
2763
|
|
|
2764
|
+
var pendingBleCommandCount: Int { commandQueue.count }
|
|
2765
|
+
|
|
2201
2766
|
private func setupCommandQueue() {
|
|
2202
2767
|
Task.detached { [weak self] in
|
|
2203
2768
|
guard let self else { return }
|
|
@@ -2308,25 +2873,26 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2308
2873
|
|
|
2309
2874
|
// MARK: - BLE Scanning
|
|
2310
2875
|
|
|
2311
|
-
private func startScan() {
|
|
2312
|
-
if pairingYieldActive {
|
|
2313
|
-
Bridge.log("LIVE: startScan blocked — pairing yield active")
|
|
2314
|
-
return
|
|
2315
|
-
}
|
|
2316
|
-
|
|
2876
|
+
private func startScan(reason: String = "scan") {
|
|
2317
2877
|
guard centralManager!.state == .poweredOn else {
|
|
2318
|
-
|
|
2878
|
+
pairFlow("scan", "blocked reason=bluetooth_off trigger=\(reason) central=\(centralManager?.state.rawValue ?? -1)")
|
|
2319
2879
|
return
|
|
2320
2880
|
}
|
|
2321
2881
|
|
|
2322
|
-
|
|
2882
|
+
pairFlow("scan", "start trigger=\(reason) target=\(UserDefaults.standard.string(forKey: PREFS_DEVICE_NAME) ?? "-")")
|
|
2323
2883
|
discoveredAdvPairing.removeAll()
|
|
2884
|
+
loggedUnnamedDiscoveries.removeAll()
|
|
2324
2885
|
isScanning = true
|
|
2886
|
+
pairingWatchScanActive = false
|
|
2325
2887
|
|
|
2326
2888
|
startReadinessCheckLoop()
|
|
2327
2889
|
|
|
2890
|
+
// The choose-glasses scan has to hear a later advertisement when the glasses
|
|
2891
|
+
// enter pairing mode. iOS reports each peripheral once unless duplicates are
|
|
2892
|
+
// allowed, so the row would stay "Not in pairing mode". Reconnect scans stay
|
|
2893
|
+
// duplicate-free so a saved glasses advertisement cannot start another connect.
|
|
2328
2894
|
let scanOptions: [String: Any] = [
|
|
2329
|
-
CBCentralManagerScanOptionAllowDuplicatesKey:
|
|
2895
|
+
CBCentralManagerScanOptionAllowDuplicatesKey: manualDiscoveryActive,
|
|
2330
2896
|
]
|
|
2331
2897
|
|
|
2332
2898
|
// let knownPeripherals = centralManager?.retrieveConnectedPeripherals(withServices: [SERVICE_UUID])
|
|
@@ -2360,18 +2926,24 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2360
2926
|
centralManager?.stopScan()
|
|
2361
2927
|
isScanning = false
|
|
2362
2928
|
DeviceStore.shared.apply(ObservableStore.bluetoothCategory, "searching", false)
|
|
2363
|
-
|
|
2929
|
+
pairFlow("scan", "stop")
|
|
2364
2930
|
}
|
|
2365
2931
|
|
|
2366
2932
|
// MARK: - Connection Management
|
|
2367
2933
|
|
|
2368
2934
|
private func connectToDevice(_ peripheral: CBPeripheral) {
|
|
2369
|
-
if
|
|
2370
|
-
|
|
2935
|
+
if automaticConnectBlocked() {
|
|
2936
|
+
pairFlow(
|
|
2937
|
+
"connect_device",
|
|
2938
|
+
"blocked state=\(pairingStore.describe()) id=\(peripheral.identifier.uuidString)"
|
|
2939
|
+
)
|
|
2371
2940
|
isConnecting = false
|
|
2372
2941
|
return
|
|
2373
2942
|
}
|
|
2374
|
-
|
|
2943
|
+
pairFlow(
|
|
2944
|
+
"connect_device",
|
|
2945
|
+
"id=\(peripheral.identifier.uuidString) name=\(peripheral.name ?? "-") state=\(peripheral.state.rawValue)"
|
|
2946
|
+
)
|
|
2375
2947
|
|
|
2376
2948
|
// A timed attempt supersedes any pending reconnect: withdraw one aimed at other
|
|
2377
2949
|
// glasses, and drop tracking for this one so its settle timer cannot touch this attempt.
|
|
@@ -2402,7 +2974,8 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2402
2974
|
guard let centralManager,
|
|
2403
2975
|
MentraLiveConnectionAttemptPolicy.shouldReconnectDirectly(
|
|
2404
2976
|
isKilled: isKilled,
|
|
2405
|
-
|
|
2977
|
+
// A window just ended: scan first so its advertisement is read.
|
|
2978
|
+
pairingYieldActive: automaticConnectBlocked() || pairingStore.awaitingTakeoverCheck,
|
|
2406
2979
|
bluetoothPoweredOn: centralManager.state == .poweredOn,
|
|
2407
2980
|
peripheralName: peripheral.name,
|
|
2408
2981
|
savedDeviceName: UserDefaults.standard.string(forKey: PREFS_DEVICE_NAME)
|
|
@@ -2433,9 +3006,13 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2433
3006
|
// The default requirement lets the system complete that authorization flow
|
|
2434
3007
|
// before the glasses subscribe; apps that do not relay notifications can opt out.
|
|
2435
3008
|
Bridge.log("LIVE: ANCS connection requirement \(requiresAncs ? "enabled" : "disabled")")
|
|
3009
|
+
Bridge.log("LIVE: Transport bridging \(Self.enableTransportBridging ? "enabled" : "disabled")")
|
|
2436
3010
|
centralManager?.connect(
|
|
2437
3011
|
peripheral,
|
|
2438
|
-
options: MentraLiveConnectionOptions.coreBluetoothOptions(
|
|
3012
|
+
options: MentraLiveConnectionOptions.coreBluetoothOptions(
|
|
3013
|
+
requiresAncs: requiresAncs,
|
|
3014
|
+
enableTransportBridging: Self.enableTransportBridging
|
|
3015
|
+
)
|
|
2439
3016
|
)
|
|
2440
3017
|
#endif
|
|
2441
3018
|
}
|
|
@@ -2523,8 +3100,11 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2523
3100
|
return
|
|
2524
3101
|
}
|
|
2525
3102
|
|
|
2526
|
-
if
|
|
2527
|
-
Bridge.log("LIVE: Reconnection aborted - pairing
|
|
3103
|
+
if automaticConnectBlocked() {
|
|
3104
|
+
Bridge.log("LIVE: Reconnection aborted - pairing state \(pairingStore.describe())")
|
|
3105
|
+
if pairingStore.isPaused {
|
|
3106
|
+
startPairingWatchScan(reason: "reconnect_while_paused")
|
|
3107
|
+
}
|
|
2528
3108
|
return
|
|
2529
3109
|
}
|
|
2530
3110
|
|
|
@@ -2561,8 +3141,8 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2561
3141
|
guard let self = self else { return }
|
|
2562
3142
|
|
|
2563
3143
|
// Use peripheral presence, not connectionState: we stay CONNECTING during backoff until scan/connect.
|
|
2564
|
-
if self.
|
|
2565
|
-
Bridge.log("LIVE: Reconnection aborted - pairing
|
|
3144
|
+
if self.automaticConnectBlocked() {
|
|
3145
|
+
Bridge.log("LIVE: Reconnection aborted - pairing state \(self.pairingStore.describe())")
|
|
2566
3146
|
return
|
|
2567
3147
|
}
|
|
2568
3148
|
if self.connectedPeripheral == nil, !self.isKilled {
|
|
@@ -2611,6 +3191,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2611
3191
|
private func attachL2capFileChannel(_ channel: CBL2CAPChannel) {
|
|
2612
3192
|
closeL2capFileChannel()
|
|
2613
3193
|
|
|
3194
|
+
guard let peer = connectedPeripheral, let token = notificationIngress.capture(peer) else { return }
|
|
2614
3195
|
let channelId = UUID()
|
|
2615
3196
|
l2capFileChannelId = channelId
|
|
2616
3197
|
let reader = MentraLiveL2capChannel(
|
|
@@ -2620,7 +3201,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2620
3201
|
// while the existing transfer state remains serialized on the main actor.
|
|
2621
3202
|
DispatchQueue.main.async {
|
|
2622
3203
|
guard let self, self.l2capFileChannelId == channelId else { return }
|
|
2623
|
-
self.
|
|
3204
|
+
self.receiveBleNotification(frame, token: token)
|
|
2624
3205
|
}
|
|
2625
3206
|
},
|
|
2626
3207
|
onClose: { [weak self] in
|
|
@@ -2796,10 +3377,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2796
3377
|
|
|
2797
3378
|
switch type {
|
|
2798
3379
|
case "glasses_ready":
|
|
2799
|
-
// A queued glasses_ready from the connection
|
|
2800
|
-
// must not
|
|
2801
|
-
if
|
|
2802
|
-
Bridge.log("LIVE: Ignoring stale glasses_ready during pairing
|
|
3380
|
+
// A queued glasses_ready from the connection just left for a pairing window
|
|
3381
|
+
// must not count as a session. Only accept readiness on a live GATT.
|
|
3382
|
+
if pairingStore.isPaused && connectedPeripheral == nil {
|
|
3383
|
+
Bridge.log("LIVE: Ignoring stale glasses_ready during pairing pause")
|
|
2803
3384
|
return
|
|
2804
3385
|
}
|
|
2805
3386
|
// glasses_ready is a REMOTE wire-session reset: the glasses ran
|
|
@@ -2807,10 +3388,6 @@ class MentraLive: NSObject, SGCManager {
|
|
|
2807
3388
|
// framing regardless of what this side negotiated earlier. Start a fresh
|
|
2808
3389
|
// wire epoch (clears v2-active state that would otherwise gate the
|
|
2809
3390
|
// handshake off), then negotiate from the caps this message advertises.
|
|
2810
|
-
pairingYieldAwaitingReclaim = false
|
|
2811
|
-
pairingYieldActive = false
|
|
2812
|
-
pairingYieldEndWorkItem?.cancel()
|
|
2813
|
-
pairingYieldEndWorkItem = nil
|
|
2814
3391
|
resetWireNegotiationState()
|
|
2815
3392
|
parsePeerWireCaps(json)
|
|
2816
3393
|
maybeSendWireHandshake()
|
|
@@ -3006,25 +3583,113 @@ class MentraLive: NSObject, SGCManager {
|
|
|
3006
3583
|
Bridge.log("LIVE: Received pong response - connection healthy")
|
|
3007
3584
|
|
|
3008
3585
|
case "entering_pairing_mode":
|
|
3009
|
-
let windowMs =
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
let
|
|
3013
|
-
|
|
3014
|
-
"
|
|
3586
|
+
let windowMs = json["window_ms"] as? Int
|
|
3587
|
+
let windowId = json["window_id"] as? Int
|
|
3588
|
+
let ownerRetained = json["owner_retained"] as? Bool ?? false
|
|
3589
|
+
let enterReason = json["reason"] as? String ?? "user_gesture"
|
|
3590
|
+
Bridge.log(
|
|
3591
|
+
"PAIRFLOW: entering_pairing_mode reason=\(enterReason) window_ms=\(windowMs ?? -1) window_id=\(windowId ?? -1) owner_retained=\(ownerRetained)"
|
|
3592
|
+
)
|
|
3593
|
+
var enterBody: [String: Any] = [
|
|
3594
|
+
"window_ms": windowMs ?? MentraLiveConnectionAttemptPolicy.defaultPairingWindowMs,
|
|
3595
|
+
"reason": enterReason,
|
|
3596
|
+
"owner_retained": ownerRetained,
|
|
3015
3597
|
]
|
|
3016
|
-
|
|
3598
|
+
if let windowId { enterBody["window_id"] = windowId }
|
|
3599
|
+
Bridge.sendTypedMessage("entering_pairing_mode", body: enterBody)
|
|
3600
|
+
let explicitAttempt = isExplicitPairingAttempt()
|
|
3601
|
+
let deviceId = connectedPeripheral?.name ?? UserDefaults.standard.string(forKey: PREFS_DEVICE_NAME) ?? ""
|
|
3602
|
+
let enterTransition = pairingStore.update("entering_pairing_mode") {
|
|
3603
|
+
$0.onEnteringPairingMode(
|
|
3604
|
+
windowMs: windowMs,
|
|
3605
|
+
windowId: windowId,
|
|
3606
|
+
ownerRetained: ownerRetained,
|
|
3607
|
+
explicitAttempt: explicitAttempt,
|
|
3608
|
+
deviceId: deviceId,
|
|
3609
|
+
nowMs: MentraLivePairingStateStore.nowMs()
|
|
3610
|
+
)
|
|
3611
|
+
}
|
|
3612
|
+
applyPairingTransition(enterTransition)
|
|
3613
|
+
|
|
3614
|
+
case "pairing_mode_ended":
|
|
3615
|
+
let endedReason = json["reason"] as? String
|
|
3616
|
+
let endedWindowId = json["window_id"] as? Int
|
|
3617
|
+
Bridge.log("PAIRFLOW: pairing_mode_ended reason=\(endedReason ?? "-") window_id=\(endedWindowId ?? -1)")
|
|
3618
|
+
var endedBody: [String: Any] = ["reason": endedReason ?? ""]
|
|
3619
|
+
if let endedWindowId { endedBody["window_id"] = endedWindowId }
|
|
3620
|
+
Bridge.sendTypedMessage("pairing_mode_ended", body: endedBody)
|
|
3621
|
+
let endedTransition = pairingStore.update("pairing_mode_ended") {
|
|
3622
|
+
$0.onPairingModeEnded(reason: endedReason, windowId: endedWindowId)
|
|
3623
|
+
}
|
|
3624
|
+
applyPairingTransition(endedTransition)
|
|
3017
3625
|
|
|
3018
3626
|
case "pairing_info":
|
|
3627
|
+
Bridge.log(
|
|
3628
|
+
"PAIRFLOW: pairing_info secure=\(json["secure_pairing_capable"] as? Bool ?? false) classic_ready=\(json["classic_bond_ready"] as? Bool ?? false) had_previous=\(json["had_previous_bond"] as? Bool ?? false) transfer=\(json["ownership_transfer"] as? Bool ?? false) owner_committed=\(json["owner_committed"] as? Bool ?? false) auto_audio=\(json["auto_audio"] as? Bool ?? false) code=<redacted>"
|
|
3629
|
+
)
|
|
3630
|
+
let ownerCommitted = json["owner_committed"] as? Bool == true
|
|
3631
|
+
// Legacy firmware that omits this field is not secure-capable.
|
|
3632
|
+
let securePairingCapable = json["secure_pairing_capable"] as? Bool ?? false
|
|
3633
|
+
// Read before any readiness below: the audio wait of a pairing depends on it.
|
|
3634
|
+
pairingInfoAutoAudio = json["auto_audio"] as? Bool ?? false
|
|
3635
|
+
rememberSecureFirmware(securePairingCapable)
|
|
3636
|
+
if securePairingCapable {
|
|
3637
|
+
pairingInfoSeenThisSession = true
|
|
3638
|
+
cancelSecureFirmwareRefresh()
|
|
3639
|
+
sendPairingSessionMessages(reason: "pairing_info")
|
|
3640
|
+
}
|
|
3641
|
+
// Readiness parked on an unknown capability: this pairing_info decides the rule.
|
|
3642
|
+
var legacyReadiness = false
|
|
3643
|
+
if awaitingPairingInfoForUnknownCapability {
|
|
3644
|
+
cancelUnknownCapabilityWait()
|
|
3645
|
+
if securePairingCapable {
|
|
3646
|
+
pairFlow("readiness", "capability=unknown pairing_info=secure action=await_owner_commit")
|
|
3647
|
+
awaitingSecurePairingConfirmation = true
|
|
3648
|
+
if !ownerCommitted { armSecurePairingWait() }
|
|
3649
|
+
} else {
|
|
3650
|
+
pairFlow("readiness", "capability=unknown pairing_info=legacy action=ready")
|
|
3651
|
+
legacyReadiness = true
|
|
3652
|
+
}
|
|
3653
|
+
}
|
|
3654
|
+
// Only owner_committed proves the bond. secure_pairing_capable is a capability flag
|
|
3655
|
+
// readable on the unencrypted window session.
|
|
3656
|
+
if MentraLiveConnectionAttemptPolicy.pairingInfoConfirmsOwner(ownerCommitted: ownerCommitted) {
|
|
3657
|
+
securePairingConfirmed = true
|
|
3658
|
+
cancelSecurePairingWait()
|
|
3659
|
+
if awaitingSecurePairingConfirmation {
|
|
3660
|
+
Bridge.log("LIVE: pairing_info owner_committed — reporting ready")
|
|
3661
|
+
markFullyBooted()
|
|
3662
|
+
}
|
|
3663
|
+
}
|
|
3664
|
+
if legacyReadiness { markFullyBooted() }
|
|
3019
3665
|
Bridge.sendPairingInfo(
|
|
3020
3666
|
hadPreviousBond: json["had_previous_bond"] as? Bool ?? false,
|
|
3021
3667
|
pairingCode: json["pairing_code"] as? String,
|
|
3022
3668
|
classicBondReady: json["classic_bond_ready"] as? Bool ?? false,
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3669
|
+
securePairingCapable: securePairingCapable,
|
|
3670
|
+
protocolVersion: json["protocol_version"] as? Int ?? 1,
|
|
3671
|
+
ownershipTransfer: json["ownership_transfer"] as? Bool ?? false,
|
|
3672
|
+
ownerCommitted: ownerCommitted,
|
|
3673
|
+
autoAudio: json["auto_audio"] as? Bool
|
|
3026
3674
|
)
|
|
3027
3675
|
|
|
3676
|
+
case "classic_link":
|
|
3677
|
+
// The glasses' own view of the phone's A2DP and HFP links to them. Preferred over
|
|
3678
|
+
// the phone's audio route, which another Bluetooth audio device can hold.
|
|
3679
|
+
let a2dp = json["a2dp"] as? Bool ?? false
|
|
3680
|
+
let hfp = json["hfp"] as? Bool ?? false
|
|
3681
|
+
pairFlow("classic_link", "a2dp=\(a2dp) hfp=\(hfp)")
|
|
3682
|
+
audioSetup.classicLink(a2dp: a2dp, hfp: hfp)
|
|
3683
|
+
refreshAudioSetup(trigger: "classic_link")
|
|
3684
|
+
#if !os(macOS)
|
|
3685
|
+
if !AudioSessionMonitor.glassesReportClassicLink {
|
|
3686
|
+
// From now on the phone signals come from passive route observation, never
|
|
3687
|
+
// from the category probe. Re-read them that way.
|
|
3688
|
+
AudioSessionMonitor.glassesReportClassicLink = true
|
|
3689
|
+
DeviceManager.shared.checkCurrentAudioDevice()
|
|
3690
|
+
}
|
|
3691
|
+
#endif
|
|
3692
|
+
|
|
3028
3693
|
case "imu_response", "imu_stream_response", "imu_gesture_response",
|
|
3029
3694
|
"imu_gesture_subscribed", "imu_ack", "imu_error":
|
|
3030
3695
|
// Handle IMU-related responses
|
|
@@ -3204,6 +3869,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
3204
3869
|
)
|
|
3205
3870
|
}
|
|
3206
3871
|
if let bluetoothMacAddress = nonEmptyStringValue(fields, "bt_mac_address") {
|
|
3872
|
+
recordWifiAdbPeerIdentity(bluetoothMacAddress)
|
|
3207
3873
|
DeviceStore.shared.apply("glasses", "bluetoothMacAddress", bluetoothMacAddress)
|
|
3208
3874
|
}
|
|
3209
3875
|
if let wifiMacAddress = nonEmptyStringValue(fields, "wifi_mac_address") {
|
|
@@ -3341,6 +4007,11 @@ class MentraLive: NSObject, SGCManager {
|
|
|
3341
4007
|
processAncsRelay(body)
|
|
3342
4008
|
}
|
|
3343
4009
|
|
|
4010
|
+
case "sr_tpdef":
|
|
4011
|
+
if let body = k900ParseBody(json["B"]), body["setting"] as? String == "touch_gesture_default" {
|
|
4012
|
+
emitSettingsAck(body)
|
|
4013
|
+
}
|
|
4014
|
+
|
|
3344
4015
|
case "sr_hrt":
|
|
3345
4016
|
if let bodyObj = json["B"] as? [String: Any] {
|
|
3346
4017
|
let readyResponse = bodyObj["ready"] as? Int ?? 0
|
|
@@ -3685,6 +4356,66 @@ class MentraLive: NSObject, SGCManager {
|
|
|
3685
4356
|
sendJson(json, wakeUp: true)
|
|
3686
4357
|
}
|
|
3687
4358
|
|
|
4359
|
+
@discardableResult func beginBleNotificationSession(source: AnyObject?) -> MentraLiveNotificationIngress.Token? {
|
|
4360
|
+
wifiAdbPeerIdentity = nil
|
|
4361
|
+
incomingChunkReassembler.clear()
|
|
4362
|
+
resetWireNegotiationState()
|
|
4363
|
+
commandQueue.removeAll()
|
|
4364
|
+
return notificationIngress.begin(source)
|
|
4365
|
+
}
|
|
4366
|
+
|
|
4367
|
+
func endBleNotificationSession() {
|
|
4368
|
+
wifiAdbPeerIdentity = nil
|
|
4369
|
+
incomingChunkReassembler.clear()
|
|
4370
|
+
resetWireNegotiationState()
|
|
4371
|
+
commandQueue.removeAll()
|
|
4372
|
+
_ = notificationIngress.begin(nil)
|
|
4373
|
+
}
|
|
4374
|
+
|
|
4375
|
+
func receiveBleNotification(_ data: Data, token: MentraLiveNotificationIngress.Token) {
|
|
4376
|
+
guard notificationIngress.current(token) else { return }
|
|
4377
|
+
let previous = receivingNotification
|
|
4378
|
+
receivingNotification = token
|
|
4379
|
+
defer { receivingNotification = previous }
|
|
4380
|
+
processReceivedData(data)
|
|
4381
|
+
}
|
|
4382
|
+
|
|
4383
|
+
private func recordWifiAdbPeerIdentity(_ mac: String) {
|
|
4384
|
+
guard let token = receivingNotification, notificationIngress.current(token) else { return }
|
|
4385
|
+
wifiAdbPeerIdentity = (mac, token)
|
|
4386
|
+
}
|
|
4387
|
+
|
|
4388
|
+
private func currentWifiAdbAdmission(_ admission: WifiAdbAdmission) -> Bool {
|
|
4389
|
+
notificationIngress.current(admission.identity.token) && wireSessionGeneration == admission.wireEpoch &&
|
|
4390
|
+
wifiAdbPeerIdentity?.mac == admission.identity.mac && wifiAdbLinkReady
|
|
4391
|
+
}
|
|
4392
|
+
|
|
4393
|
+
var wifiAdbLinkReady: Bool {
|
|
4394
|
+
connectionState == ConnTypes.CONNECTED && connectedPeripheral?.state == .connected && txCharacteristic != nil
|
|
4395
|
+
}
|
|
4396
|
+
|
|
4397
|
+
func enqueueWifiAdbState(_ enabled: Bool) -> Bool {
|
|
4398
|
+
// Match Wi-Fi forget: public log delegates must not reenter between validation and enqueue.
|
|
4399
|
+
sendJson(["type": "set_wifi_adb_state", "enabled": enabled], wakeUp: true, requireAck: true, bridgeLogging: false)
|
|
4400
|
+
}
|
|
4401
|
+
|
|
4402
|
+
/// MainActor performs identity validation and queue insertion without suspension.
|
|
4403
|
+
func sendWifiAdbState(_ enabled: Bool, expectedBluetoothMac: String) throws {
|
|
4404
|
+
guard expectedBluetoothMac.range(of: "^(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$", options: .regularExpression) != nil,
|
|
4405
|
+
let identity = wifiAdbPeerIdentity,
|
|
4406
|
+
identity.mac.caseInsensitiveCompare(expectedBluetoothMac) == .orderedSame,
|
|
4407
|
+
notificationIngress.current(identity.token), wifiAdbLinkReady else {
|
|
4408
|
+
throw BluetoothSdkError(code: "device_identity_changed", message: "The selected glasses identity or connection changed.")
|
|
4409
|
+
}
|
|
4410
|
+
let admission = WifiAdbAdmission(identity: identity, wireEpoch: wireSessionGeneration)
|
|
4411
|
+
wifiAdbAdmission = admission
|
|
4412
|
+
defer { wifiAdbAdmission = nil }
|
|
4413
|
+
guard enqueueWifiAdbState(enabled), !admission.refused, admission.queued > 0,
|
|
4414
|
+
currentWifiAdbAdmission(admission) else {
|
|
4415
|
+
throw BluetoothSdkError(code: "command_send_failed", message: "Could not queue Wi-Fi debugging for the selected glasses.")
|
|
4416
|
+
}
|
|
4417
|
+
}
|
|
4418
|
+
|
|
3688
4419
|
func sendWifiAdbState(_ enabled: Bool) {
|
|
3689
4420
|
Bridge.log("LIVE: 🔧 Sending Wi-Fi ADB state: \(enabled)")
|
|
3690
4421
|
|
|
@@ -3850,6 +4581,25 @@ class MentraLive: NSObject, SGCManager {
|
|
|
3850
4581
|
sendJson(json, wakeUp: true)
|
|
3851
4582
|
}
|
|
3852
4583
|
|
|
4584
|
+
func sendTouchGestureDefaultBehaviorEnabled(requestId: String, gesture: TouchGesture, enabled: Bool) throws {
|
|
4585
|
+
guard connectedPeripheral != nil, txCharacteristic != nil else {
|
|
4586
|
+
throw BluetoothSdkError(code: "not_connected", message: "Mentra Live glasses are not connected.")
|
|
4587
|
+
}
|
|
4588
|
+
try sendTouchGestureDefaultCommand(requestId: requestId, gesture: gesture, enabled: enabled)
|
|
4589
|
+
}
|
|
4590
|
+
|
|
4591
|
+
/// Kept separate from readiness admission so tests can replay the real K900 wire exchange.
|
|
4592
|
+
func sendTouchGestureDefaultCommand(requestId: String, gesture: TouchGesture, enabled: Bool) throws {
|
|
4593
|
+
let body = try JSONSerialization.data(withJSONObject: [
|
|
4594
|
+
"request_id": requestId, "gesture": gesture.rawValue, "enabled": enabled,
|
|
4595
|
+
])
|
|
4596
|
+
guard let bodyString = String(data: body, encoding: .utf8),
|
|
4597
|
+
sendRawK900Command(["C": "cs_tpdef", "V": 1, "B": bodyString])
|
|
4598
|
+
else {
|
|
4599
|
+
throw BluetoothSdkError(code: "command_send_failed", message: "Could not encode the touch gesture setting.")
|
|
4600
|
+
}
|
|
4601
|
+
}
|
|
4602
|
+
|
|
3853
4603
|
func sendGalleryServerEnabled(requestId: String, enabled: Bool) {
|
|
3854
4604
|
sendJson([
|
|
3855
4605
|
"type": "set_gallery_server_enabled",
|
|
@@ -3921,6 +4671,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
3921
4671
|
// MARK: - Message Handlers
|
|
3922
4672
|
|
|
3923
4673
|
private func handleGlassesReady() {
|
|
4674
|
+
pairFlow(
|
|
4675
|
+
"glasses_ready",
|
|
4676
|
+
"id=\(connectedPeripheral?.identifier.uuidString ?? "-") name=\(connectedPeripheral?.name ?? "-") gatt=\(txCharacteristic != nil)"
|
|
4677
|
+
)
|
|
3924
4678
|
Bridge.log("LIVE: 🎉 Received glasses_ready message - SOC is booted and ready!")
|
|
3925
4679
|
|
|
3926
4680
|
stopReadinessCheckLoop()
|
|
@@ -3978,11 +4732,142 @@ class MentraLive: NSObject, SGCManager {
|
|
|
3978
4732
|
}
|
|
3979
4733
|
}
|
|
3980
4734
|
|
|
4735
|
+
// handleDeviceReady promotes the pairing target to the default device. Secure glasses
|
|
4736
|
+
// own a phone only after a BLE bond and announce it with pairing_info; GATT works
|
|
4737
|
+
// unencrypted inside the open window, so readiness alone is not a pairing.
|
|
4738
|
+
let readiness = MentraLiveConnectionAttemptPolicy.readinessAtGlassesReady(
|
|
4739
|
+
explicitPairing: isExplicitPairingAttempt(),
|
|
4740
|
+
selectedCapability: selectedTargetSecureCapability(),
|
|
4741
|
+
securePairingInfoThisSession: pairingInfoSeenThisSession,
|
|
4742
|
+
securePairingConfirmed: securePairingConfirmed
|
|
4743
|
+
)
|
|
4744
|
+
switch readiness {
|
|
4745
|
+
case .ready:
|
|
4746
|
+
markFullyBooted()
|
|
4747
|
+
case .awaitOwnerCommit:
|
|
4748
|
+
Bridge.log("LIVE: glasses_ready before owner commit — waiting for pairing_info")
|
|
4749
|
+
awaitingSecurePairingConfirmation = true
|
|
4750
|
+
armSecurePairingWait()
|
|
4751
|
+
case .awaitPairingInfo:
|
|
4752
|
+
pairFlow("readiness", "capability=unknown action=await_pairing_info waitMs=\(unknownCapabilityPairingInfoWaitMs)")
|
|
4753
|
+
armUnknownCapabilityWait()
|
|
4754
|
+
}
|
|
4755
|
+
}
|
|
4756
|
+
|
|
4757
|
+
private func markFullyBooted() {
|
|
4758
|
+
// A session that reached ready means the glasses still admit this phone. Read the
|
|
4759
|
+
// attempt kind first: setting fullyBooted runs handleDeviceReady, which clears pending.
|
|
4760
|
+
let explicitAttempt = isExplicitPairingAttempt()
|
|
4761
|
+
pairingStore.update("session_ready") { $0.onSessionReady(explicitAttempt: explicitAttempt) }
|
|
4762
|
+
awaitingSecurePairingConfirmation = false
|
|
4763
|
+
cancelSecurePairingWait()
|
|
4764
|
+
cancelUnknownCapabilityWait()
|
|
4765
|
+
armSecureFirmwareRefresh()
|
|
4766
|
+
if !fullyBooted {
|
|
4767
|
+
audioSetup.startPhase(
|
|
4768
|
+
explicitAttempt ? .pairing(autoAudio: pairingInfoAutoAudio) : .reconnect,
|
|
4769
|
+
nowMs: audioSetupNowMs()
|
|
4770
|
+
)
|
|
4771
|
+
}
|
|
4772
|
+
// Publish audioSetup before readiness so the ready update never carries a stale idle.
|
|
4773
|
+
refreshAudioSetup(trigger: "session_ready", sessionReady: true)
|
|
3981
4774
|
fullyBooted = true
|
|
3982
4775
|
connected = true
|
|
3983
4776
|
updateConnectionState(ConnTypes.CONNECTED)
|
|
3984
4777
|
}
|
|
3985
4778
|
|
|
4779
|
+
/// The selected scan row's capability (`pending_device_secure_pairing_capable`); nil when
|
|
4780
|
+
/// unknown, for example a target re-listed from saved state.
|
|
4781
|
+
private func selectedTargetSecureCapability() -> Bool? {
|
|
4782
|
+
DeviceStore.shared.get("bluetooth", "pending_device_secure_pairing_capable") as? Bool
|
|
4783
|
+
}
|
|
4784
|
+
|
|
4785
|
+
/// Unknown capability: a secure pairing_info within the wait switches to the secure rule,
|
|
4786
|
+
/// none means legacy firmware and the glasses are reported ready.
|
|
4787
|
+
private func armUnknownCapabilityWait() {
|
|
4788
|
+
cancelUnknownCapabilityWait()
|
|
4789
|
+
awaitingPairingInfoForUnknownCapability = true
|
|
4790
|
+
let work = DispatchWorkItem { [weak self] in
|
|
4791
|
+
guard let self, self.awaitingPairingInfoForUnknownCapability else { return }
|
|
4792
|
+
self.unknownCapabilityWaitWorkItem = nil
|
|
4793
|
+
self.awaitingPairingInfoForUnknownCapability = false
|
|
4794
|
+
self.pairFlow("readiness", "capability=unknown pairing_info=none action=ready_legacy")
|
|
4795
|
+
self.markFullyBooted()
|
|
4796
|
+
}
|
|
4797
|
+
unknownCapabilityWaitWorkItem = work
|
|
4798
|
+
DispatchQueue.main.asyncAfter(
|
|
4799
|
+
deadline: .now() + .milliseconds(max(unknownCapabilityPairingInfoWaitMs, 0)),
|
|
4800
|
+
execute: work
|
|
4801
|
+
)
|
|
4802
|
+
}
|
|
4803
|
+
|
|
4804
|
+
private func cancelUnknownCapabilityWait() {
|
|
4805
|
+
awaitingPairingInfoForUnknownCapability = false
|
|
4806
|
+
unknownCapabilityWaitWorkItem?.cancel()
|
|
4807
|
+
unknownCapabilityWaitWorkItem = nil
|
|
4808
|
+
}
|
|
4809
|
+
|
|
4810
|
+
/// `mentra_live_secure_firmware` follows the current glasses: a ready session that gets no
|
|
4811
|
+
/// pairing_info runs legacy firmware (for example after a rollback).
|
|
4812
|
+
private func armSecureFirmwareRefresh() {
|
|
4813
|
+
guard secureFirmwareRefreshWorkItem == nil, !pairingInfoSeenThisSession else { return }
|
|
4814
|
+
let work = DispatchWorkItem { [weak self] in
|
|
4815
|
+
guard let self else { return }
|
|
4816
|
+
self.secureFirmwareRefreshWorkItem = nil
|
|
4817
|
+
guard !self.pairingInfoSeenThisSession else { return }
|
|
4818
|
+
self.pairFlow("secure_firmware", "pairing_info=none afterMs=\(self.secureFirmwareRefreshMs) secure=false")
|
|
4819
|
+
self.rememberSecureFirmware(false)
|
|
4820
|
+
}
|
|
4821
|
+
secureFirmwareRefreshWorkItem = work
|
|
4822
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(max(secureFirmwareRefreshMs, 0)), execute: work)
|
|
4823
|
+
}
|
|
4824
|
+
|
|
4825
|
+
private func cancelSecureFirmwareRefresh() {
|
|
4826
|
+
secureFirmwareRefreshWorkItem?.cancel()
|
|
4827
|
+
secureFirmwareRefreshWorkItem = nil
|
|
4828
|
+
}
|
|
4829
|
+
|
|
4830
|
+
/// Bond confirmation has to arrive on its own. A dismissed pairing sheet, or a bond that
|
|
4831
|
+
/// never finishes, must fail the attempt instead of leaving the loading screen up.
|
|
4832
|
+
private func armSecurePairingWait() {
|
|
4833
|
+
cancelSecurePairingWait()
|
|
4834
|
+
// Every parked secure readiness gets this deadline, so the pair can never hang.
|
|
4835
|
+
guard MentraLiveConnectionAttemptPolicy.shouldRunSecurePairingWait(
|
|
4836
|
+
explicitPairing: isExplicitPairingAttempt(),
|
|
4837
|
+
securePairingTarget: isSecurePairingTarget()
|
|
4838
|
+
) else { return }
|
|
4839
|
+
let work = DispatchWorkItem { [weak self] in
|
|
4840
|
+
guard let self else { return }
|
|
4841
|
+
guard self.awaitingSecurePairingConfirmation, self.isExplicitPairingAttempt(),
|
|
4842
|
+
!self.securePairingConfirmed
|
|
4843
|
+
else { return }
|
|
4844
|
+
self.awaitingSecurePairingConfirmation = false
|
|
4845
|
+
Bridge.log("LIVE: secure pairing bond wait expired")
|
|
4846
|
+
Bridge.sendPairFailureEvent("errors:pairingCouldNotStart")
|
|
4847
|
+
}
|
|
4848
|
+
securePairingWaitWorkItem = work
|
|
4849
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 40, execute: work)
|
|
4850
|
+
}
|
|
4851
|
+
|
|
4852
|
+
private func cancelSecurePairingWait() {
|
|
4853
|
+
securePairingWaitWorkItem?.cancel()
|
|
4854
|
+
securePairingWaitWorkItem = nil
|
|
4855
|
+
}
|
|
4856
|
+
|
|
4857
|
+
private func isSecurePairingTarget() -> Bool {
|
|
4858
|
+
MentraLiveConnectionAttemptPolicy.isSecurePairingTarget(
|
|
4859
|
+
selectedCapability: selectedTargetSecureCapability(),
|
|
4860
|
+
securePairingInfoThisSession: pairingInfoSeenThisSession
|
|
4861
|
+
)
|
|
4862
|
+
}
|
|
4863
|
+
|
|
4864
|
+
/// The user is pairing from the scan screen rather than reconnecting a saved owner.
|
|
4865
|
+
private func isExplicitPairingAttempt() -> Bool {
|
|
4866
|
+
let pendingName = DeviceStore.shared.get("bluetooth", "pending_device_name") as? String ?? ""
|
|
4867
|
+
let pendingAddress = DeviceStore.shared.get("bluetooth", "pending_device_address") as? String ?? ""
|
|
4868
|
+
return manualDiscoveryActive || !pendingName.isEmpty || !pendingAddress.isEmpty
|
|
4869
|
+
}
|
|
4870
|
+
|
|
3986
4871
|
private func handleWifiScanResult(_ json: [String: Any]) {
|
|
3987
4872
|
var networks: [[String: Any]] = []
|
|
3988
4873
|
|
|
@@ -4019,6 +4904,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
4019
4904
|
DeviceStore.shared.apply("glasses", "otaVersionUrl", otaVersionUrl)
|
|
4020
4905
|
DeviceStore.shared.apply("glasses", "firmwareVersion", firmwareVersion)
|
|
4021
4906
|
if let bluetoothMacAddress {
|
|
4907
|
+
recordWifiAdbPeerIdentity(bluetoothMacAddress)
|
|
4022
4908
|
DeviceStore.shared.apply("glasses", "bluetoothMacAddress", bluetoothMacAddress)
|
|
4023
4909
|
}
|
|
4024
4910
|
if let serialNumber {
|
|
@@ -4701,6 +5587,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
4701
5587
|
}
|
|
4702
5588
|
|
|
4703
5589
|
func queueSend(_ data: Data, id: String, trace: BleWriteTrace?) {
|
|
5590
|
+
if let admission = wifiAdbAdmission {
|
|
5591
|
+
guard currentWifiAdbAdmission(admission) else { admission.refused = true; return }
|
|
5592
|
+
admission.queued += 1
|
|
5593
|
+
}
|
|
4704
5594
|
let significantQueueSize = SIGNIFICANT_BLE_TRACE_QUEUE_SIZE
|
|
4705
5595
|
// Insert synchronously on MainActor so fragments and dependent commands retain call order.
|
|
4706
5596
|
let generation = wireSessionGeneration
|
|
@@ -4805,7 +5695,8 @@ class MentraLive: NSObject, SGCManager {
|
|
|
4805
5695
|
}
|
|
4806
5696
|
}
|
|
4807
5697
|
|
|
4808
|
-
BleTraceLogger.logMap(direction: "phone_to_glasses", layer: layer, type: trace.commandType, payload: payload
|
|
5698
|
+
BleTraceLogger.logMap(direction: "phone_to_glasses", layer: layer, type: trace.commandType, payload: payload,
|
|
5699
|
+
forwardToBridge: wifiAdbAdmission == nil)
|
|
4809
5700
|
}
|
|
4810
5701
|
|
|
4811
5702
|
private func bleTraceWarningReason(stage: String, extra: [String: Any?]) -> String? {
|
|
@@ -5135,7 +6026,8 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5135
6026
|
direction: "phone_to_glasses",
|
|
5136
6027
|
layer: "sdk_ble_command",
|
|
5137
6028
|
payload: json,
|
|
5138
|
-
bytes: jsonData.count
|
|
6029
|
+
bytes: jsonData.count,
|
|
6030
|
+
forwardToBridge: bridgeLogging
|
|
5139
6031
|
)
|
|
5140
6032
|
|
|
5141
6033
|
if useBinaryWireProtocol, isNewVersion {
|
|
@@ -5747,7 +6639,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5747
6639
|
private var readinessCheckDispatchTimer: DispatchSourceTimer?
|
|
5748
6640
|
|
|
5749
6641
|
private func startReadinessCheckLoop() {
|
|
5750
|
-
|
|
6642
|
+
pairFlow(
|
|
6643
|
+
"readiness",
|
|
6644
|
+
"start gatt=\(txCharacteristic != nil) id=\(connectedPeripheral?.identifier.uuidString ?? "-") name=\(connectedPeripheral?.name ?? "-")"
|
|
6645
|
+
)
|
|
5751
6646
|
stopReadinessCheckLoop()
|
|
5752
6647
|
|
|
5753
6648
|
readinessCheckCounter = 0
|
|
@@ -5767,8 +6662,15 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5767
6662
|
guard let self else { return }
|
|
5768
6663
|
|
|
5769
6664
|
self.readinessCheckCounter += 1
|
|
6665
|
+
let gattReady = self.txCharacteristic != nil
|
|
6666
|
+
if self.readinessCheckCounter == 1 || self.readinessCheckCounter % 4 == 0 {
|
|
6667
|
+
self.pairFlow(
|
|
6668
|
+
"readiness",
|
|
6669
|
+
"n=\(self.readinessCheckCounter) gatt=\(gattReady) id=\(self.connectedPeripheral?.identifier.uuidString ?? "-")"
|
|
6670
|
+
)
|
|
6671
|
+
}
|
|
5770
6672
|
Bridge.log(
|
|
5771
|
-
"LIVE: 🔄 Readiness check #\(self.readinessCheckCounter): waiting for glasses SOC to boot"
|
|
6673
|
+
"LIVE: 🔄 Readiness check #\(self.readinessCheckCounter): waiting for glasses SOC to boot gatt=\(gattReady)"
|
|
5772
6674
|
)
|
|
5773
6675
|
// requestReadyK900()
|
|
5774
6676
|
|
|
@@ -5799,8 +6701,15 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5799
6701
|
}
|
|
5800
6702
|
|
|
5801
6703
|
private func stopReadinessCheckLoop() {
|
|
6704
|
+
let wasRunning = readinessCheckDispatchTimer != nil
|
|
5802
6705
|
readinessCheckDispatchTimer?.cancel()
|
|
5803
6706
|
readinessCheckDispatchTimer = nil
|
|
6707
|
+
if wasRunning {
|
|
6708
|
+
pairFlow(
|
|
6709
|
+
"readiness",
|
|
6710
|
+
"stop n=\(readinessCheckCounter) gatt=\(txCharacteristic != nil) id=\(connectedPeripheral?.identifier.uuidString ?? "-")"
|
|
6711
|
+
)
|
|
6712
|
+
}
|
|
5804
6713
|
Bridge.log("LIVE: 🔄 Stopped glasses SOC readiness check loop")
|
|
5805
6714
|
}
|
|
5806
6715
|
|
|
@@ -5812,6 +6721,10 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5812
6721
|
guard let self else { return }
|
|
5813
6722
|
|
|
5814
6723
|
if self.isConnecting, self.connectionState != ConnTypes.CONNECTED {
|
|
6724
|
+
self.pairFlow(
|
|
6725
|
+
"connect_timeout",
|
|
6726
|
+
"id=\(self.connectedPeripheral?.identifier.uuidString ?? "-") name=\(self.connectedPeripheral?.name ?? "-") state=\(self.connectionState)"
|
|
6727
|
+
)
|
|
5815
6728
|
Bridge.log("LIVE: Connection timeout - closing GATT connection")
|
|
5816
6729
|
self.isConnecting = false
|
|
5817
6730
|
|
|
@@ -5846,7 +6759,7 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5846
6759
|
/// promotes it to the default device. Re-emit only that explicit pending target; an
|
|
5847
6760
|
/// established owner's connected glasses have no pending identity and remain hidden.
|
|
5848
6761
|
private func emitConnectedPendingDeviceForPairingScan() {
|
|
5849
|
-
guard manualDiscoveryActive,
|
|
6762
|
+
guard manualDiscoveryActive, let peripheral = connectedPeripheral,
|
|
5850
6763
|
let name = peripheral.name,
|
|
5851
6764
|
MentraLivePendingPairingTarget.shouldRecover(
|
|
5852
6765
|
isConnected: peripheral.state == .connected,
|
|
@@ -5872,17 +6785,35 @@ class MentraLive: NSObject, SGCManager {
|
|
|
5872
6785
|
)
|
|
5873
6786
|
}
|
|
5874
6787
|
|
|
6788
|
+
private func discoveredAdvertisementName(
|
|
6789
|
+
_ peripheral: CBPeripheral,
|
|
6790
|
+
advertisementData: [String: Any]
|
|
6791
|
+
) -> String? {
|
|
6792
|
+
if let name = peripheral.name, !name.isEmpty { return name }
|
|
6793
|
+
if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String, !name.isEmpty {
|
|
6794
|
+
return name
|
|
6795
|
+
}
|
|
6796
|
+
return nil
|
|
6797
|
+
}
|
|
6798
|
+
|
|
6799
|
+
/// Returns true when this advertisement changed the row the scan screen is showing.
|
|
6800
|
+
@discardableResult
|
|
5875
6801
|
private func cacheAdvPairing(
|
|
5876
6802
|
_ name: String,
|
|
5877
6803
|
pairingMode: Bool,
|
|
5878
6804
|
pairingCode: String?,
|
|
5879
6805
|
securePairingCapable: Bool
|
|
5880
|
-
) {
|
|
5881
|
-
|
|
6806
|
+
) -> Bool {
|
|
6807
|
+
let next = CachedAdvPairing(
|
|
5882
6808
|
pairingMode: pairingMode,
|
|
5883
6809
|
pairingCode: pairingCode,
|
|
5884
6810
|
securePairingCapable: securePairingCapable
|
|
5885
6811
|
)
|
|
6812
|
+
if discoveredAdvPairing[name] == next {
|
|
6813
|
+
return false
|
|
6814
|
+
}
|
|
6815
|
+
discoveredAdvPairing[name] = next
|
|
6816
|
+
return true
|
|
5886
6817
|
}
|
|
5887
6818
|
|
|
5888
6819
|
private func emitDiscoveredDeviceFromCache(
|
|
@@ -6003,9 +6934,14 @@ class MentraLive: NSObject, SGCManager {
|
|
|
6003
6934
|
// MARK: - Cleanup
|
|
6004
6935
|
|
|
6005
6936
|
private func destroy() {
|
|
6006
|
-
|
|
6937
|
+
pairFlow(
|
|
6938
|
+
"destroy",
|
|
6939
|
+
"name=\(connectedPeripheral?.name ?? "-") id=\(connectedPeripheral?.identifier.uuidString ?? "-") scanning=\(isScanning)"
|
|
6940
|
+
)
|
|
6007
6941
|
|
|
6008
6942
|
isKilled = true
|
|
6943
|
+
// The pairing state outlives the manager; only its timer belongs to this instance.
|
|
6944
|
+
cancelPairingPauseTimer()
|
|
6009
6945
|
unpairFlushWorkItem?.cancel()
|
|
6010
6946
|
unpairFlushWorkItem = nil
|
|
6011
6947
|
unpairFlushPending = false
|
|
@@ -6049,6 +6985,92 @@ class MentraLive: NSObject, SGCManager {
|
|
|
6049
6985
|
centralManager = nil
|
|
6050
6986
|
|
|
6051
6987
|
updateConnectionState(ConnTypes.DISCONNECTED)
|
|
6988
|
+
resetSecurePairingSession()
|
|
6989
|
+
stopObservingAudioSetupInputs()
|
|
6990
|
+
}
|
|
6991
|
+
}
|
|
6992
|
+
|
|
6993
|
+
// MARK: - Audio setup
|
|
6994
|
+
|
|
6995
|
+
extension MentraLive {
|
|
6996
|
+
nonisolated static func monotonicNowMs() -> Int {
|
|
6997
|
+
Int(ProcessInfo.processInfo.systemUptime * 1000)
|
|
6998
|
+
}
|
|
6999
|
+
|
|
7000
|
+
private static let audioSetupInputKeys: Set<String> = [
|
|
7001
|
+
"fullyBooted", "bluetoothClassicConnected", "bluetoothClassicProfilesConnected", "bluetoothAudioRouted",
|
|
7002
|
+
]
|
|
7003
|
+
|
|
7004
|
+
/// Readiness (from any writer, including `sr_hrt ready=0`) and the phone's three audio
|
|
7005
|
+
/// signals re-evaluate the state in the same update that changed them.
|
|
7006
|
+
fileprivate func observeAudioSetupInputs() {
|
|
7007
|
+
guard audioSetupStoreListenerId == nil else { return }
|
|
7008
|
+
audioSetup.startPhase(.reconnect, nowMs: audioSetupNowMs())
|
|
7009
|
+
audioSetupStoreListenerId = DeviceStore.shared.store.addListener { [weak self] category, changes in
|
|
7010
|
+
MainActor.assumeIsolated {
|
|
7011
|
+
guard category == "glasses",
|
|
7012
|
+
changes.keys.contains(where: { MentraLive.audioSetupInputKeys.contains($0) })
|
|
7013
|
+
else { return }
|
|
7014
|
+
self?.refreshAudioSetup(trigger: "status")
|
|
7015
|
+
}
|
|
7016
|
+
}
|
|
7017
|
+
refreshAudioSetup(trigger: "init")
|
|
7018
|
+
}
|
|
7019
|
+
|
|
7020
|
+
fileprivate func stopObservingAudioSetupInputs() {
|
|
7021
|
+
if let id = audioSetupStoreListenerId {
|
|
7022
|
+
DeviceStore.shared.store.removeListener(id)
|
|
7023
|
+
audioSetupStoreListenerId = nil
|
|
7024
|
+
}
|
|
7025
|
+
audioSetupDeadlineWorkItem?.cancel()
|
|
7026
|
+
audioSetupDeadlineWorkItem = nil
|
|
7027
|
+
}
|
|
7028
|
+
|
|
7029
|
+
/// The phone's view: the Classic bond, a connected HFP input or a matching Bluetooth output.
|
|
7030
|
+
private func phoneAudioUp() -> Bool {
|
|
7031
|
+
["bluetoothClassicConnected", "bluetoothClassicProfilesConnected", "bluetoothAudioRouted"].contains {
|
|
7032
|
+
DeviceStore.shared.get("glasses", $0) as? Bool == true
|
|
7033
|
+
}
|
|
7034
|
+
}
|
|
7035
|
+
|
|
7036
|
+
/// The BLE session ended. Its `classic_link` report no longer applies.
|
|
7037
|
+
fileprivate func endAudioSetupSession() {
|
|
7038
|
+
audioSetup.endSession()
|
|
7039
|
+
#if !os(macOS)
|
|
7040
|
+
AudioSessionMonitor.glassesReportClassicLink = false
|
|
7041
|
+
#endif
|
|
7042
|
+
refreshAudioSetup(trigger: "disconnect")
|
|
7043
|
+
}
|
|
7044
|
+
|
|
7045
|
+
/// Feeds `GlassesAudioSetupPolicy`, arms one timer for its next deadline and publishes on change.
|
|
7046
|
+
/// Only the active driver publishes; a manager being created or torn down stays quiet.
|
|
7047
|
+
fileprivate func refreshAudioSetup(trigger: String, sessionReady: Bool? = nil) {
|
|
7048
|
+
guard (DeviceManager.shared.sgc as AnyObject?) === self else {
|
|
7049
|
+
audioSetupDeadlineWorkItem?.cancel()
|
|
7050
|
+
audioSetupDeadlineWorkItem = nil
|
|
7051
|
+
return
|
|
7052
|
+
}
|
|
7053
|
+
let nowMs = audioSetupNowMs()
|
|
7054
|
+
let decision = audioSetup.evaluate(
|
|
7055
|
+
sessionReady: sessionReady ?? fullyBooted,
|
|
7056
|
+
phoneAudioUp: phoneAudioUp(),
|
|
7057
|
+
nowMs: nowMs
|
|
7058
|
+
)
|
|
7059
|
+
audioSetupDeadlineWorkItem?.cancel()
|
|
7060
|
+
audioSetupDeadlineWorkItem = nil
|
|
7061
|
+
if let deadlineMs = decision.nextDeadlineMs {
|
|
7062
|
+
let work = DispatchWorkItem { [weak self] in
|
|
7063
|
+
guard let self else { return }
|
|
7064
|
+
self.audioSetupDeadlineWorkItem = nil
|
|
7065
|
+
self.refreshAudioSetup(trigger: "deadline")
|
|
7066
|
+
}
|
|
7067
|
+
audioSetupDeadlineWorkItem = work
|
|
7068
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(max(deadlineMs - nowMs, 0)), execute: work)
|
|
7069
|
+
}
|
|
7070
|
+
guard DeviceStore.shared.publishAudioSetup(decision.state) else { return }
|
|
7071
|
+
Bridge.log(
|
|
7072
|
+
"PAIRFLOW: audio_setup state=\(decision.state.name) phase=\(audioSetup.phase.logName) afterMs=\(nowMs - audioSetup.phaseStartedAtMs) trigger=\(trigger) glassesReport=\(audioSetup.glassesReportsClassicLink ? "a2dp:\(audioSetup.glassesA2dp),hfp:\(audioSetup.glassesHfp)" : "none") phoneAudio=\(phoneAudioUp())"
|
|
7073
|
+
)
|
|
6052
7074
|
}
|
|
6053
7075
|
}
|
|
6054
7076
|
|
|
@@ -6152,6 +7174,7 @@ extension MentraLive {
|
|
|
6152
7174
|
|
|
6153
7175
|
/// Sends phone_ready to (re)start the glasses-driven readiness flow.
|
|
6154
7176
|
private func sendPhoneReady(reason: String) {
|
|
7177
|
+
pairFlow("phone_ready", "reason=\(reason) gatt=\(txCharacteristic != nil) id=\(connectedPeripheral?.identifier.uuidString ?? "-")")
|
|
6155
7178
|
Bridge.log("LIVE: 📱 Sending phone_ready to glasses (\(reason)) - waiting for glasses_ready response")
|
|
6156
7179
|
let readyMsg: [String: Any] = [
|
|
6157
7180
|
"type": "phone_ready",
|