From f4f84442f834222788605381785f03c11e8c70cc Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Tue, 25 Aug 2020 12:55:22 +0200 Subject: [PATCH] chore: Update example app (#105) Co-authored-by: Elias Lecomte --- .gitignore | 3 + example/RNWifiReborn/App.js | 57 ++++++++++++--- example/RNWifiReborn/android/app/build.gradle | 73 ------------------- example/RNWifiReborn/android/build.gradle | 6 +- example/RNWifiReborn/package-lock.json | 5 +- example/RNWifiReborn/package.json | 2 +- 6 files changed, 57 insertions(+), 89 deletions(-) diff --git a/.gitignore b/.gitignore index 8e29aa9..43eba10 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,9 @@ build/ .gradle local.properties *.iml +.classpath +.project +org.eclipse.buildship.core.prefs # BUCK buck-out/ diff --git a/example/RNWifiReborn/App.js b/example/RNWifiReborn/App.js index d409bda..9583197 100644 --- a/example/RNWifiReborn/App.js +++ b/example/RNWifiReborn/App.js @@ -7,6 +7,7 @@ */ import React, {useState, useEffect} from 'react'; +import { PermissionsAndroid, Button } from 'react-native'; import { SafeAreaView, StyleSheet, @@ -23,14 +24,47 @@ const App = () => { const [connected, setConnected] = useState({connected: false, ssid: 'S4N'}); const [ssid, setSsid] = useState(''); const password ="tanenbaum-1981"; - const isWep = false; - const wifi = async () => { + const initWifi = async () => { + try { + const ssid = await WifiManager.getCurrentWifiSSID(); + setSsid(ssid); + console.log('Your current connected wifi SSID is ' + ssid); + } catch (error) { + setSsid('Cannot get current SSID!' + error.message); + console.log('Cannot get current SSID!', {error}); + } + } + + const requestLocationPermission = async () => { + try { + const granted = await PermissionsAndroid.request( + PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, + { + title: "React Native Wifi Reborn App Permission", + message: + "Location permission is required to connect with or scan for Wifi networks. ", + buttonNeutral: "Ask Me Later", + buttonNegative: "Cancel", + buttonPositive: "OK" + } + ); + if (granted === PermissionsAndroid.RESULTS.GRANTED) { + initWifi(); + } else { + console.log("Location permission denied"); + } + } catch (err) { + console.warn(err); + } + }; + + const connectWithWifi = async () => { try { const data = await WifiManager.connectToProtectedSSID( ssid, password, - isWep, + false, ); console.log('Connected successfully!', {data}); setConnected({connected: true, ssid}); @@ -38,18 +72,20 @@ const App = () => { setConnected({connected: false, error: error.message}); console.log('Connection failed!', {error}); } + }; + const scanExample = async () => { try { - const ssid = await WifiManager.getCurrentWifiSSID(); - setSsid(ssid); - console.log('Your current connected wifi SSID is ' + ssid); + const data = await WifiManager.reScanAndLoadWifiList() + console.log(data); } catch (error) { - setSsid('Cannot get current SSID!' + error.message); - console.log('Cannot get current SSID!', {error}); + console.log(error); } - }; + } + + useEffect(() => { - wifi(); + requestLocationPermission(); }, []); return ( @@ -72,6 +108,7 @@ const App = () => { {JSON.stringify(connected)} +