Download examples/bluetooth/react-native/src/App.tsx from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 5.51 kB
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/examples/bluetooth/react-native/src/App.tsx
- Command line
-
hf download hf://Anirudh1950/MentraOS/examples/bluetooth/react-native/src/App.tsx
-
curl -L -o App.tsx https://huggingface.co/Anirudh1950/MentraOS/resolve/main/examples/bluetooth/react-native/src/App.tsx
5.51 kB
| import React, { useEffect, useRef, useState } from 'react'; | |
| import { Keyboard, Platform, Pressable, Text, View, StyleSheet, StatusBar } from 'react-native'; | |
| import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context'; | |
| import { KeyboardVisibleContext } from './components/keyboardLayout'; | |
| import { TabBar, TabKey } from './components/TabBar'; | |
| import { CustomMentraLiveOtaFlow } from './ota/CustomMentraLiveOtaFlow'; | |
| import { DeviceScreen } from './screens/DeviceScreen'; | |
| import { CameraScreen } from './screens/CameraScreen'; | |
| import { StreamScreen } from './screens/StreamScreen'; | |
| import { SystemScreen } from './screens/SystemScreen'; | |
| import { ConsoleScreen } from './screens/ConsoleScreen'; | |
| import { otaFlowAdmission } from './otaFlowAdmission'; | |
| import { connectedWifiStatus } from './sdkFormat'; | |
| import { colors } from './components/theme'; | |
| import { isMentraLiveRuntime, useBluetoothSdkExample } from './useBluetoothSdkExample'; | |
| export default function App() { | |
| const [tab, setTab] = useState<TabKey>('device'); | |
| const [keyboardVisible, setKeyboardVisible] = useState(false); | |
| const [otaVisible, setOtaVisible] = useState(false); | |
| const [waitingForWifi, setWaitingForWifi] = useState(false); | |
| const otaCompletedGenerationRef = useRef<number | null>(null); | |
| const sdk = useBluetoothSdkExample({activeTab: tab}); | |
| const currentWifi = connectedWifiStatus(sdk.glasses); | |
| // A failed attempt to join another network must not block the current one. | |
| const otaWifiSetupComplete = currentWifi !== null && sdk.wifiConnectingSsid === null; | |
| const mentraLiveConnected = isMentraLiveRuntime(sdk.glasses); | |
| const connectionGeneration = mentraLiveConnected && sdk.glasses.connected | |
| ? sdk.glassesConnectionGeneration | |
| : null; | |
| useEffect(() => { | |
| const admission = otaFlowAdmission({ | |
| completedConnectionGeneration: otaCompletedGenerationRef.current, | |
| connectionGeneration, | |
| waitingForWifi, | |
| }); | |
| if (admission === 'idle') { | |
| if (!otaVisible) { | |
| otaCompletedGenerationRef.current = null; | |
| setWaitingForWifi(false); | |
| } | |
| return; | |
| } | |
| if (admission === 'wait_for_wifi' || admission === 'done') { | |
| return; | |
| } | |
| setWaitingForWifi(false); | |
| setOtaVisible(true); | |
| }, [connectionGeneration, mentraLiveConnected, otaVisible, sdk.glasses, waitingForWifi]); | |
| useEffect(() => { | |
| const showEvent = Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow'; | |
| const hideEvent = Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide'; | |
| const showSub = Keyboard.addListener(showEvent, () => setKeyboardVisible(true)); | |
| const hideSub = Keyboard.addListener(hideEvent, () => setKeyboardVisible(false)); | |
| return () => { | |
| showSub.remove(); | |
| hideSub.remove(); | |
| }; | |
| }, []); | |
| const finishOta = () => { | |
| otaCompletedGenerationRef.current = connectionGeneration; | |
| setOtaVisible(false); | |
| }; | |
| const openWifiSetup = () => { | |
| setWaitingForWifi(true); | |
| setOtaVisible(false); | |
| setTab('system'); | |
| }; | |
| const openOta = () => { | |
| otaCompletedGenerationRef.current = null; | |
| setWaitingForWifi(false); | |
| setOtaVisible(true); | |
| }; | |
| return ( | |
| <SafeAreaProvider> | |
| <StatusBar barStyle="dark-content" /> | |
| <KeyboardVisibleContext.Provider value={keyboardVisible}> | |
| {otaVisible ? ( | |
| <CustomMentraLiveOtaFlow | |
| deviceName="Mentra Live" | |
| onFinished={finishOta} | |
| onOpenWifiSetup={openWifiSetup} | |
| /> | |
| ) : ( | |
| <SafeAreaView style={styles.root} edges={['top']}> | |
| {waitingForWifi && tab === 'system' && ( | |
| <View style={styles.otaResume}> | |
| <Text style={styles.otaResumeText}> | |
| {otaWifiSetupComplete | |
| ? `Connected to ${currentWifi?.ssid}` | |
| : 'Connect to a Wi-Fi network to continue the update.'} | |
| </Text> | |
| {otaWifiSetupComplete && ( | |
| <Pressable | |
| accessibilityRole="button" | |
| onPress={openOta} | |
| style={styles.otaResumeButton}> | |
| <Text style={styles.otaResumeLabel}>Continue update</Text> | |
| </Pressable> | |
| )} | |
| </View> | |
| )} | |
| <View style={styles.screen}> | |
| {tab === 'device' && <DeviceScreen sdk={sdk} onOpenOta={openOta} />} | |
| {tab === 'camera' && <CameraScreen sdk={sdk} />} | |
| {tab === 'stream' && <StreamScreen sdk={sdk} />} | |
| {tab === 'system' && <SystemScreen sdk={sdk} />} | |
| {tab === 'console' && <ConsoleScreen sdk={sdk} />} | |
| </View> | |
| {!keyboardVisible && <TabBar active={tab} onChange={setTab} />} | |
| </SafeAreaView> | |
| )} | |
| </KeyboardVisibleContext.Provider> | |
| </SafeAreaProvider> | |
| ); | |
| } | |
| const styles = StyleSheet.create({ | |
| root: { flex: 1, backgroundColor: '#fff' }, | |
| screen: { flex: 1 }, | |
| otaResume: { padding: 16, gap: 12, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: colors.hairline }, | |
| otaResumeText: { color: colors.ink, fontSize: 14 }, | |
| otaResumeButton: { backgroundColor: colors.greenInk, borderRadius: 14, minHeight: 48, alignItems: 'center', justifyContent: 'center' }, | |
| otaResumeLabel: { color: colors.bg, fontSize: 14, fontWeight: '700' }, | |
| }); | |