import { Platform } from 'react-native'; export interface NetworkInfo { wifi: Array<{ ssid: string; bssid: string }>; bluetooth: Array<{ ssid: string; bssid: string }>; cellular: string; } /** * Network information collection utility * Collects real BSSID/SSID for native platforms, falls back to "Unknown" for web */ export class NetworkInfoCollector { private static instance: NetworkInfoCollector; private cachedInfo: NetworkInfo | null = null; private lastCacheTime: number = 0; private readonly CACHE_DURATION = 30000; // 30 seconds static getInstance(): NetworkInfoCollector { if (!NetworkInfoCollector.instance) { NetworkInfoCollector.instance = new NetworkInfoCollector(); } return NetworkInfoCollector.instance; } /** * Collect network information based on platform */ async collectNetworkInfo(): Promise { // Check cache first const now = Date.now(); if (this.cachedInfo && (now - this.lastCacheTime) < this.CACHE_DURATION) { console.log('🌐 NetworkInfo: Using cached network information'); return this.cachedInfo; } console.log('🌐 NetworkInfo: Collecting fresh network information...'); try { let networkInfo: NetworkInfo; if (Platform.OS === 'ios') { networkInfo = await this.collectIOSNetworkInfo(); } else if (Platform.OS === 'android') { networkInfo = await this.collectAndroidNetworkInfo(); } else { // Web or other platforms networkInfo = this.getDefaultNetworkInfo(); } // Cache the result this.cachedInfo = networkInfo; this.lastCacheTime = now; console.log('🌐 NetworkInfo: Network information collected:', networkInfo); return networkInfo; } catch (error) { console.error('🌐 NetworkInfo: Error collecting network information:', error); return this.getDefaultNetworkInfo(); } } /** * Collect network information for iOS */ private async collectIOSNetworkInfo(): Promise { try { // Try to use native iOS network information const networkInfo = await this.getIOSNetworkInfo(); return networkInfo; } catch (error) { console.log('🌐 NetworkInfo: iOS native collection failed, using fallback'); return this.getDefaultNetworkInfo(); } } /** * Collect network information for Android */ private async collectAndroidNetworkInfo(): Promise { try { // Try to use native Android network information const networkInfo = await this.getAndroidNetworkInfo(); return networkInfo; } catch (error) { console.log('🌐 NetworkInfo: Android native collection failed, using fallback'); return this.getDefaultNetworkInfo(); } } /** * Get iOS network information using native modules */ private async getIOSNetworkInfo(): Promise { try { // Try to use native iOS NetworkInfoModule const { NativeModules } = require('react-native'); if (NativeModules.NetworkInfoModule) { const networkInfo = await NativeModules.NetworkInfoModule.getNetworkInfo(); if (networkInfo) { return { wifi: networkInfo.wifi || [{ ssid: 'Unknown', bssid: 'Unknown' }], bluetooth: networkInfo.bluetooth || [{ ssid: 'Unknown', bssid: 'Unknown' }], cellular: networkInfo.cellular || 'Unknown' }; } } // Fallback to individual collection methods const wifiInfo = await this.getIOSWifiInfo(); const bluetoothInfo = await this.getIOSBluetoothInfo(); const cellularInfo = await this.getIOSCellularInfo(); return { wifi: wifiInfo, bluetooth: bluetoothInfo, cellular: cellularInfo }; } catch (error) { console.error('🌐 NetworkInfo: iOS network collection error:', error); throw error; } } /** * Get Android network information using native modules */ private async getAndroidNetworkInfo(): Promise { try { // Try to use native Android NetworkInfoModule const { NativeModules } = require('react-native'); if (NativeModules.NetworkInfoModule) { const networkInfo = await NativeModules.NetworkInfoModule.getNetworkInfo(); if (networkInfo) { return { wifi: networkInfo.wifi || [{ ssid: 'Unknown', bssid: 'Unknown' }], bluetooth: networkInfo.bluetooth || [{ ssid: 'Unknown', bssid: 'Unknown' }], cellular: networkInfo.cellular || 'Unknown' }; } } // Fallback to individual collection methods const wifiInfo = await this.getAndroidWifiInfo(); const bluetoothInfo = await this.getAndroidBluetoothInfo(); const cellularInfo = await this.getAndroidCellularInfo(); return { wifi: wifiInfo, bluetooth: bluetoothInfo, cellular: cellularInfo }; } catch (error) { console.error('🌐 NetworkInfo: Android network collection error:', error); throw error; } } /** * Get iOS WiFi information */ private async getIOSWifiInfo(): Promise> { try { // Try to get WiFi information using available methods // This is a placeholder - in a real implementation, you'd use a native module // For iOS, we can try to get some basic network info const ssid = await this.getIOSSSID(); const bssid = await this.getIOSBSSID(); if (ssid && ssid !== 'Unknown' && bssid && bssid !== 'Unknown') { return [{ ssid, bssid }]; } return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } catch (error) { console.log('🌐 NetworkInfo: iOS WiFi info collection failed'); return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } } /** * Get iOS Bluetooth information */ private async getIOSBluetoothInfo(): Promise> { try { // Bluetooth information collection for iOS // This would require Bluetooth permissions and native modules return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } catch (error) { console.log('🌐 NetworkInfo: iOS Bluetooth info collection failed'); return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } } /** * Get iOS Cellular information */ private async getIOSCellularInfo(): Promise { try { // Try to get cellular carrier information // This would require native modules or system APIs return 'Unknown'; } catch (error) { console.log('🌐 NetworkInfo: iOS cellular info collection failed'); return 'Unknown'; } } /** * Get Android WiFi information */ private async getAndroidWifiInfo(): Promise> { try { // Try to use native Android NetworkInfoModule const { NativeModules } = require('react-native'); if (NativeModules.NetworkInfoModule) { const networkInfo = await NativeModules.NetworkInfoModule.getNetworkInfo(); if (networkInfo && networkInfo.wifi) { return networkInfo.wifi; } } return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } catch (error) { console.log('🌐 NetworkInfo: Android WiFi info collection failed:', error); return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } } /** * Get Android Bluetooth information */ private async getAndroidBluetoothInfo(): Promise> { try { // Try to use native Android NetworkInfoModule const { NativeModules } = require('react-native'); if (NativeModules.NetworkInfoModule) { const networkInfo = await NativeModules.NetworkInfoModule.getNetworkInfo(); if (networkInfo && networkInfo.bluetooth) { return networkInfo.bluetooth; } } return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } catch (error) { console.log('🌐 NetworkInfo: Android Bluetooth info collection failed:', error); return [{ ssid: 'Unknown', bssid: 'Unknown' }]; } } /** * Get Android Cellular information */ private async getAndroidCellularInfo(): Promise { try { // Try to use native Android NetworkInfoModule const { NativeModules } = require('react-native'); if (NativeModules.NetworkInfoModule) { const networkInfo = await NativeModules.NetworkInfoModule.getNetworkInfo(); if (networkInfo && networkInfo.cellular) { return networkInfo.cellular; } } return 'Unknown'; } catch (error) { console.log('🌐 NetworkInfo: Android cellular info collection failed:', error); return 'Unknown'; } } /** * Get iOS SSID using native module */ private async getIOSSSID(): Promise { try { // Try to use native iOS NetworkInfoModule const { NativeModules } = require('react-native'); if (NativeModules.NetworkInfoModule) { const ssid = await NativeModules.NetworkInfoModule.getWifiSSID(); return ssid || 'Unknown'; } return 'Unknown'; } catch (error) { console.log('🌐 NetworkInfo: iOS SSID collection failed:', error); return 'Unknown'; } } /** * Get iOS BSSID using native module */ private async getIOSBSSID(): Promise { try { // Try to use native iOS NetworkInfoModule const { NativeModules } = require('react-native'); if (NativeModules.NetworkInfoModule) { const bssid = await NativeModules.NetworkInfoModule.getWifiBSSID(); return bssid || 'Unknown'; } return 'Unknown'; } catch (error) { console.log('🌐 NetworkInfo: iOS BSSID collection failed:', error); return 'Unknown'; } } /** * Get default network information (fallback) */ private getDefaultNetworkInfo(): NetworkInfo { console.log('🌐 NetworkInfo: Using default network information'); return { wifi: [{ ssid: 'Unknown', bssid: 'Unknown' }], bluetooth: [{ ssid: 'Unknown', bssid: 'Unknown' }], cellular: 'Unknown' }; } /** * Clear cached network information */ clearCache(): void { this.cachedInfo = null; this.lastCacheTime = 0; console.log('🌐 NetworkInfo: Cache cleared'); } /** * Get platform-specific network capabilities */ getPlatformCapabilities(): { canCollectWifi: boolean; canCollectBluetooth: boolean; canCollectCellular: boolean } { return { canCollectWifi: Platform.OS === 'ios' || Platform.OS === 'android', canCollectBluetooth: Platform.OS === 'ios' || Platform.OS === 'android', canCollectCellular: Platform.OS === 'ios' || Platform.OS === 'android' }; } } // Export singleton instance export const networkInfoCollector = NetworkInfoCollector.getInstance();