react-native-asleep 1.2.0 → 2.0.0
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/README.ko.md +374 -0
- package/README.md +263 -225
- package/android/build.gradle +1 -1
- package/android/src/main/java/ai/asleep/reactnative/AsleepModule.kt +107 -74
- package/build/Asleep.types.d.ts +18 -4
- package/build/Asleep.types.d.ts.map +1 -1
- package/build/Asleep.types.js.map +1 -1
- package/build/AsleepStore.d.ts.map +1 -1
- package/build/AsleepStore.js +46 -16
- package/build/AsleepStore.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/docs/INTEGRATION.ko.md +395 -0
- package/docs/INTEGRATION.md +442 -0
- package/ios/Asleep.podspec +1 -1
- package/ios/AsleepModule.swift +35 -14
- package/package.json +5 -3
- package/src/Asleep.types.ts +14 -5
- package/src/AsleepStore.ts +69 -16
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -1,56 +1,84 @@
|
|
|
1
1
|
# react-native-asleep
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[English](./README.md) | [한국어](./README.ko.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Sleep tracking for React Native and Expo apps, powered by Asleep's AI technology. The SDK uses the device microphone, requires no wearable, and exposes one React hook plus typed data models for iOS and Android.
|
|
6
6
|
|
|
7
|
-
This SDK is under active development. v2 has a deliberately small public surface
|
|
7
|
+
> This SDK is under active development. v2 has a deliberately small public surface. Pin exact versions and review the [changelog](./CHANGELOG.md) before upgrading.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## What is included
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
- Microphone-based sleep tracking and Android service restoration
|
|
12
|
+
- Sleep reports, session lists, average reports, and in-progress analysis
|
|
13
|
+
- A shared state model across iOS and Android
|
|
14
|
+
- Structured `AsleepError` values with recovery categories
|
|
15
|
+
- `useAsleep()` for React and a small `Asleep` API for non-React code
|
|
16
|
+
|
|
17
|
+
## Requirements
|
|
18
|
+
|
|
19
|
+
| Component | Minimum |
|
|
20
|
+
|---|---|
|
|
21
|
+
| React Native | 0.74 |
|
|
22
|
+
| React | 18.2 |
|
|
23
|
+
| iOS deployment target | 14.0 |
|
|
24
|
+
| Android `minSdkVersion` | 24 |
|
|
25
|
+
|
|
26
|
+
The example app is currently verified with React Native 0.79.2, Expo 53, React 19.0.0, and Android compile/target SDK 34.
|
|
27
|
+
|
|
28
|
+
Bundled native SDK versions. The React Native package follows its own semver; its version number does not mirror the native SDKs.
|
|
29
|
+
|
|
30
|
+
| react-native-asleep | iOS SDK | Android SDK |
|
|
31
|
+
|---|---|---|
|
|
32
|
+
| 2.0.0 | 3.3.2 | 3.3.1 |
|
|
33
|
+
| 1.0.17 – 1.2.0 | 3.2.0 | 3.2.1 |
|
|
34
|
+
| 1.0.15 | 3.2.0 | 3.2.0 |
|
|
35
|
+
| 1.0.14 | 3.1.8 | 3.1.5 |
|
|
36
|
+
| 1.0.13 | 3.1.6 | 3.1.4 |
|
|
37
|
+
|
|
38
|
+
1.2.0 already contained the breaking v2 API; 2.0.0 is the semver-correct release of that API.
|
|
12
39
|
|
|
13
40
|
## Installation
|
|
14
41
|
|
|
15
|
-
###
|
|
42
|
+
### Expo projects
|
|
16
43
|
|
|
17
44
|
```bash
|
|
18
45
|
expo install react-native-asleep
|
|
19
46
|
```
|
|
20
47
|
|
|
21
|
-
|
|
48
|
+
This package contains native modules, so create a new development or store build after installing or upgrading it. It does not run in Expo Go and cannot be upgraded through an OTA update alone.
|
|
22
49
|
|
|
23
|
-
|
|
50
|
+
### Bare React Native projects
|
|
24
51
|
|
|
25
|
-
|
|
52
|
+
This library uses the Expo Modules API. Install Expo modules once if the app does not already use them:
|
|
26
53
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
It prompts `Install the Expo CLI integration? (Y/n)` — either answer wires the bare modules. See the [Expo bare guide](https://docs.expo.dev/bare/installing-expo-modules/) for details.
|
|
54
|
+
```bash
|
|
55
|
+
npx install-expo-modules@latest
|
|
56
|
+
npm install react-native-asleep
|
|
57
|
+
```
|
|
32
58
|
|
|
33
|
-
|
|
59
|
+
Then install iOS pods:
|
|
34
60
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
61
|
+
```bash
|
|
62
|
+
cd ios
|
|
63
|
+
bundle install
|
|
64
|
+
bundle exec pod install
|
|
65
|
+
```
|
|
38
66
|
|
|
39
|
-
|
|
67
|
+
Android needs no additional linking step.
|
|
40
68
|
|
|
41
|
-
#### Static
|
|
69
|
+
#### Static frameworks
|
|
42
70
|
|
|
43
|
-
The iOS
|
|
71
|
+
The iOS pod is a static framework. Do not add `use_frameworks!` only for this package. If the app already requires it, use static linkage:
|
|
44
72
|
|
|
45
73
|
```ruby
|
|
46
74
|
use_frameworks! :linkage => :static
|
|
47
75
|
```
|
|
48
76
|
|
|
49
|
-
|
|
77
|
+
Dynamic linkage is known to break Expo SDK 55 builds; see [expo/expo#44487](https://github.com/expo/expo/issues/44487) and [expo/expo#41556](https://github.com/expo/expo/issues/41556).
|
|
50
78
|
|
|
51
|
-
####
|
|
79
|
+
#### Monorepos and workspaces
|
|
52
80
|
|
|
53
|
-
If `react-native-asleep` is hoisted to a parent `node_modules`, configure autolinking in
|
|
81
|
+
If `react-native-asleep` is hoisted to a parent `node_modules`, configure Expo autolinking in the app's `package.json`:
|
|
54
82
|
|
|
55
83
|
```json
|
|
56
84
|
{
|
|
@@ -62,49 +90,22 @@ If `react-native-asleep` is hoisted to a parent `node_modules`, configure autoli
|
|
|
62
90
|
}
|
|
63
91
|
```
|
|
64
92
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
The [example app](./example) is the reference setup. Adjacent versions are expected to work but aren't verified.
|
|
68
|
-
|
|
69
|
-
| Component | Version |
|
|
70
|
-
| --------------------------- | -------- |
|
|
71
|
-
| React Native | 0.79.2 |
|
|
72
|
-
| Expo | 53 |
|
|
73
|
-
| React | 19.0.0 |
|
|
74
|
-
| iOS deployment target | 14.0 |
|
|
75
|
-
| Android `minSdkVersion` | 24 |
|
|
76
|
-
| Android `compileSdkVersion` | 34 |
|
|
77
|
-
| Android `targetSdkVersion` | 34 |
|
|
78
|
-
|
|
79
|
-
#### Bundled native SDK versions
|
|
80
|
-
|
|
81
|
-
| Platform | Native SDK version |
|
|
82
|
-
|---|---|
|
|
83
|
-
| iOS | 3.2.0 |
|
|
84
|
-
| Android | 3.2.1 |
|
|
85
|
-
|
|
86
|
-
## Setup
|
|
93
|
+
## Native configuration
|
|
87
94
|
|
|
88
|
-
|
|
95
|
+
Create an API key in the [Asleep Dashboard](https://dashboard.asleep.ai).
|
|
89
96
|
|
|
90
|
-
|
|
91
|
-
2. Create an account and generate your API key
|
|
92
|
-
3. Note your API key for configuration
|
|
97
|
+
### iOS
|
|
93
98
|
|
|
94
|
-
|
|
99
|
+
Your app must declare microphone usage and background audio.
|
|
95
100
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
Add the microphone usage description and audio background mode.
|
|
99
|
-
|
|
100
|
-
**For Expo Managed Projects (app.json):**
|
|
101
|
+
For Expo projects:
|
|
101
102
|
|
|
102
103
|
```json
|
|
103
104
|
{
|
|
104
105
|
"expo": {
|
|
105
106
|
"ios": {
|
|
106
107
|
"infoPlist": {
|
|
107
|
-
"NSMicrophoneUsageDescription": "This app
|
|
108
|
+
"NSMicrophoneUsageDescription": "This app uses the microphone for sleep tracking.",
|
|
108
109
|
"UIBackgroundModes": ["audio"]
|
|
109
110
|
}
|
|
110
111
|
}
|
|
@@ -112,264 +113,301 @@ Add the microphone usage description and audio background mode.
|
|
|
112
113
|
}
|
|
113
114
|
```
|
|
114
115
|
|
|
115
|
-
|
|
116
|
+
For bare React Native projects, add the equivalent entries to the app's `Info.plist`:
|
|
116
117
|
|
|
117
118
|
```xml
|
|
118
119
|
<key>NSMicrophoneUsageDescription</key>
|
|
119
|
-
<string>This app
|
|
120
|
+
<string>This app uses the microphone for sleep tracking.</string>
|
|
120
121
|
<key>UIBackgroundModes</key>
|
|
121
122
|
<array>
|
|
122
123
|
<string>audio</string>
|
|
123
124
|
</array>
|
|
124
125
|
```
|
|
125
126
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
The library declares its own permissions; the manifest merger combines them into your app. No `<uses-permission>` entries are needed in your `AndroidManifest.xml`. For reference, the library declares:
|
|
127
|
+
### Android
|
|
129
128
|
|
|
130
|
-
|
|
131
|
-
<uses-permission android:name="android.permission.INTERNET" />
|
|
132
|
-
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
|
133
|
-
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
|
134
|
-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
|
135
|
-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
|
|
136
|
-
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
|
137
|
-
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
|
138
|
-
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
|
|
139
|
-
```
|
|
129
|
+
The library declares its required permissions and foreground service in its manifest. The manifest merger adds them to the app, so do not duplicate them only for this SDK.
|
|
140
130
|
|
|
141
|
-
`RECORD_AUDIO`
|
|
131
|
+
`RECORD_AUDIO` is required at runtime. On Android 13 and later, `requestRequiredPermissions()` also requests `POST_NOTIFICATIONS` so the foreground-service notification can remain visible, but notification denial does not make its return value `false`.
|
|
142
132
|
|
|
143
133
|
## Quick Start
|
|
144
134
|
|
|
135
|
+
The required startup order is:
|
|
136
|
+
|
|
137
|
+
```text
|
|
138
|
+
check for an existing Android tracking service
|
|
139
|
+
→ initialize the SDK configuration
|
|
140
|
+
→ check battery optimization
|
|
141
|
+
→ request permissions from a user action
|
|
142
|
+
→ start tracking
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Always call `initAsleepConfig()` after `checkAndRestoreTracking()`, including when an Android service session was restored. On iOS, the restoration check is a required cross-platform prerequisite but reports no persistent service.
|
|
146
|
+
|
|
145
147
|
```tsx
|
|
146
|
-
import { useEffect } from "react";
|
|
147
|
-
import { Button, Text, View } from "react-native";
|
|
148
|
+
import { useEffect, useRef, useState } from "react";
|
|
149
|
+
import { Button, Platform, Text, View } from "react-native";
|
|
148
150
|
import { AsleepError, useAsleep } from "react-native-asleep";
|
|
149
151
|
|
|
150
|
-
function SleepTracker() {
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
checkAndRestoreTracking,
|
|
157
|
-
checkBatteryOptimization,
|
|
158
|
-
hasRequiredPermissions,
|
|
159
|
-
requestRequiredPermissions,
|
|
160
|
-
startTracking,
|
|
161
|
-
stopTracking,
|
|
162
|
-
} = useAsleep();
|
|
163
|
-
|
|
164
|
-
// Required order before startTracking: configure -> restore any in-progress session -> battery check
|
|
152
|
+
export function SleepTracker() {
|
|
153
|
+
const asleep = useAsleep();
|
|
154
|
+
const hasBootstrapped = useRef(false);
|
|
155
|
+
const [isReady, setIsReady] = useState(false);
|
|
156
|
+
const [bootstrapMessage, setBootstrapMessage] = useState<string | null>(null);
|
|
157
|
+
|
|
165
158
|
useEffect(() => {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
159
|
+
if (hasBootstrapped.current) return;
|
|
160
|
+
hasBootstrapped.current = true;
|
|
161
|
+
|
|
162
|
+
async function bootstrap() {
|
|
163
|
+
await asleep.checkAndRestoreTracking();
|
|
164
|
+
|
|
165
|
+
const config = { apiKey: "YOUR_API_KEY" };
|
|
166
|
+
if (Platform.OS === "ios") {
|
|
167
|
+
let removeJoined = () => {};
|
|
168
|
+
let removeFailed = () => {};
|
|
169
|
+
const configurationFinished = new Promise<void>((resolve, reject) => {
|
|
170
|
+
removeJoined = asleep.addEventListener("onUserJoined", () => resolve());
|
|
171
|
+
removeFailed = asleep.addEventListener("onUserJoinFailed", (failure) => {
|
|
172
|
+
reject(
|
|
173
|
+
new AsleepError(
|
|
174
|
+
"USER_JOIN_FAILED",
|
|
175
|
+
failure.detail ?? failure.error ?? "User join failed.",
|
|
176
|
+
{
|
|
177
|
+
sdkCode: failure.sdkCode,
|
|
178
|
+
caseName: failure.caseName,
|
|
179
|
+
cause: failure,
|
|
180
|
+
},
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
await Promise.all([
|
|
188
|
+
asleep.initAsleepConfig(config),
|
|
189
|
+
configurationFinished,
|
|
190
|
+
]);
|
|
191
|
+
} finally {
|
|
192
|
+
removeJoined();
|
|
193
|
+
removeFailed();
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
await asleep.initAsleepConfig(config);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
await asleep.checkBatteryOptimization();
|
|
200
|
+
setIsReady(true);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
void bootstrap().catch((cause: unknown) => {
|
|
204
|
+
setBootstrapMessage(cause instanceof Error ? cause.message : String(cause));
|
|
205
|
+
});
|
|
206
|
+
}, [
|
|
207
|
+
asleep.checkAndRestoreTracking,
|
|
208
|
+
asleep.initAsleepConfig,
|
|
209
|
+
asleep.addEventListener,
|
|
210
|
+
asleep.checkBatteryOptimization,
|
|
211
|
+
]);
|
|
212
|
+
|
|
213
|
+
async function start() {
|
|
174
214
|
try {
|
|
175
|
-
if (!(await hasRequiredPermissions())) {
|
|
176
|
-
const granted = await requestRequiredPermissions();
|
|
215
|
+
if (!(await asleep.hasRequiredPermissions())) {
|
|
216
|
+
const granted = await asleep.requestRequiredPermissions();
|
|
177
217
|
if (!granted) return;
|
|
178
218
|
}
|
|
179
219
|
|
|
180
|
-
await
|
|
181
|
-
|
|
220
|
+
const battery = await asleep.checkBatteryOptimization();
|
|
221
|
+
if (!battery.exempted) {
|
|
222
|
+
await asleep.requestBatteryOptimizationExemption();
|
|
223
|
+
return; // Check again after the user returns from Android settings.
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
await asleep.startTracking({
|
|
227
|
+
android: {
|
|
228
|
+
notification: {
|
|
229
|
+
title: "Sleep tracking",
|
|
230
|
+
text: "Sleep analysis is running.",
|
|
231
|
+
},
|
|
232
|
+
},
|
|
182
233
|
});
|
|
183
|
-
} catch (cause) {
|
|
234
|
+
} catch (cause: unknown) {
|
|
184
235
|
if (!(cause instanceof AsleepError)) throw cause;
|
|
185
|
-
//
|
|
236
|
+
// The same AsleepError is also available reactively as asleep.error.
|
|
186
237
|
}
|
|
187
|
-
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function stop() {
|
|
241
|
+
try {
|
|
242
|
+
await asleep.stopTracking();
|
|
243
|
+
} catch (cause: unknown) {
|
|
244
|
+
if (!(cause instanceof AsleepError)) throw cause;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const shouldStop =
|
|
249
|
+
asleep.isTracking || asleep.error?.category === "recordingDead";
|
|
188
250
|
|
|
189
251
|
return (
|
|
190
252
|
<View>
|
|
191
|
-
<Text>Status: {status}</Text>
|
|
192
|
-
{
|
|
193
|
-
|
|
253
|
+
<Text>Status: {asleep.status}</Text>
|
|
254
|
+
{bootstrapMessage ? <Text>{bootstrapMessage}</Text> : null}
|
|
255
|
+
{asleep.error ? <Text>{asleep.error.message}</Text> : null}
|
|
256
|
+
<Button
|
|
257
|
+
title={shouldStop ? "Stop tracking" : "Start tracking"}
|
|
258
|
+
disabled={!isReady}
|
|
259
|
+
onPress={shouldStop ? stop : start}
|
|
260
|
+
/>
|
|
194
261
|
</View>
|
|
195
262
|
);
|
|
196
263
|
}
|
|
197
264
|
```
|
|
198
265
|
|
|
199
|
-
|
|
266
|
+
Call `requestRequiredPermissions()` from a user-driven interaction. `startTracking()` checks permission but never opens a permission dialog. On Android, return from the battery settings screen and call `checkBatteryOptimization()` again before retrying.
|
|
200
267
|
|
|
201
|
-
|
|
268
|
+
On iOS, `initAsleepConfig()` starts asynchronous user configuration but its bridge call can return before the native managers are ready. Register the user events before calling it and wait for `onUserJoined` or `onUserJoinFailed`, as shown above. Android resolves `initAsleepConfig()` after configuration; do not wait for `onUserJoined` there because a restored-service fast path does not emit it.
|
|
202
269
|
|
|
203
|
-
The
|
|
270
|
+
The `recordingDead` category is another deliberate exception: `isTracking` is `false`, but the native session remains open, so the action must still call `stopTracking()` before starting a new session.
|
|
271
|
+
|
|
272
|
+
For ODA (On-Device Analysis), `setup()` is the configuration entry point for a new ODA session. This Quick Start documents service mode; `setup()` cannot run while a restored session is tracking.
|
|
273
|
+
|
|
274
|
+
## Core API
|
|
204
275
|
|
|
205
276
|
```ts
|
|
206
277
|
import {
|
|
207
278
|
Asleep,
|
|
208
279
|
AsleepError,
|
|
209
280
|
useAsleep,
|
|
210
|
-
type AsleepAverageReport,
|
|
211
281
|
type AsleepReport,
|
|
212
282
|
type AsleepSession,
|
|
213
283
|
type TrackingStatus,
|
|
214
284
|
} from "react-native-asleep";
|
|
215
285
|
```
|
|
216
286
|
|
|
217
|
-
|
|
287
|
+
### Credentials
|
|
218
288
|
|
|
219
|
-
|
|
289
|
+
`setup()` and `initAsleepConfig()` accept exactly one credential form, typed as `AsleepCredentials`:
|
|
220
290
|
|
|
221
|
-
|
|
291
|
+
```ts
|
|
292
|
+
const asleep = useAsleep();
|
|
222
293
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
| `isTrackingPaused` | `true` only while `status` is `"paused"` |
|
|
228
|
-
| `isRecoveryRequired` | `true` only while `status` is `"recoveryRequired"` |
|
|
229
|
-
| `isSetupInProgress` / `isSetupComplete` | Derived setup lifecycle |
|
|
230
|
-
| `sessionId` / `userId` | Current native identifiers |
|
|
231
|
-
| `error` | Last `AsleepError`, or `null` |
|
|
232
|
-
| `analysisResult` / `isAnalyzing` | Latest analysis event and request state |
|
|
233
|
-
| `didClose` / `isODAEnabled` / `log` | Session history, mode, and opt-in debug log |
|
|
294
|
+
await asleep.initAsleepConfig({ apiKey: "YOUR_API_KEY", userId });
|
|
295
|
+
// or token authentication
|
|
296
|
+
await asleep.initAsleepConfig({ appId: "YOUR_APP_ID", appSecret: "YOUR_APP_SECRET", userId });
|
|
297
|
+
```
|
|
234
298
|
|
|
235
|
-
`
|
|
299
|
+
Mixing `apiKey` with `appId`/`appSecret` is a type error. At runtime, passing neither form, both forms, an incomplete pair, or an empty string rejects with `AsleepError` code `INVALID_ARGUMENT` before any native call. Keep the secret out of source control like the API key.
|
|
236
300
|
|
|
237
|
-
###
|
|
301
|
+
### `useAsleep()`
|
|
238
302
|
|
|
239
|
-
The
|
|
303
|
+
The primary React API. It attaches the ref-counted native listeners while mounted and returns state plus actions from the shared SDK store.
|
|
240
304
|
|
|
241
|
-
|
|
242
|
-
- Tracking: `startTracking`, `stopTracking`, `resumeTracking`
|
|
243
|
-
- Reports: `getReport`, `getReportList`, `getAverageReport`, `deleteSession`, `requestAnalysis`
|
|
244
|
-
- Prerequisites: `checkBatteryOptimization`, `requestBatteryOptimizationExemption`, `hasRequiredPermissions`, `requestRequiredPermissions`
|
|
245
|
-
- Utilities: `enableLog`, `clearError`, `addEventListener`, and the deprecated no-op `setCustomNotification`
|
|
305
|
+
Important state:
|
|
246
306
|
|
|
247
|
-
|
|
307
|
+
| Field | Meaning |
|
|
308
|
+
|---|---|
|
|
309
|
+
| `status` | `"idle"`, `"tracking"`, `"paused"`, or `"recoveryRequired"` |
|
|
310
|
+
| `isTracking` | `true` for every status except `"idle"` |
|
|
311
|
+
| `isTrackingPaused` | The native session is temporarily interrupted |
|
|
312
|
+
| `isRecoveryRequired` | iOS needs an explicit foreground `resumeTracking()` |
|
|
313
|
+
| `isSetupComplete` | SDK configuration completed |
|
|
314
|
+
| `sessionId`, `userId` | Current native identifiers |
|
|
315
|
+
| `analysisResult`, `isAnalyzing` | Latest analysis event and request state |
|
|
316
|
+
| `error` | The latest `AsleepError`, or `null` |
|
|
248
317
|
|
|
249
|
-
|
|
318
|
+
Important actions:
|
|
250
319
|
|
|
251
|
-
|
|
320
|
+
| Area | Actions |
|
|
321
|
+
|---|---|
|
|
322
|
+
| Configuration and restore | `setup`, `initAsleepConfig`, `checkAndRestoreTracking` |
|
|
323
|
+
| Prerequisites | `checkBatteryOptimization`, `requestBatteryOptimizationExemption`, `hasRequiredPermissions`, `requestRequiredPermissions` |
|
|
324
|
+
| Tracking | `startTracking`, `stopTracking`, `resumeTracking` |
|
|
325
|
+
| Reports | `getReport`, `getReportList`, `getAverageReport`, `deleteSession` |
|
|
326
|
+
| Analysis | `requestAnalysis` |
|
|
327
|
+
| Utilities | `addEventListener`, `enableLog`, `clearError` |
|
|
252
328
|
|
|
253
|
-
|
|
254
|
-
import { Asleep } from "react-native-asleep";
|
|
329
|
+
### `Asleep`
|
|
255
330
|
|
|
256
|
-
|
|
257
|
-
const teardown = Asleep.initialize();
|
|
331
|
+
Use the imperative escape hatch where React hooks are unavailable:
|
|
258
332
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
});
|
|
333
|
+
```ts
|
|
334
|
+
import { Asleep } from "react-native-asleep";
|
|
262
335
|
|
|
263
|
-
const
|
|
264
|
-
|
|
336
|
+
const releaseListeners = Asleep.initialize();
|
|
337
|
+
const unsubscribe = Asleep.subscribe((state) => {
|
|
338
|
+
console.log(state.status, state.error?.code);
|
|
265
339
|
});
|
|
266
340
|
|
|
267
|
-
|
|
341
|
+
console.log(Asleep.getState().status);
|
|
268
342
|
|
|
269
|
-
offAnalysis();
|
|
270
343
|
unsubscribe();
|
|
271
|
-
|
|
344
|
+
releaseListeners();
|
|
272
345
|
```
|
|
273
346
|
|
|
274
|
-
`initialize()` is ref-counted
|
|
275
|
-
|
|
276
|
-
On iOS, `isRecoveryRequired` becomes `true` when recording cannot resume while the app is in the background. After the app returns to the foreground, call `resumeTracking()`. The flag clears only after the next successful audio upload. `resumeTracking()` is iOS-only and rejects with `UNSUPPORTED_PLATFORM` on Android.
|
|
277
|
-
|
|
278
|
-
## Tracking lifecycle and platform compensations
|
|
279
|
-
|
|
280
|
-
The library handles a number of platform quirks internally so JS sees a consistent event model:
|
|
347
|
+
`Asleep.initialize()` is ref-counted and must be paired with its returned cleanup function. Importing the package alone does not attach native listeners.
|
|
281
348
|
|
|
282
|
-
|
|
283
|
-
- Android SDK 3.2.x emits both `onFail` and `onFinish` for fatal codes — the wrapper suppresses the spurious `onTrackingClosed` so JS doesn't misread a fatal error as a clean close.
|
|
284
|
-
- iOS interruption recovery may emit duplicate `didResume` events — the wrapper deduplicates while still clearing `error`.
|
|
285
|
-
- iOS audio initialization failure stops recording without closing the native session — stop and restart the session; `resumeTracking()` cannot recover it.
|
|
349
|
+
### Errors
|
|
286
350
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
## Error classification
|
|
290
|
-
|
|
291
|
-
Every failed action and native failure event produces an `AsleepError`, which extends `Error`:
|
|
351
|
+
Every failed action throws `AsleepError`; the same error is stored in `useAsleep().error` when applicable.
|
|
292
352
|
|
|
293
353
|
```ts
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
354
|
+
try {
|
|
355
|
+
await asleep.startTracking();
|
|
356
|
+
} catch (cause: unknown) {
|
|
357
|
+
if (cause instanceof AsleepError) {
|
|
358
|
+
console.log(cause.code, cause.category, cause.sdkCode);
|
|
359
|
+
}
|
|
300
360
|
}
|
|
301
361
|
```
|
|
302
362
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
| `category` | Meaning | Suggested app-side severity |
|
|
306
|
-
|---|---|---|
|
|
307
|
-
| `terminal` | Native session is gone; no `onTrackingClosed` will follow. Start a new session. | error |
|
|
308
|
-
| `recordingDead` | Recorder torn down but the session is still open; `stopTracking()` then start again. | error |
|
|
309
|
-
| `recoveryRequired` | Tracking is alive; call `resumeTracking()` in the foreground (`isRecoveryRequired` is also set). | warning |
|
|
310
|
-
| `transient` | The native session survived (e.g. one upload window failed after internal retries); later uploads continue. | warning |
|
|
311
|
-
| `unknown` | Unclassified code — do not assume it is benign. | error |
|
|
363
|
+
Use `code` for stable machine branching and `message` for display. Runtime tracking failures can also have one of these recovery categories:
|
|
312
364
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
code: error.code,
|
|
321
|
-
sdkCode: error.sdkCode,
|
|
322
|
-
category: error.category,
|
|
323
|
-
});
|
|
324
|
-
} else {
|
|
325
|
-
Sentry.captureException(error);
|
|
326
|
-
}
|
|
327
|
-
}, [error]);
|
|
328
|
-
```
|
|
365
|
+
| Category | Meaning |
|
|
366
|
+
|---|---|
|
|
367
|
+
| `terminal` | The native session is already gone |
|
|
368
|
+
| `recordingDead` | Recording stopped but the native session remains open |
|
|
369
|
+
| `recoveryRequired` | The live iOS session needs a foreground resume |
|
|
370
|
+
| `transient` | The native session survived the failure |
|
|
371
|
+
| `unknown` | The failure is not classified; do not assume it is recoverable |
|
|
329
372
|
|
|
330
|
-
|
|
373
|
+
See the integration guide for the required action for each category.
|
|
331
374
|
|
|
332
|
-
|
|
375
|
+
## Production integration
|
|
333
376
|
|
|
334
|
-
|
|
377
|
+
The README covers installation and the first successful tracking flow. Before shipping, read the [Integration Guide](./docs/INTEGRATION.md) for:
|
|
335
378
|
|
|
336
|
-
-
|
|
337
|
-
-
|
|
338
|
-
-
|
|
339
|
-
-
|
|
379
|
+
- cold-start restoration and listener ownership
|
|
380
|
+
- app-owned versus SDK-owned state
|
|
381
|
+
- Android foreground-service and battery behavior
|
|
382
|
+
- iOS interruption recovery
|
|
383
|
+
- recovery-category handling
|
|
384
|
+
- analysis and report timing
|
|
385
|
+
- real-device release checks
|
|
340
386
|
|
|
341
387
|
## Migrating from 1.x
|
|
342
388
|
|
|
343
|
-
v2
|
|
344
|
-
|
|
345
|
-
1. `error: string | null` is now `AsleepError | null`. Render `error?.message` and branch on `error?.code`.
|
|
346
|
-
2. `errorInfo` is removed. Read `error.category`, `error.sdkCode`, and `error.caseName`.
|
|
347
|
-
3. Report and analysis queries throw `AsleepError` on failure instead of returning `null` or `[]`. A session that has no report surfaces as code `REPORT_NOT_FOUND`; an empty report list still resolves to `[]`.
|
|
348
|
-
4. Public state setters are removed. Native events and SDK actions are the sole state writers; `clearError()` remains public.
|
|
349
|
-
5. `getTrackingDurationMinutes()` is removed and `trackingStartTime` is internal. Apps own wall-clock duration.
|
|
350
|
-
6. `requestMicrophonePermission()` is removed. Use `requestRequiredPermissions()`.
|
|
351
|
-
7. `startTracking()` no longer requests permission. Check with `hasRequiredPermissions()`, explicitly request if needed, then start.
|
|
352
|
-
8. The default export, `Asleep` class, `AsleepSDK`, and raw `asleepStore` export are removed. Use `useAsleep()` or the named `Asleep` escape hatch.
|
|
353
|
-
9. `zustand` is no longer a dependency. Remove it from your app if nothing else uses it.
|
|
354
|
-
10. Use the additive `status: TrackingStatus` field when a single lifecycle value is clearer than multiple derived booleans.
|
|
355
|
-
11. Android 13+: `requestRequiredPermissions()` still requests `POST_NOTIFICATIONS` (so the foreground-service notification stays visible), but its return value now reflects only what tracking needs to run — microphone-side permissions, matching `hasRequiredPermissions()`. In 1.x a denied notification permission made it return `false` even though tracking could start.
|
|
389
|
+
v2 removes the parallel and mutable v1 surfaces:
|
|
356
390
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
-
|
|
391
|
+
| 1.x | v2 |
|
|
392
|
+
|---|---|
|
|
393
|
+
| `error: string \| null` and `errorInfo` | One `AsleepError`; use `message`, `code`, `category`, and `sdkCode` |
|
|
394
|
+
| Report failures returned `null` or `[]` | Report APIs throw `AsleepError`; an empty report list remains a valid result |
|
|
395
|
+
| Public state setters and raw store | Native events and SDK actions are the only writers |
|
|
396
|
+
| `getTrackingDurationMinutes()` | Store a wall-clock start time in the app |
|
|
397
|
+
| `requestMicrophonePermission()` | `requestRequiredPermissions()` |
|
|
398
|
+
| `startTracking()` requested permission | Check and request permission explicitly before starting |
|
|
399
|
+
| Default export, `AsleepSDK`, and `Asleep` class | `useAsleep()` and the named thin `Asleep` escape hatch |
|
|
400
|
+
| Required `zustand` dependency | No external state-library dependency |
|
|
363
401
|
|
|
364
|
-
|
|
402
|
+
Android 13 and later still request notification permission, but `requestRequiredPermissions()` now returns the microphone-side result that determines whether tracking can start. Pin the new version, update all imports and error branches, and test cold-start permissions before release.
|
|
365
403
|
|
|
366
404
|
## Example app
|
|
367
405
|
|
|
368
|
-
See [
|
|
406
|
+
See the [example app on GitHub](https://github.com/asleep-ai/asleep-sdk-react-native/tree/main/example) for a runnable implementation.
|
|
369
407
|
|
|
370
408
|
## License
|
|
371
409
|
|
|
372
|
-
|
|
410
|
+
See [LICENSE.md](./LICENSE.md).
|
|
373
411
|
|
|
374
412
|
## Support
|
|
375
413
|
|