File size: 13,938 Bytes
914f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# React Native Hooks Proposal For The Bluetooth SDK

This document started as a proposal for making the React Native SDK feel more
React-native without hiding the underlying Bluetooth lifecycle. The stable hook
set below is now implemented under:

```ts

import {

  useBluetoothEvent,

  useBluetoothScan,

  useBluetoothStatus,

  useGlassesConnection,

} from '@mentra/bluetooth-sdk/react';

```

It is based on the current React Native starter app in the Partner Kit:

```text

Mentra-Bluetooth-SDK-Partner-Kit/examples/react-native/src/useMentraSdk.ts

```

The starter app is useful as a complete demo, but it is also evidence that
customers will reimplement the same subscription, cleanup, scan, connect,
request/response, and timer patterns unless the SDK gives them small React
building blocks.

## Current Pain Points

- The example app has one large `useMentraSdk()` hook that owns unrelated
  concerns: SDK status, scan results, default-device persistence, photo upload,
  streaming, Wi-Fi, hotspot, mic recording, audio playback, RGB LED controls,
  and console logging.
- Every app must currently remember to load initial status with
  `getGlassesStatus()` and `getBluetoothStatus()`, then separately subscribe to
  `onGlassesStatus()` and `onBluetoothStatus()`, merge partial updates, and
  remove listeners on unmount.
- Device picker flows need scan state, incremental scan results, selected
  device tracking, timeout handling, and cleanup. The current `scan(...)`
  helper is good, but React apps still need lifecycle state around it.
- Connection flows need consistent `connecting`, `connected`, `disconnecting`,
  and `error` state, plus optional default-device persistence.
- Photo capture and streaming are request/response lifecycles. Apps need to
  create request IDs, correlate events, handle timeouts, stop timers, and clean
  up on unmount.
- Microphone recording is easy to start with `setMicState(...)`, but React apps
  need a safe way to collect PCM frames and stop recording without leaking event
  listeners.

## Design Principles

- Keep the root SDK imperative and language-neutral:
  `import BluetoothSdk from '@mentra/bluetooth-sdk'`.
- Add React helpers under an explicit subpath:
  `import {useGlassesConnection} from '@mentra/bluetooth-sdk/react'`.
- Hooks should be thin lifecycle helpers, not a full application framework.
- Hooks should compose. A developer can use only `useBluetoothStatus()` or use a
  higher-level `useGlassesConnection()` that composes scan and connect state.
- Hooks should not own app-specific infrastructure such as local demo servers,
  MediaMTX reachability, Expo audio playback, app analytics, or cloud auth.
- Hooks should make cleanup the default: stop scans, remove listeners, clear
  timers, and stop keep-alives on unmount when the hook created them.
- Hooks should expose SDK-native types. Do not invent a parallel React-only
  status model unless it removes an impossible state.

## Proposed Package Shape

Add a React subpath:

```json

{

  "exports": {

    ".": {

      "types": "./build/index.d.ts",

      "react-native": "./src/index.ts",

      "default": "./build/index.js"

    },

    "./react": {

      "types": "./build/react/index.d.ts",

      "react-native": "./src/react/index.ts",

      "default": "./build/react/index.js"

    }

  }

}

```

Implemented source layout:

```text

mobile/modules/bluetooth-sdk/src/react/index.ts

mobile/modules/bluetooth-sdk/src/react/useBluetoothEvent.ts

mobile/modules/bluetooth-sdk/src/react/useBluetoothStatus.ts

mobile/modules/bluetooth-sdk/src/react/useBluetoothScan.ts

mobile/modules/bluetooth-sdk/src/react/useGlassesConnection.ts

```

The SDK already has `react` as a peer dependency, so the subpath does not add a
new conceptual runtime requirement for React Native consumers.

## Proposed Hooks

### `useBluetoothEvent`

Low-level typed event subscription with automatic cleanup.

```ts

function useBluetoothEvent<EventName extends BluetoothSdkEventName>(

  eventName: EventName,

  listener: BluetoothSdkEventListener<EventName>,

  options?: {enabled?: boolean},

): void

```

Example:

```ts

useBluetoothEvent('button_press', (event) => {

  console.log(event.buttonId, event.pressType);

});

```

This removes the repeated `addListener(...); return subscription.remove` pattern
without hiding the event system.

### `useBluetoothStatus`

Loads initial status, subscribes to partial updates, and returns merged status.

```ts

function useBluetoothStatus(options?: {

  enabled?: boolean;

  onError?: (error: unknown) => void;

}): {

  glassesStatus: Partial<GlassesStatus>;

  bluetoothStatus: Partial<BluetoothStatus>;

  connected: boolean;

  error: unknown | null;

  loading: boolean;

  ready: boolean;

  refresh: () => Promise<void>;

}

```

This replaces the repeated combination of:

```ts

await BluetoothSdk.getGlassesStatus();

await BluetoothSdk.getBluetoothStatus();

BluetoothSdk.onGlassesStatus(...);

BluetoothSdk.onBluetoothStatus(...);

```

It should use `createDisconnectedGlassesStatus()` as the initial glasses state.

### `useBluetoothScan`

React lifecycle wrapper around `BluetoothSdk.scan(...)`.

```ts

function useBluetoothScan(options?: {

  model?: DeviceModel;

  timeoutMs?: number;

  dedupe?: 'id' | 'name' | ((device: Device) => string);

  onError?: (error: unknown) => void;

}): {

  devices: Device[];

  scanning: boolean;

  error: unknown | null;

  model: DeviceModel;

  setModel: (model: DeviceModel) => void;

  selectedDevice: Device | null;

  selectDevice: (device: Device | null) => void;

  startScan: (model?: DeviceModel) => Promise<Device[]>;

  stopScan: () => Promise<void>;

  clearResults: () => void;

}

```

Why both a returned `Promise<Device[]>` and live `devices` state:

- `devices` updates as scan results arrive, so the UI can show a picker during
  the scan.
- The returned promise resolves with the final scan result list, so submit-style
  code can do one thing after scanning completes.

This is the React version of the lower-level SDK `scan(model, {onResults})`
helper rather than a replacement for it.

### `useGlassesConnection`

Common connection state and actions, optionally composed with `useBluetoothScan`.

```ts

type DefaultDeviceStorage = {

  load: () => Promise<Device | null>;

  save: (device: Device | null) => Promise<void>;

};



function useGlassesConnection(options?: {

  scanModel?: DeviceModel;

  scanTimeoutMs?: number;

  autoConnectDefault?: boolean;

  defaultDeviceStorage?: DefaultDeviceStorage;

  onError?: (error: unknown) => void;

}): {

  glassesStatus: Partial<GlassesStatus>;

  bluetoothStatus: Partial<BluetoothStatus>;

  connected: boolean;

  ready: boolean;

  busy: boolean;

  action: 'idle' | 'scanning' | 'connecting' | 'disconnecting' | 'forgetting';

  error: unknown | null;

  defaultDevice: Device | null;

  scan: ReturnType<typeof useBluetoothScan>;

  connect: (device?: Device) => Promise<void>;

  connectDefault: () => Promise<void>;

  disconnect: () => Promise<void>;

  forget: () => Promise<void>;

  clearDefaultDevice: () => Promise<void>;

  setDefaultDevice: (device: Device | null) => Promise<void>;

}

```

The storage adapter keeps the SDK from choosing AsyncStorage, MMKV, SecureStore,
or a filesystem implementation for the customer. The starter app can pass its
current file-backed default-device storage, while customer apps can pass their
own persistence.

Basic customer usage:

```tsx

import {DeviceModels} from '@mentra/bluetooth-sdk';

import {useGlassesConnection} from '@mentra/bluetooth-sdk/react';



function DevicePicker() {

  const glasses = useGlassesConnection({

    scanModel: DeviceModels.MentraLive,

    autoConnectDefault: true,

    defaultDeviceStorage: myDeviceStorage,

  });



  return (

    <>

      <Button disabled={glasses.busy} title="Scan" onPress={() => glasses.scan.startScan()} />

      {glasses.scan.devices.map((device) => (

        <Button key={device.id} title={device.name} onPress={() => glasses.connect(device)} />

      ))}

      <Button disabled={!glasses.connected} title="Disconnect" onPress={glasses.disconnect} />

    </>

  );

}

```

### `usePhotoRequest`

Not implemented yet.

Correlates `requestPhoto(...)` calls with `photo_response` events.

```ts

function usePhotoRequest(options: {

  appId: string;

  timeoutMs?: number;

  onResponse?: (event: PhotoResponseEvent) => void;

  onError?: (error: unknown) => void;

}): {

  activeRequestId: string | null;

  response: PhotoResponseEvent | null;

  status: 'idle' | 'requesting' | 'success' | 'error' | 'timeout';

  error: unknown | null;

  requestPhoto: (request: {

    requestId?: string;

    size: PhotoSize;

    webhookUrl: string | null;

    authToken?: string | null;

    compress?: PhotoCompression;

    sound?: boolean;

  }) => Promise<string>;

  reset: () => void;

}

```

This should not start a local phone photo receiver or poll a local cloud helper.
Those are starter-kit concerns. The hook only handles the SDK request and the
SDK response event lifecycle.

### `useStreamSession`

Not implemented yet.

Owns stream start/stop state and stream status cleanup. The Bluetooth SDK
itself owns stream keep-alives for direct app streams.

```ts

function useStreamSession(options?: {

  onStatus?: (event: StreamStatusEvent) => void;

  onError?: (error: unknown) => void;

}): {

  streamId: string | null;

  active: boolean;

  starting: boolean;

  status: StreamStatusEvent | null;

  error: unknown | null;

  startStream: (request: Omit<StreamStartRequest, 'streamId'> & {

    streamId?: string;

  }) => Promise<string>;

  stopStream: () => Promise<void>;

}

```

This hook should not call `keepStreamAlive(...)`; that is SDK-owned now. The
hook should reset local stream UI state on `stopStream()`, unmount, or terminal
`stream_status` events. Preview readiness, MediaMTX status queries, and direct
in-phone WebRTC receiver setup should stay in the starter kit because they are
demo infrastructure, not Bluetooth SDK behavior.

### `useMicPcmRecorder`

Not implemented yet.

Small utility hook for the most common microphone event lifecycle.

```ts

function useMicPcmRecorder(options?: {

  bypassVad?: boolean;

  sendTranscript?: boolean;

  onFrame?: (frame: Uint8Array) => void;

  onError?: (error: unknown) => void;

}): {

  recording: boolean;

  frames: number;

  bytes: number;

  pcm: Uint8Array;

  error: unknown | null;

  start: () => Promise<void>;

  stop: () => Promise<Uint8Array>;

  reset: () => void;

}

```

This hook should collect PCM bytes from `mic_pcm` and call
`setMicState(true, true, bypassVad, sendTranscript, false)` on start and
`setMicState(false)` on stop. WAV encoding, Expo `File`, and Expo audio playback
should stay in the starter kit because they are app-platform choices.

## What Should Stay Out Of The SDK Hooks

- Android permission request UI. The SDK can document required permissions, but
  apps should decide where and how to ask users.
- Local demo cloud helpers, MediaMTX reachability checks, and direct receiver
  UI. These belong in the starter kit.
- Expo audio playback implementation. The SDK can provide speaker routing
  signals and `setOwnAppAudioPlaying(...)`, but apps play audio through normal
  React Native or Expo audio APIs.
- Customer persistence choices. Hooks can accept a storage adapter but should
  not depend on a specific storage package.
- Console/event log UI. The SDK can expose typed events; apps decide how to
  render logs.

## Migration Path For The Starter App

1. Replace initial status loading and status subscriptions with
   `useBluetoothStatus()`.
2. Replace manual scan state with `useBluetoothScan()`.
3. Replace default-device restore/autoconnect logic with
   `useGlassesConnection({defaultDeviceStorage, autoConnectDefault: true})`.
4. Replace photo request ID/timeout/listener code with `usePhotoRequest()`, but
   keep `MentraDirectReceiver` setup in the starter app.
5. Replace stream keep-alive timer code with `useStreamSession()`, but keep
   MediaMTX/direct receiver preview logic in the starter app.
6. Replace PCM listener and start/stop mic state with `useMicPcmRecorder()`,
   but keep WAV writing and playback in the starter app.

## Open Questions For Review

- Should the first public hook release include only `useBluetoothStatus`,
  `useBluetoothEvent`, `useBluetoothScan`, and `useGlassesConnection`, leaving
  photo/stream/mic hooks as starter-kit recipes until we see customer usage?
- Should `useBluetoothScan` dedupe devices by `id` only, or should it support
  a default Mentra Live name-based dedupe for iOS BLE/audio duplicate rows?
- Should `useGlassesConnection` own Android Bluetooth permission checks, or is
  a separate `useBluetoothPermissions()` hook cleaner?
- Should hooks live in `@mentra/bluetooth-sdk/react`, or should they be root
  exports from `@mentra/bluetooth-sdk` for discoverability?
- Should `useStreamSession` stop the stream automatically on unmount by default?
  This is safer for demos but could surprise an app that intentionally wants a
  stream to survive screen navigation.

## Recommendation

Start with the small stable hook set:

```ts

useBluetoothEvent

useBluetoothStatus

useBluetoothScan

useGlassesConnection

```

These directly address the most error-prone React lifecycle work and make the
device picker flow much easier without committing the SDK to app-specific photo,
stream preview, or audio playback opinions. Then add `usePhotoRequest`,
`useStreamSession`, and `useMicPcmRecorder` after the starter app proves their
shape and Claude/reviewer feedback confirms they are not hiding too much.