@mentra/bluetooth-sdk 3.5.0-dev.1518 → 3.5.0-dev.1519

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/MIGRATION.md +9 -0
  2. package/README.md +64 -11
  3. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
  4. package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +10 -0
  5. package/build/BluetoothSdk.types.d.ts +23 -5
  6. package/build/BluetoothSdk.types.d.ts.map +1 -1
  7. package/build/BluetoothSdk.types.js +5 -0
  8. package/build/BluetoothSdk.types.js.map +1 -1
  9. package/build/_internal.d.ts +1 -0
  10. package/build/_internal.d.ts.map +1 -1
  11. package/build/_internal.js +1 -0
  12. package/build/_internal.js.map +1 -1
  13. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  14. package/build/_private/BluetoothSdkModule.js +20 -9
  15. package/build/_private/BluetoothSdkModule.js.map +1 -1
  16. package/build/_private/MentraLocalNetworkModule.d.ts +21 -0
  17. package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
  18. package/build/_private/MentraLocalNetworkModule.js.map +1 -1
  19. package/build/generated/releaseMetadata.js +5 -5
  20. package/build/generated/releaseMetadata.js.map +1 -1
  21. package/build/glasses-wifi/connection.d.ts +6 -0
  22. package/build/glasses-wifi/connection.d.ts.map +1 -0
  23. package/build/glasses-wifi/connection.js +44 -0
  24. package/build/glasses-wifi/connection.js.map +1 -0
  25. package/build/glasses-wifi/continuous.d.ts +39 -0
  26. package/build/glasses-wifi/continuous.d.ts.map +1 -0
  27. package/build/glasses-wifi/continuous.js +151 -0
  28. package/build/glasses-wifi/continuous.js.map +1 -0
  29. package/build/glasses-wifi/files.d.ts +11 -0
  30. package/build/glasses-wifi/files.d.ts.map +1 -0
  31. package/build/glasses-wifi/files.js +33 -0
  32. package/build/glasses-wifi/files.js.map +1 -0
  33. package/build/glasses-wifi/index.d.ts +20 -0
  34. package/build/glasses-wifi/index.d.ts.map +1 -0
  35. package/build/glasses-wifi/index.js +15 -0
  36. package/build/glasses-wifi/index.js.map +1 -0
  37. package/build/glasses-wifi/link.d.ts +31 -0
  38. package/build/glasses-wifi/link.d.ts.map +1 -0
  39. package/build/glasses-wifi/link.js +71 -0
  40. package/build/glasses-wifi/link.js.map +1 -0
  41. package/build/glasses-wifi/native.d.ts +63 -0
  42. package/build/glasses-wifi/native.d.ts.map +1 -0
  43. package/build/glasses-wifi/native.js +138 -0
  44. package/build/glasses-wifi/native.js.map +1 -0
  45. package/build/glasses-wifi/service.d.ts +86 -0
  46. package/build/glasses-wifi/service.d.ts.map +1 -0
  47. package/build/glasses-wifi/service.js +497 -0
  48. package/build/glasses-wifi/service.js.map +1 -0
  49. package/build/index.d.ts +4 -2
  50. package/build/index.d.ts.map +1 -1
  51. package/build/index.js +24 -6
  52. package/build/index.js.map +1 -1
  53. package/build/ota-transport/index.d.ts +2 -43
  54. package/build/ota-transport/index.d.ts.map +1 -1
  55. package/build/ota-transport/index.js +3 -49
  56. package/build/ota-transport/index.js.map +1 -1
  57. package/build/react/useBluetoothStatus.d.ts.map +1 -1
  58. package/build/react/useBluetoothStatus.js +5 -24
  59. package/build/react/useBluetoothStatus.js.map +1 -1
  60. package/build/react/useMentraBluetooth.d.ts +1 -2
  61. package/build/react/useMentraBluetooth.d.ts.map +1 -1
  62. package/build/react/useMentraBluetooth.js +0 -1
  63. package/build/react/useMentraBluetooth.js.map +1 -1
  64. package/expo-module.config.json +1 -1
  65. package/ios/MentraBluetoothSDK.podspec +5 -4
  66. package/ios/MentraLocalNetworkModule.swift +264 -0
  67. package/ios/Source/BluetoothSdkDefaults.swift +1 -1
  68. package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
  69. package/ios/Source/hotspot/GlassesHotspotNetwork.swift +563 -0
  70. package/ios/Source/hotspot/HotspotAccessPurpose.swift +5 -0
  71. package/ios/Source/hotspot/HotspotAddressPolicy.swift +50 -0
  72. package/ios/Source/hotspot/HotspotInterfacePolicy.swift +155 -0
  73. package/ios/Source/hotspot/HotspotJoinFailure.swift +27 -0
  74. package/ios/Source/hotspot/HotspotLeaveCompletion.swift +24 -0
  75. package/ios/Source/hotspot/HotspotNetworkOwnership.swift +22 -0
  76. package/ios/Source/hotspot/HotspotReapplyPolicy.swift +26 -0
  77. package/ios/Source/hotspot/LocalNetworkAccessRequest.swift +160 -0
  78. package/ios/Source/hotspot/MacHotspotAssociation.swift +415 -0
  79. package/ios/Source/hotspot/MacHotspotAuthorization.swift +126 -0
  80. package/ios/Source/hotspot/MacHotspotAuthorizationFacts.swift +51 -0
  81. package/ios/Source/hotspot/MacHotspotCleanupContinuation.swift +35 -0
  82. package/ios/Tests/HotspotAddressPolicyTests.swift +30 -0
  83. package/ios/Tests/HotspotInterfacePolicyTests.swift +304 -0
  84. package/ios/Tests/HotspotJoinFailureTests.swift +37 -0
  85. package/ios/Tests/HotspotLeaveCompletionTests.swift +29 -0
  86. package/ios/Tests/HotspotReapplyPolicyTests.swift +18 -0
  87. package/ios/Tests/LocalNetworkAccessRequestTests.swift +237 -0
  88. package/ios/Tests/MacHotspotAssociationTests.swift +521 -0
  89. package/ios/Tests/MacHotspotAuthorizationFactsTests.swift +62 -0
  90. package/ios/Tests/MacHotspotAuthorizationTests.swift +348 -0
  91. package/ios/Tests/MacWifiInvocationTests.swift +76 -0
  92. package/package.json +7 -2
  93. package/plugin/build/withIos.d.ts +4 -0
  94. package/plugin/build/withIos.js +20 -1
  95. package/src/BluetoothSdk.types.ts +27 -5
  96. package/src/_internal.ts +1 -0
  97. package/src/_private/BluetoothSdkModule.ts +20 -10
  98. package/src/_private/MentraLocalNetworkModule.ts +22 -0
  99. package/src/generated/releaseMetadata.ts +5 -5
  100. package/src/glasses-wifi/connection.ts +47 -0
  101. package/src/glasses-wifi/continuous.ts +177 -0
  102. package/src/glasses-wifi/files.ts +32 -0
  103. package/src/glasses-wifi/index.ts +24 -0
  104. package/src/glasses-wifi/link.ts +94 -0
  105. package/src/glasses-wifi/native.ts +193 -0
  106. package/src/glasses-wifi/service.ts +554 -0
  107. package/src/index.ts +34 -10
  108. package/src/ota-transport/index.ts +3 -85
  109. package/src/react/useBluetoothStatus.ts +5 -26
  110. package/src/react/useMentraBluetooth.ts +0 -3
@@ -38,10 +38,10 @@ Pod::Spec.new do |s|
38
38
  }
39
39
 
40
40
  # iOS frameworks required by Bluetooth SDK
41
- ios_frameworks = ['AVFoundation', 'CoreBluetooth', 'UIKit', 'CoreGraphics', 'ImageIO']
42
- ios_frameworks << 'Network' if include_expo_adapter
41
+ # Network, NetworkExtension and CoreLocation: the glasses hotspot join (Source/hotspot).
42
+ ios_frameworks = ['AVFoundation', 'CoreBluetooth', 'UIKit', 'CoreGraphics', 'ImageIO', 'Network', 'NetworkExtension', 'CoreLocation']
43
43
  s.ios.frameworks = ios_frameworks
44
- s.osx.frameworks = ['AVFoundation', 'CoreBluetooth', 'CoreAudio', 'AudioToolbox', 'ImageIO', 'JavaScriptCore', 'Network']
44
+ s.osx.frameworks = ['AVFoundation', 'CoreBluetooth', 'CoreAudio', 'AudioToolbox', 'ImageIO', 'JavaScriptCore', 'Network', 'CoreLocation']
45
45
 
46
46
  # System libraries required by MentraOS
47
47
  s.ios.libraries = 'bz2', 'z'
@@ -76,7 +76,8 @@ Pod::Spec.new do |s|
76
76
  "LocalIPv4.swift",
77
77
  "LocalOtaServer.swift",
78
78
  "BackgroundOtaArtifactDownloader.swift",
79
- "MentraOtaServerModule.swift"
79
+ "MentraOtaServerModule.swift",
80
+ "MentraLocalNetworkModule.swift"
80
81
  ]
81
82
  native_source_files.concat(adapter_source_files) if include_expo_adapter
82
83
  s.ios.source_files = native_source_files
@@ -0,0 +1,264 @@
1
+ import ExpoModulesCore
2
+ import Foundation
3
+
4
+ /// MentraOS-internal transport for glasses-local traffic, the iOS side of Android's module of the
5
+ /// same name. The glasses Wi-Fi service joins `GlassesHotspotNetwork.shared` here, once for every
6
+ /// consumer; Mentra Call and the managed relay read the address it verified.
7
+ public final class MentraLocalNetworkModule: Module {
8
+ private let queue = DispatchQueue(label: "com.mentra.bluetoothsdk.local-network")
9
+ /// The join id that holds the shared network, from join until its leave finishes.
10
+ private var hotspotOwner: String?
11
+ private lazy var downloads = LocalDownloads { [weak self] body in self?.sendEvent("downloadProgress", body) }
12
+
13
+ public func definition() -> ModuleDefinition {
14
+ Name("MentraLocalNetwork")
15
+ Events("hotspotPermissionRequired", "hotspotLost", "downloadProgress")
16
+
17
+ // Where the glasses Wi-Fi service saves files it fetches, such as deferred photos.
18
+ Function("cacheDirectory") { () -> String in
19
+ FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.path ?? NSTemporaryDirectory()
20
+ }
21
+
22
+ // The service's timers: JS timers pause while the app is in the background.
23
+ AsyncFunction("delay") { (ms: Int, promise: Promise) in
24
+ DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .milliseconds(max(0, ms))) {
25
+ promise.resolve(nil)
26
+ }
27
+ }
28
+
29
+ AsyncFunction("joinHotspot") { (id: String, ssid: String, password: String, gateway: String?, purpose: String, frequency: Int?, promise: Promise) in
30
+ self.queue.async { self.join(id: id, ssid: ssid, password: password, gateway: gateway, purpose: purpose, frequency: frequency, promise: promise) }
31
+ }
32
+
33
+ AsyncFunction("leaveHotspot") { (id: String, promise: Promise) in
34
+ self.queue.async { self.leave(id: id, promise: promise) }
35
+ }
36
+
37
+ AsyncFunction("download") { (requestId: String, url: String, destination: String, headers: [String: String],
38
+ connectionTimeoutMs: Int, readTimeoutMs: Int, promise: Promise) in
39
+ self.downloads.start(requestId: requestId, url: url, destination: destination, headers: headers,
40
+ connectionTimeoutMs: connectionTimeoutMs, readTimeoutMs: readTimeoutMs, promise: promise)
41
+ }
42
+
43
+ AsyncFunction("cancel") { (requestId: String) in
44
+ self.downloads.cancel(requestId)
45
+ }
46
+
47
+ OnDestroy {
48
+ self.downloads.invalidate()
49
+ #if os(iOS)
50
+ self.queue.async {
51
+ guard self.hotspotOwner != nil else { return }
52
+ GlassesHotspotNetwork.shared.leave { _ in }
53
+ }
54
+ #endif
55
+ }
56
+ }
57
+
58
+ private func join(id: String, ssid: String, password: String, gateway: String?, purpose: String, frequency: Int?, promise: Promise) {
59
+ #if os(iOS)
60
+ guard hotspotOwner == nil else {
61
+ promise.reject(HotspotError("Previous hotspot session has not been released")); return
62
+ }
63
+ guard let accessPurpose = HotspotAccessPurpose(rawValue: purpose) else {
64
+ promise.reject(HotspotError("Invalid hotspot access purpose")); return
65
+ }
66
+ hotspotOwner = id
67
+ let hotspot = GlassesHotspotNetwork.shared
68
+ // The join waits while Local Network access is pending or denied; the caller explains it.
69
+ hotspot.onPermissionRequired = { [weak self] systemAnswer in
70
+ self?.sendEvent("hotspotPermissionRequired", ["id": id, "systemAnswer": systemAnswer])
71
+ }
72
+ hotspot.onLost = { [weak self] reason in
73
+ self?.sendEvent("hotspotLost", ["id": id, "reason": reason])
74
+ }
75
+ hotspot.join(ssid: ssid, passphrase: password, gateway: gateway, frequency: frequency, purpose: accessPurpose) { result in
76
+ switch result {
77
+ case let .success(address): promise.resolve(address)
78
+ case let .failure(error):
79
+ let failure = HotspotJoinFailure(error)
80
+ promise.reject(Exception(name: "HotspotJoinFailure", description: failure.message, code: failure.code))
81
+ }
82
+ }
83
+ #else
84
+ promise.reject(HotspotError("Joining the glasses hotspot needs iOS"))
85
+ #endif
86
+ }
87
+
88
+ private func leave(id: String, promise: Promise) {
89
+ #if os(iOS)
90
+ guard hotspotOwner == id else { promise.resolve(nil); return }
91
+ GlassesHotspotNetwork.shared.leave { result in
92
+ self.queue.async {
93
+ switch result {
94
+ case .success:
95
+ if self.hotspotOwner == id { self.hotspotOwner = nil }
96
+ promise.resolve(nil)
97
+ case let .failure(error): promise.reject(error)
98
+ }
99
+ }
100
+ }
101
+ #else
102
+ promise.resolve(nil)
103
+ #endif
104
+ }
105
+ }
106
+
107
+ /// HTTP GETs to the glasses written straight to a file, with Android's `download` contract:
108
+ /// a non-2xx status fails, `downloadProgress` reports the status and headers before the first
109
+ /// body byte and then every chunk, and the result is `{statusCode, bytesWritten, headers}`.
110
+ private final class LocalDownloads: NSObject, URLSessionDataDelegate {
111
+ private final class Transfer {
112
+ let requestId: String
113
+ let destination: URL
114
+ let promise: Promise
115
+ var file: FileHandle?
116
+ var statusCode = 0
117
+ var headers: [String: String] = [:]
118
+ var contentLength: Int64 = -1
119
+ var bytesWritten: Int64 = 0
120
+ var failure: String?
121
+ var connectTimer: DispatchWorkItem?
122
+
123
+ init(requestId: String, destination: URL, promise: Promise) {
124
+ self.requestId = requestId; self.destination = destination; self.promise = promise
125
+ }
126
+ }
127
+
128
+ private let progress: ([String: Any]) -> Void
129
+ private let delegateQueue: OperationQueue = {
130
+ let queue = OperationQueue()
131
+ queue.maxConcurrentOperationCount = 1
132
+ queue.name = "com.mentra.bluetoothsdk.local-download"
133
+ return queue
134
+ }()
135
+
136
+ private lazy var session: URLSession = {
137
+ let configuration = URLSessionConfiguration.ephemeral
138
+ configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
139
+ configuration.urlCache = nil
140
+ configuration.waitsForConnectivity = false
141
+ return URLSession(configuration: configuration, delegate: self, delegateQueue: delegateQueue)
142
+ }()
143
+
144
+ private let lock = NSLock()
145
+ private var tasks: [String: URLSessionDataTask] = [:]
146
+ private var transfers: [Int: Transfer] = [:]
147
+
148
+ init(progress: @escaping ([String: Any]) -> Void) {
149
+ self.progress = progress
150
+ }
151
+
152
+ func start(requestId: String, url: String, destination: String, headers: [String: String],
153
+ connectionTimeoutMs: Int, readTimeoutMs: Int, promise: Promise)
154
+ {
155
+ guard let source = URL(string: url), source.scheme == "http" || source.scheme == "https" else {
156
+ promise.reject("ERR_LOCAL_DOWNLOAD", "Invalid download URL"); return
157
+ }
158
+ var request = URLRequest(url: source)
159
+ request.httpMethod = "GET"
160
+ // URLSession's request timeout is an idle timeout between packets: Android's read timeout.
161
+ request.timeoutInterval = TimeInterval(max(1, readTimeoutMs)) / 1000
162
+ headers.forEach { request.setValue($1, forHTTPHeaderField: $0) }
163
+ let task = session.dataTask(with: request)
164
+ let transfer = Transfer(requestId: requestId, destination: URL(fileURLWithPath: destination), promise: promise)
165
+ // Android's connect timeout: no response headers by then fails the download.
166
+ let timer = DispatchWorkItem { [weak self, weak task] in
167
+ guard let self, let task else { return }
168
+ self.delegateQueue.addOperation {
169
+ guard let transfer = self.transfers[task.taskIdentifier], transfer.statusCode == 0 else { return }
170
+ transfer.failure = "Connection timed out"
171
+ task.cancel()
172
+ }
173
+ }
174
+ transfer.connectTimer = timer
175
+ lock.lock()
176
+ tasks.removeValue(forKey: requestId)?.cancel()
177
+ tasks[requestId] = task
178
+ lock.unlock()
179
+ delegateQueue.addOperation {
180
+ self.transfers[task.taskIdentifier] = transfer
181
+ DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .milliseconds(max(1, connectionTimeoutMs)), execute: timer)
182
+ task.resume()
183
+ }
184
+ }
185
+
186
+ func cancel(_ requestId: String) {
187
+ lock.lock()
188
+ let task = tasks.removeValue(forKey: requestId)
189
+ lock.unlock()
190
+ task?.cancel()
191
+ }
192
+
193
+ func invalidate() {
194
+ session.invalidateAndCancel()
195
+ }
196
+
197
+ func urlSession(_: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse,
198
+ completionHandler: @escaping (URLSession.ResponseDisposition) -> Void)
199
+ {
200
+ guard let transfer = transfers[dataTask.taskIdentifier] else { completionHandler(.cancel); return }
201
+ transfer.connectTimer?.cancel()
202
+ guard let http = response as? HTTPURLResponse else {
203
+ transfer.failure = "Not an HTTP response"; completionHandler(.cancel); return
204
+ }
205
+ transfer.statusCode = http.statusCode
206
+ guard (200 ... 299).contains(http.statusCode) else {
207
+ transfer.failure = "HTTP \(http.statusCode)"; completionHandler(.cancel); return
208
+ }
209
+ var headers: [String: String] = [:]
210
+ for (key, value) in http.allHeaderFields {
211
+ if let key = key as? String { headers[key] = "\(value)" }
212
+ }
213
+ transfer.headers = headers
214
+ transfer.contentLength = http.expectedContentLength
215
+ do {
216
+ try FileManager.default.createDirectory(at: transfer.destination.deletingLastPathComponent(), withIntermediateDirectories: true)
217
+ guard FileManager.default.createFile(atPath: transfer.destination.path, contents: nil) else {
218
+ throw HotspotError("Could not create \(transfer.destination.lastPathComponent)")
219
+ }
220
+ transfer.file = try FileHandle(forWritingTo: transfer.destination)
221
+ } catch {
222
+ transfer.failure = error.localizedDescription; completionHandler(.cancel); return
223
+ }
224
+ progress([
225
+ "requestId": transfer.requestId,
226
+ "bytesWritten": 0,
227
+ "contentLength": transfer.contentLength,
228
+ "statusCode": transfer.statusCode,
229
+ "headers": headers,
230
+ ])
231
+ completionHandler(.allow)
232
+ }
233
+
234
+ func urlSession(_: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
235
+ guard let transfer = transfers[dataTask.taskIdentifier], let file = transfer.file, transfer.failure == nil else { return }
236
+ do {
237
+ try file.write(contentsOf: data)
238
+ } catch {
239
+ transfer.failure = error.localizedDescription
240
+ dataTask.cancel()
241
+ return
242
+ }
243
+ transfer.bytesWritten += Int64(data.count)
244
+ progress(["requestId": transfer.requestId, "bytesWritten": transfer.bytesWritten, "contentLength": transfer.contentLength])
245
+ }
246
+
247
+ func urlSession(_: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
248
+ guard let transfer = transfers.removeValue(forKey: task.taskIdentifier) else { return }
249
+ transfer.connectTimer?.cancel()
250
+ try? transfer.file?.close()
251
+ lock.lock()
252
+ if tasks[transfer.requestId] === task { tasks.removeValue(forKey: transfer.requestId) }
253
+ lock.unlock()
254
+ if let message = transfer.failure ?? error?.localizedDescription {
255
+ transfer.promise.reject("ERR_LOCAL_DOWNLOAD", message)
256
+ } else {
257
+ transfer.promise.resolve([
258
+ "statusCode": transfer.statusCode,
259
+ "bytesWritten": transfer.bytesWritten,
260
+ "headers": transfer.headers,
261
+ ])
262
+ }
263
+ }
264
+ }
@@ -17,7 +17,7 @@ enum BluetoothSdkDefaults {
17
17
  /// persist.mentra.wear_detection_off=1.
18
18
  static let autoPowerOffEnabled = true
19
19
  private static let infoSdkVersionKey = "MentraBluetoothSdkVersion"
20
- private static let swiftPackageSdkVersion = "3.5.0-dev.1518"
20
+ private static let swiftPackageSdkVersion = "3.5.0-dev.1519"
21
21
  private static let swiftPackageSdkVersionPlaceholder = "__MENTRA" + "_BLUETOOTH_SDK_VERSION__"
22
22
 
23
23
  private static func normalizedSdkVersion(_ value: String?) -> String? {
@@ -3,9 +3,9 @@ import Foundation
3
3
  /// Generated by release CI. Do not edit in a release checkout.
4
4
  enum GeneratedReleaseMetadata {
5
5
  static let familyBaseVersion = "3.5.0"
6
- static let releaseIdentity = "3.5.0-dev.1518"
7
- static let releaseSetId = "mentra-3.5.0-dev.1518"
8
- static let sourceCommit = "666b7fc139108169a874e0d76ce563209c428602"
9
- static let otaManifestUrl = "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.1518.json"
10
- static let otaManifestSha256 = "2c8c1306b29194991049a6681612aab422a28f5ed1cec6d2a0b45225f9a33bbb"
6
+ static let releaseIdentity = "3.5.0-dev.1519"
7
+ static let releaseSetId = "mentra-3.5.0-dev.1519"
8
+ static let sourceCommit = "941b66acac0b51cbec181e3b7fee730339118b0b"
9
+ static let otaManifestUrl = "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.1519.json"
10
+ static let otaManifestSha256 = "dc250e0efe84f8f59e8097d7210a4797f1557500a3c5929cb6ea823acb1c57e3"
11
11
  }