import React from 'react'; import { StyleSheet, Text, View, TouchableOpacity, ScrollView, ActivityIndicator, } from "react-native"; import SetupView from '../components/SetupView'; import MaterialIcon from "react-native-vector-icons/MaterialCommunityIcons"; function WifiListElement(props) { let iconName = "wifi-strength-" + props.strength; if (props.lock) { iconName += "-lock"; } return ( {props.text} ) } const wifiListElementStyles = { container: { backgroundColor: "rgba(255,255,255,0.4)", borderRadius: 5, width: "100%", height: 50, flexDirection: "row", alignItems: "center", marginTop: 8, marginBottom: 8, }, icon: { fontSize: 25, color: "rgba(255,255,255,1)", marginLeft: 15, marginRight: 15, }, text: { color: "rgba(255,255,255,1)", fontSize: 18, width: "100%" } }; // --------------------------------------------------------------------------------------------- class WifiSelectionView extends React.Component { constructor() { super(); this.state = { wifiInfo: [] }; this.mounted = false; } processDeviceResponse(response) { // sort from strong to weak response.sort((e1, e2) => { if (e1.rssi > e2.rssi) return -1; if (e1.rssi < e2.rssi) return 1; else return 0; }); let ssidsAlreadyAdded = {}; let result = []; for (let i = 0; i < response.length; i++) { if (response[i].ssid in ssidsAlreadyAdded) continue; const locked = (response[i].sec != "open"); let strength = 1; if (response[i].rssi > -30) strength = 4; else if (response[i].rssi > -67) strength = 3; else if (response[i].rssi > -70) strength = 2; result.push({ ssid: response[i].ssid, locked: locked, strength: strength }); ssidsAlreadyAdded[response[i].ssid] = true; } return result; } componentDidMount() { let component = this; component.mounted = true; this.props.device.conn.scanWifiNetworks().then( (result) => { if(component.mounted) { this.setState({ wifiInfo: this.processDeviceResponse(result) }) } } ); } componentWillUnmount() { this.mounted = false; } render() { let inner; if (this.state.wifiInfo.length > 0) { inner = ( {this.state.wifiInfo.map(e => ( { this.props.navigation.navigate("WifiPasswordView", { ssid: e.ssid, lock: e.locked, strength: e.strength, confirmPwInput: false, buttonText: "OK", subText: "Please enter the password for your home WiFi", onSubmit: (ssid, pw) => { this.props.device.conn.wifiSetModeSTA(ssid, pw); }, }); }}> ) )} ) } else { inner = ( Scanning WiFi networks ) } return ( { this.props.navigation.navigate("WifiPasswordView", { ssid: "swimtracker-E2842S", // todo real id here lock: true, strength: 4, confirmPwInput: true, buttonText: "Set Password", subText: "Use this option only if you're home WiFi doesn't reach the SwimTracker. The SwimTracker creates its own WiFi with the password you set here.", onSubmit: (ssid, pw) => { this.props.device.conn.wifiSetModeAP(pw); }, }); }} lowerRightButtonText="Need help?" > {inner} ) } } const styles = StyleSheet.create({ listContainer: { height: "75%", flex: 1, } }); export default WifiSelectionView;