import React, {useEffect, useRef, useState} from 'react'; import {ActivityIndicator, Button, Image, ScrollView, StyleSheet, Text, TextInput, View} from 'react-native'; import BluetoothSdk, {type Device, type OtaStatusEvent} from '@mentra/bluetooth-sdk'; import {useMentraBluetooth} from '@mentra/bluetooth-sdk/react'; export default function App() { const session = useMentraBluetooth(); const [devices, setDevices] = useState([]); const [webhookUrl, setWebhookUrl] = useState(''); const [photoUrl, setPhotoUrl] = useState(); const [previewError, setPreviewError] = useState(false); const [lastAction, setLastAction] = useState(''); const [busy, setBusy] = useState(false); const action = useRef(null); const [otaStatus, setOtaStatus] = useState(); const connected = session.glasses.connected; const ready = session.glasses.ready; useEffect(() => { return () => { action.current?.abort(); void BluetoothSdk.stopScan().catch(() => undefined); }; }, []); useEffect(() => { if (!connected) { setOtaStatus(undefined); return; } let active = true; const ota = BluetoothSdk.addListener('ota_status', event => { if (active) setOtaStatus(event); }); return () => { active = false; ota.remove(); }; }, [connected]); useEffect(() => { if (!connected && action.current) { action.current.abort(); action.current = null; setBusy(false); setLastAction('Disconnected'); } }, [connected]); async function run(label: string, operation: (signal: AbortSignal) => Promise) { if (action.current) return; const controller = new AbortController(); action.current = controller; setBusy(true); setLastAction(label); try { await operation(controller.signal); } catch (error) { if (!controller.signal.aborted) setLastAction(error instanceof Error ? error.message : String(error)); } finally { if (!controller.signal.aborted) { action.current = null; setBusy(false); } } } async function scan(signal: AbortSignal) { setDevices([]); await BluetoothSdk.scan({model: 'Mentra Live', timeoutMs: 10000, onResults: results => { if (!signal.aborted) setDevices(results); }}); if (!signal.aborted) setLastAction('Scan complete'); } async function capture(signal: AbortSignal) { let url: URL; try { url = new URL(webhookUrl); } catch { throw new Error('Enter an HTTP or HTTPS webhook URL.'); } if (!['http:', 'https:'].includes(url.protocol)) throw new Error('Enter an HTTP or HTTPS webhook URL.'); setPhotoUrl(undefined); setPreviewError(false); const requestId = `photo-${Date.now()}`; let pending = true; const progress = BluetoothSdk.addListener('photo_status', event => { if (pending && !signal.aborted && event.requestId === requestId) setLastAction(`Photo: ${event.status}`); }); try { const photo = await BluetoothSdk.requestPhoto({requestId, webhookUrl: url.href, size: 'medium', authToken: null, compress: 'none', sound: true}); if (signal.aborted) return; setPhotoUrl(photo.photoUrl); setLastAction(`Photo uploaded: ${photo.requestId}`); } finally { pending = false; progress.remove(); } } return ( Mentra SDK Mac {connected ? 'Connected' : 'Disconnected'}