import React from 'react'; import {Alert, StyleSheet, Button, Switch, ScrollView} from 'react-native'; import {ThemedText} from '@/components/themed-text'; import {ThemedView} from '@/components/themed-view'; import {useBleExplorer} from '@/hooks/useBleExplorer'; import {useBlePeripheral} from '@/hooks/useBlePeripheral'; import {BLE_UUIDS, PROTOCOL_VERSION} from '@/ble'; import ParallaxScrollView from '@/components/parallax-scroll-view'; import {IconSymbol} from '@/components/ui/icon-symbol'; export default function TabTwoScreen() { const [deviceMode, setDeviceMode] = React.useState<'central' | 'peripheral'>('central'); const { isScanning, isConnected, connectedDevice, deviceInfo, version, isActivated, transferProgress, isTransferring, discoveredDevices, loading, error, startScan, stopScan, connectToDevice, disconnectDevice, queryActivationStatus, queryDeviceVersion, requestDeviceInfo, updateActivationTime, sendIdentityCheck, transferSampleFile, clearLogs, } = useBleExplorer(); const { isAdvertising, connectedCentralCount, logs: peripheralLogs, deviceInfo: peripheralDeviceInfo, loading: peripheralLoading, error: peripheralError, startAdvertising, stopAdvertising, getCharacteristicReadValue, updateDeviceInfo, resetDeviceInfo, clearLogs: clearPeripheralLogs, } = useBlePeripheral(); return ( }> BLE Explorer {/* Device Mode Selection */} Device Mode Central { if (value) { // Switching to peripheral mode - stop central operations first console.log('Switching to peripheral mode - stopping central operations'); if (isScanning) { await stopScan(); // Add small delay to ensure cleanup await new Promise(resolve => setTimeout(resolve, 500)); } if (isConnected) { await disconnectDevice(); // Add small delay to ensure cleanup await new Promise(resolve => setTimeout(resolve, 500)); } } else { // Switching to central mode - stop peripheral operations first console.log('Switching to central mode - stopping peripheral operations'); if (isAdvertising) { await stopAdvertising(); // Add small delay to ensure cleanup await new Promise(resolve => setTimeout(resolve, 500)); } } setDeviceMode(value ? 'peripheral' : 'central'); }} disabled={isScanning || isConnected || isAdvertising || peripheralLoading.advertising} /> Peripheral {deviceMode === 'central' ? 'Central mode: Scan for and connect to BLE peripherals' : `Peripheral mode: Act as BLE receiver`} {deviceMode === 'central' ? ( <> {/* Connection Status */} Connection Status Scanning: {isScanning ? 'Yes' : 'No'} Connected: {isConnected ? 'Yes' : 'No'} Device: {connectedDevice?.name || 'None'} {error && Error: {error}} {/* Discovered Devices */} Discovered Devices Total devices found: {discoveredDevices.length} {discoveredDevices.length === 0 ? ( No devices discovered yet. Start scanning to find devices. ) : ( {discoveredDevices.map((item) => ( {item.name || 'Unknown Device'} {item.id} {item.serviceUUIDs && item.serviceUUIDs.length > 0 && ( Services: {item.serviceUUIDs.join(', ')} )} {item.connected && ( Connected )}