@mentra/bluetooth-sdk 3.5.0-dev.1517 → 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 (119) hide show
  1. package/MIGRATION.md +9 -0
  2. package/README.md +67 -12
  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/android/src/main/java/com/mentra/bluetoothsdk/camera/CameraModels.kt +9 -2
  6. package/android/src/test/java/com/mentra/bluetoothsdk/camera/PhotoRequestTest.kt +8 -0
  7. package/build/BluetoothSdk.types.d.ts +33 -7
  8. package/build/BluetoothSdk.types.d.ts.map +1 -1
  9. package/build/BluetoothSdk.types.js +5 -0
  10. package/build/BluetoothSdk.types.js.map +1 -1
  11. package/build/_internal.d.ts +1 -0
  12. package/build/_internal.d.ts.map +1 -1
  13. package/build/_internal.js +1 -0
  14. package/build/_internal.js.map +1 -1
  15. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  16. package/build/_private/BluetoothSdkModule.js +20 -9
  17. package/build/_private/BluetoothSdkModule.js.map +1 -1
  18. package/build/_private/MentraLocalNetworkModule.d.ts +21 -0
  19. package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
  20. package/build/_private/MentraLocalNetworkModule.js.map +1 -1
  21. package/build/_private/photoRequestPayload.d.ts +1 -1
  22. package/build/_private/photoRequestPayload.d.ts.map +1 -1
  23. package/build/_private/photoRequestPayload.js +4 -1
  24. package/build/_private/photoRequestPayload.js.map +1 -1
  25. package/build/generated/releaseMetadata.js +5 -5
  26. package/build/generated/releaseMetadata.js.map +1 -1
  27. package/build/glasses-wifi/connection.d.ts +6 -0
  28. package/build/glasses-wifi/connection.d.ts.map +1 -0
  29. package/build/glasses-wifi/connection.js +44 -0
  30. package/build/glasses-wifi/connection.js.map +1 -0
  31. package/build/glasses-wifi/continuous.d.ts +39 -0
  32. package/build/glasses-wifi/continuous.d.ts.map +1 -0
  33. package/build/glasses-wifi/continuous.js +151 -0
  34. package/build/glasses-wifi/continuous.js.map +1 -0
  35. package/build/glasses-wifi/files.d.ts +11 -0
  36. package/build/glasses-wifi/files.d.ts.map +1 -0
  37. package/build/glasses-wifi/files.js +33 -0
  38. package/build/glasses-wifi/files.js.map +1 -0
  39. package/build/glasses-wifi/index.d.ts +20 -0
  40. package/build/glasses-wifi/index.d.ts.map +1 -0
  41. package/build/glasses-wifi/index.js +15 -0
  42. package/build/glasses-wifi/index.js.map +1 -0
  43. package/build/glasses-wifi/link.d.ts +31 -0
  44. package/build/glasses-wifi/link.d.ts.map +1 -0
  45. package/build/glasses-wifi/link.js +71 -0
  46. package/build/glasses-wifi/link.js.map +1 -0
  47. package/build/glasses-wifi/native.d.ts +63 -0
  48. package/build/glasses-wifi/native.d.ts.map +1 -0
  49. package/build/glasses-wifi/native.js +138 -0
  50. package/build/glasses-wifi/native.js.map +1 -0
  51. package/build/glasses-wifi/service.d.ts +86 -0
  52. package/build/glasses-wifi/service.d.ts.map +1 -0
  53. package/build/glasses-wifi/service.js +497 -0
  54. package/build/glasses-wifi/service.js.map +1 -0
  55. package/build/index.d.ts +4 -2
  56. package/build/index.d.ts.map +1 -1
  57. package/build/index.js +24 -6
  58. package/build/index.js.map +1 -1
  59. package/build/ota-transport/index.d.ts +2 -43
  60. package/build/ota-transport/index.d.ts.map +1 -1
  61. package/build/ota-transport/index.js +3 -49
  62. package/build/ota-transport/index.js.map +1 -1
  63. package/build/react/useBluetoothStatus.d.ts.map +1 -1
  64. package/build/react/useBluetoothStatus.js +5 -24
  65. package/build/react/useBluetoothStatus.js.map +1 -1
  66. package/build/react/useMentraBluetooth.d.ts +1 -2
  67. package/build/react/useMentraBluetooth.d.ts.map +1 -1
  68. package/build/react/useMentraBluetooth.js +0 -1
  69. package/build/react/useMentraBluetooth.js.map +1 -1
  70. package/expo-module.config.json +1 -1
  71. package/ios/MentraBluetoothSDK.podspec +5 -4
  72. package/ios/MentraLocalNetworkModule.swift +264 -0
  73. package/ios/Source/BluetoothSdkDefaults.swift +1 -1
  74. package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
  75. package/ios/Source/camera/CameraModels.swift +7 -2
  76. package/ios/Source/hotspot/GlassesHotspotNetwork.swift +563 -0
  77. package/ios/Source/hotspot/HotspotAccessPurpose.swift +5 -0
  78. package/ios/Source/hotspot/HotspotAddressPolicy.swift +50 -0
  79. package/ios/Source/hotspot/HotspotInterfacePolicy.swift +155 -0
  80. package/ios/Source/hotspot/HotspotJoinFailure.swift +27 -0
  81. package/ios/Source/hotspot/HotspotLeaveCompletion.swift +24 -0
  82. package/ios/Source/hotspot/HotspotNetworkOwnership.swift +22 -0
  83. package/ios/Source/hotspot/HotspotReapplyPolicy.swift +26 -0
  84. package/ios/Source/hotspot/LocalNetworkAccessRequest.swift +160 -0
  85. package/ios/Source/hotspot/MacHotspotAssociation.swift +415 -0
  86. package/ios/Source/hotspot/MacHotspotAuthorization.swift +126 -0
  87. package/ios/Source/hotspot/MacHotspotAuthorizationFacts.swift +51 -0
  88. package/ios/Source/hotspot/MacHotspotCleanupContinuation.swift +35 -0
  89. package/ios/Tests/HotspotAddressPolicyTests.swift +30 -0
  90. package/ios/Tests/HotspotInterfacePolicyTests.swift +304 -0
  91. package/ios/Tests/HotspotJoinFailureTests.swift +37 -0
  92. package/ios/Tests/HotspotLeaveCompletionTests.swift +29 -0
  93. package/ios/Tests/HotspotReapplyPolicyTests.swift +18 -0
  94. package/ios/Tests/LocalNetworkAccessRequestTests.swift +237 -0
  95. package/ios/Tests/MacHotspotAssociationTests.swift +521 -0
  96. package/ios/Tests/MacHotspotAuthorizationFactsTests.swift +62 -0
  97. package/ios/Tests/MacHotspotAuthorizationTests.swift +348 -0
  98. package/ios/Tests/MacWifiInvocationTests.swift +76 -0
  99. package/ios/Tests/PhotoRequestRoutingTests.swift +7 -0
  100. package/package.json +7 -2
  101. package/plugin/build/withIos.d.ts +4 -0
  102. package/plugin/build/withIos.js +20 -1
  103. package/src/BluetoothSdk.types.ts +37 -7
  104. package/src/_internal.ts +1 -0
  105. package/src/_private/BluetoothSdkModule.ts +20 -10
  106. package/src/_private/MentraLocalNetworkModule.ts +22 -0
  107. package/src/_private/photoRequestPayload.ts +4 -1
  108. package/src/generated/releaseMetadata.ts +5 -5
  109. package/src/glasses-wifi/connection.ts +47 -0
  110. package/src/glasses-wifi/continuous.ts +177 -0
  111. package/src/glasses-wifi/files.ts +32 -0
  112. package/src/glasses-wifi/index.ts +24 -0
  113. package/src/glasses-wifi/link.ts +94 -0
  114. package/src/glasses-wifi/native.ts +193 -0
  115. package/src/glasses-wifi/service.ts +554 -0
  116. package/src/index.ts +34 -10
  117. package/src/ota-transport/index.ts +3 -85
  118. package/src/react/useBluetoothStatus.ts +5 -26
  119. package/src/react/useMentraBluetooth.ts +0 -3
@@ -0,0 +1,193 @@
1
+ import {Platform} from "react-native"
2
+
3
+ // The service is this module's only caller; it reaches the native joins through it.
4
+ // eslint-disable-next-line no-restricted-imports
5
+ import MentraLocalNetwork from "../_private/MentraLocalNetworkModule"
6
+
7
+ /**
8
+ * The phone's native joins to the glasses' Wi-Fi, under the glasses Wi-Fi service: one link at a
9
+ * time. Android joins the hotspot or the Wi-Fi Direct group through `ScopedSoftApNetwork` and
10
+ * sends requests over the `Network` it gets; iOS joins the hotspot through `GlassesHotspotNetwork`
11
+ * and its requests take the system's route to it.
12
+ */
13
+
14
+ export type JoinPurpose = "outgoing-client" | "incoming-server"
15
+
16
+ let generation = 0
17
+ /** Android: requests go over the joined `Network`, not the default one. */
18
+ let scoped = false
19
+ /** iOS: the native join's owner id, while joined or joining. */
20
+ let iosOwner: string | null = null
21
+
22
+ const native = () => {
23
+ if (!MentraLocalNetwork) throw new Error("The glasses Wi-Fi link is not available in this native build")
24
+ return MentraLocalNetwork
25
+ }
26
+
27
+ const requestId = (prefix: string) => `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2)}`
28
+
29
+ /** Android: whether the phone and the connected glasses can use the Wi-Fi Direct link now. */
30
+ export function wifiDirectAvailable(): boolean {
31
+ return Platform.OS === "android" && MentraLocalNetwork?.wifiDirectAvailable?.() === true
32
+ }
33
+
34
+ /** Join the glasses' hotspot. Resolves with the phone's address on it, when the platform reports it. */
35
+ export async function joinHotspot(
36
+ ssid: string,
37
+ password: string,
38
+ options: {
39
+ gateway?: string
40
+ /** The hotspot's channel in MHz, when the glasses report it: a Mac then scans only that channel. */
41
+ frequency?: number
42
+ purpose: JoinPurpose
43
+ /** iOS: the join has waited a few seconds for Local Network access, which may have been denied. */
44
+ onPermissionRequired?: (systemAnswer: string) => void
45
+ },
46
+ ): Promise<string | undefined> {
47
+ const current = ++generation
48
+ scoped = false
49
+ if (Platform.OS === "ios") {
50
+ if (iosOwner) throw new Error("Previous hotspot session has not been released")
51
+ const owner = requestId("ios_hotspot")
52
+ iosOwner = owner
53
+ const permission = native().addListener("hotspotPermissionRequired", (event) => {
54
+ if (event.id === owner) options.onPermissionRequired?.(event.systemAnswer ?? "unknown")
55
+ })
56
+ try {
57
+ const address = await native().joinHotspot(
58
+ owner,
59
+ ssid,
60
+ password,
61
+ options.gateway ?? null,
62
+ options.purpose,
63
+ options.frequency ?? null,
64
+ )
65
+ if (current !== generation) throw new Error("Local network connection was superseded")
66
+ return address
67
+ } catch (error) {
68
+ try {
69
+ await native().leaveHotspot(owner)
70
+ } catch (cleanupError) {
71
+ // The owner stays recorded: the next join must not start over a session still being left.
72
+ console.warn("[GlassesWifi] Leaving a failed hotspot join failed", cleanupError)
73
+ throw error
74
+ }
75
+ if (iosOwner === owner) iosOwner = null
76
+ throw error
77
+ } finally {
78
+ permission.remove()
79
+ }
80
+ }
81
+ if (Platform.OS !== "android") throw new Error("The glasses hotspot cannot be joined on this platform")
82
+ const result = await native().connect(ssid, password, options.purpose === "incoming-server")
83
+ scoped = true
84
+ return result.localAddress
85
+ }
86
+
87
+ /** Android: join the glasses' Wi-Fi Direct group; the native join asks the glasses for it over Bluetooth. */
88
+ export async function joinWifiDirect(
89
+ purpose: JoinPurpose,
90
+ ): Promise<{ssid: string; localAddress?: string; gateway?: string}> {
91
+ generation += 1
92
+ scoped = false
93
+ const result = await native().connectWifiDirect(purpose === "incoming-server")
94
+ scoped = true
95
+ return result
96
+ }
97
+
98
+ /** Leave whatever the phone joined, or stop a join in progress. Leaving a group also removes it. */
99
+ export async function leave(): Promise<void> {
100
+ generation += 1
101
+ if (!MentraLocalNetwork) return
102
+ if (Platform.OS === "ios") {
103
+ const owner = iosOwner
104
+ if (!owner) return
105
+ await MentraLocalNetwork.leaveHotspot(owner)
106
+ if (iosOwner === owner) iosOwner = null
107
+ return
108
+ }
109
+ // A failed release keeps the network for the next leave.
110
+ await MentraLocalNetwork.disconnect()
111
+ scoped = false
112
+ }
113
+
114
+ /** The phone left the glasses' network without being asked to. */
115
+ export function onLost(listener: () => void): () => void {
116
+ if (!MentraLocalNetwork) return () => {}
117
+ const subscription =
118
+ Platform.OS === "ios"
119
+ ? MentraLocalNetwork.addListener("hotspotLost", (event) => {
120
+ if (event.id === iosOwner) listener()
121
+ })
122
+ : MentraLocalNetwork.addListener("networkLost", () => {
123
+ scoped = false
124
+ listener()
125
+ })
126
+ return () => subscription.remove()
127
+ }
128
+
129
+ /** Android: requests to the glasses must go over the joined `Network` (`MentraLocalNetwork.request`). */
130
+ export function isScoped(): boolean {
131
+ return scoped
132
+ }
133
+
134
+ /** Android: an HTTP request to the glasses over the joined `Network`; the body comes back as base64. */
135
+ export function request(
136
+ requestId: string,
137
+ url: string,
138
+ method: string,
139
+ headers: Record<string, string>,
140
+ body: string | null,
141
+ timeoutMs: number,
142
+ ): Promise<{status: number; headers: Record<string, string>; bodyBase64: string}> {
143
+ return native().request(requestId, url, method, headers, body, timeoutMs)
144
+ }
145
+
146
+ /** A download from the glasses over the link into `destination`, a path; progress comes by `onDownloadProgress`. */
147
+ export function download(
148
+ requestId: string,
149
+ url: string,
150
+ destination: string,
151
+ headers: Record<string, string>,
152
+ connectionTimeoutMs: number,
153
+ readTimeoutMs: number,
154
+ ): Promise<{statusCode: number; bytesWritten: number; headers: Record<string, string>}> {
155
+ return native().download(requestId, url, destination, headers, connectionTimeoutMs, readTimeoutMs)
156
+ }
157
+
158
+ /** Stop a request or download in progress. */
159
+ export async function cancel(requestId: string): Promise<void> {
160
+ await MentraLocalNetwork?.cancel(requestId)
161
+ }
162
+
163
+ export function onDownloadProgress(
164
+ listener: (event: {
165
+ requestId: string
166
+ bytesWritten: number
167
+ contentLength: number
168
+ statusCode?: number
169
+ headers?: Record<string, string>
170
+ }) => void,
171
+ ): () => void {
172
+ if (!MentraLocalNetwork) return () => {}
173
+ const subscription = MentraLocalNetwork.addListener("downloadProgress", listener)
174
+ return () => subscription.remove()
175
+ }
176
+
177
+ /** Download a file from the glasses over the link into this app's cache folder, as a `file://` URI. */
178
+ export async function downloadToCache(
179
+ url: string,
180
+ fileName: string,
181
+ timeouts: {connectionMs: number; readMs: number},
182
+ ): Promise<{statusCode: number; uri: string; bytes: number}> {
183
+ const path = `${native().cacheDirectory().replace(/\/$/, "")}/${fileName}`
184
+ const {statusCode, bytesWritten} = await download(requestId("file"), url, path, {}, timeouts.connectionMs, timeouts.readMs)
185
+ return {statusCode, uri: `file://${path}`, bytes: bytesWritten}
186
+ }
187
+
188
+ /** A wait that keeps running while the app is in the background, unlike a JS timer on Android. */
189
+ export function delay(ms: number): Promise<void> {
190
+ const nativeDelay = MentraLocalNetwork?.delay
191
+ if (typeof nativeDelay === "function") return nativeDelay.call(MentraLocalNetwork, Math.max(0, Math.round(ms)))
192
+ return new Promise((resolve) => setTimeout(resolve, ms))
193
+ }