Gathering detailed insights and metrics for react-native-asleep
Gathering detailed insights and metrics for react-native-asleep
Gathering detailed insights and metrics for react-native-asleep
Gathering detailed insights and metrics for react-native-asleep
npm install react-native-asleep
Typescript
Module System
Node Version
NPM Version
TypeScript (52.12%)
Kotlin (27.51%)
Swift (13.53%)
JavaScript (3.57%)
Ruby (3.18%)
C (0.1%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
47 Commits
3 Branches
7 Contributors
Updated on Jul 10, 2025
Latest Version
1.0.4
Package Id
react-native-asleep@1.0.4
Unpacked Size
177.17 kB
Size
37.36 kB
File Count
33
NPM Version
10.9.2
Node Version
22.17.0
Published on
Jul 10, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
Advanced sleep tracking SDK for React Native applications, powered by Asleep's AI technology.
This is an experimental version of the SDK and is not recommended for production use. Please be aware that it may contain bugs and breaking changes.
The Asleep SDK for React Native provides comprehensive sleep tracking capabilities using advanced AI algorithms. It can detect sleep patterns, stages, and provide detailed analytics through audio analysis without requiring any wearable devices.
1expo install react-native-asleep
1npm install react-native-asleep zustand
1npx pod-install
expo
package.Add the following permissions to your app:
For Expo Managed Projects (app.json):
1{ 2 "expo": { 3 "ios": { 4 "infoPlist": { 5 "NSMicrophoneUsageDescription": "This app needs microphone access for sleep tracking", 6 "UIBackgroundModes": ["audio"] 7 } 8 } 9 } 10}
For Bare React Native Projects (ios/YourApp/Info.plist):
1<key>NSMicrophoneUsageDescription</key> 2<string>This app needs microphone access for sleep tracking</string> 3<key>UIBackgroundModes</key> 4<array> 5 <string>audio</string> 6</array>
1<uses-permission android:name="android.permission.RECORD_AUDIO" /> 2<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> 3<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> 4<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE"/> 5<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
1import React, { useEffect } from "react"; 2import { useAsleep } from "react-native-asleep"; 3import { View, Text, Button } from "react-native"; 4 5const SleepTracker = () => { 6 const { 7 userId, 8 sessionId, 9 isTracking, 10 error, 11 initAsleepConfig, 12 startTracking, 13 stopTracking, 14 getReport, 15 enableLog, 16 } = useAsleep(); 17 18 useEffect(() => { 19 const initSDK = async () => { 20 try { 21 // Enable debug logging (optional) 22 enableLog(true); 23 24 // Initialize SDK with your API key 25 await initAsleepConfig({ 26 apiKey: "YOUR_API_KEY", 27 userId: "optional-user-id", // Optional: SDK generates one if not provided 28 }); 29 30 console.log("SDK initialized successfully"); 31 } catch (error) { 32 console.error("Failed to initialize SDK:", error); 33 } 34 }; 35 36 initSDK(); 37 }, []); 38 39 const handleStartTracking = async () => { 40 try { 41 await startTracking(); 42 console.log("Sleep tracking started"); 43 } catch (error) { 44 console.error("Failed to start tracking:", error); 45 } 46 }; 47 48 const handleStopTracking = async () => { 49 try { 50 await stopTracking(); 51 console.log("Sleep tracking stopped"); 52 } catch (error) { 53 console.error("Failed to stop tracking:", error); 54 } 55 }; 56 57 const handleGetReport = async () => { 58 if (!sessionId) return; 59 60 try { 61 const report = await getReport(sessionId); 62 console.log("Sleep report:", report); 63 } catch (error) { 64 console.error("Failed to get report:", error); 65 } 66 }; 67 68 return ( 69 <View> 70 <Text>User ID: {userId}</Text> 71 <Text>Session ID: {sessionId}</Text> 72 <Text>Status: {isTracking ? "Tracking" : "Not Tracking"}</Text> 73 74 <Button 75 title="Start Tracking" 76 onPress={handleStartTracking} 77 disabled={isTracking} 78 /> 79 <Button 80 title="Stop Tracking" 81 onPress={handleStopTracking} 82 disabled={!isTracking} 83 /> 84 <Button 85 title="Get Report" 86 onPress={handleGetReport} 87 disabled={!sessionId} 88 /> 89 </View> 90 ); 91};
For use outside React components or for singleton access:
1import { AsleepSDK } from "react-native-asleep"; 2 3class SleepManager { 4 async initializeSDK() { 5 try { 6 await AsleepSDK.initAsleepConfig({ 7 apiKey: "YOUR_API_KEY", 8 }); 9 10 // Initialize event listeners 11 AsleepSDK.initialize(); 12 13 console.log("SDK initialized"); 14 } catch (error) { 15 console.error("SDK initialization failed:", error); 16 } 17 } 18 19 async startSleepTracking() { 20 await AsleepSDK.startTracking(); 21 } 22 23 async stopSleepTracking() { 24 return await AsleepSDK.stopTracking(); 25 } 26 27 getCurrentStatus() { 28 return { 29 isTracking: AsleepSDK.isTracking(), 30 userId: AsleepSDK.getUserId(), 31 sessionId: AsleepSDK.getSessionId(), 32 }; 33 } 34}
1import { useAsleepStore } from "react-native-asleep"; 2 3// Get current state 4const currentState = useAsleepStore.getState(); 5 6// Subscribe to specific state changes 7const unsubscribe = useAsleepStore.subscribe( 8 (state) => state.isTracking, 9 (isTracking) => { 10 console.log("Tracking status changed:", isTracking); 11 } 12);
Returns an object with the following properties and methods:
userId: string | null
- Current user IDsessionId: string | null
- Current session IDisTracking: boolean
- Whether tracking is activeerror: string | null
- Last error messagedidClose: boolean
- Whether the last session was closedlog: string
- Latest log messageinitAsleepConfig(config: AsleepConfig): Promise<void>
- Initialize SDKstartTracking(): Promise<void>
- Start sleep trackingstopTracking(): Promise<void>
- Stop sleep trackinggetReport(sessionId: string): Promise<AsleepReport | null>
- Get sleep reportgetReportList(fromDate: string, toDate: string): Promise<AsleepSession[]>
- Get list of reportsenableLog(enabled: boolean): void
- Enable/disable debug loggingsetCustomNotification(title: string, text: string): Promise<void>
- Set custom notification (Android only)Configuration object for SDK initialization:
1interface AsleepConfig { 2 apiKey: string; // Required: Your API key 3 userId?: string; // Optional: User identifier 4 baseUrl?: string; // Optional: Custom API base URL 5 callbackUrl?: string; // Optional: Webhook callback URL 6}
Sleep analysis report structure:
1interface AsleepReport { 2 sessionId: string; 3 sleepEfficiency: number; 4 sleepLatency: number; 5 sleepTime: number; 6 wakeTime: number; 7 lightSleepTime: number; 8 deepSleepTime: number; 9 remSleepTime: number; 10 // ... additional metrics 11}
Session information structure:
1interface AsleepSession { 2 sessionId: string; 3 startTime: string; 4 endTime: string; 5 state: string; 6 // ... additional session data 7}
The SDK automatically handles events through the zustand store. Events are processed internally and state is updated accordingly. You can monitor state changes using useEffect:
1const { userId, sessionId, isTracking, error } = useAsleep(); 2 3useEffect(() => { 4 if (error) { 5 console.error("SDK Error:", error); 6 } 7}, [error]); 8 9useEffect(() => { 10 if (sessionId) { 11 console.log("New session created:", sessionId); 12 } 13}, [sessionId]);
See the /example
directory for a complete implementation example.
initAsleepConfig
before other methodsEnable debug logging to see detailed SDK operations:
1const { enableLog } = useAsleep(); 2enableLog(true);
This project is licensed under the MIT License.
For issues and support:
No vulnerabilities found.
No security vulnerabilities found.