React native connect to wifi

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A react-native implementation for viewing and connecting to Wifi networks on Android and iOS devices.

License

JuanSeBestia/react-native-wifi-reborn

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This project is based on the no longer maintained https://github.com/robwalkerco/react-native-wifi.

$ npm install react-native-wifi-reborn —save

You need use enable Access WIFI Information , with correct profile. Hotspot Configuration is required in order to connect to networks.

You need put «Privacy — Location When In Use Usage Description» or «Privacy — Location Always and When In Use Usage Description» in Settings -> info

Since Android 6, you must request the ACCESS_FINE_LOCATION permission at runtime to use the device’s Wi-Fi scanning and managing capabilities. In order to accomplish this, you can use the PermissionsAndroid API or React Native Permissions.

import  PermissionsAndroid > from 'react-native'; const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,  title: 'Location permission is required for WiFi connections', message: 'This app needs location permission as this is required ' + 'to scan for wifi networks.', buttonNegative: 'DENY', buttonPositive: 'ALLOW', >, ); if (granted === PermissionsAndroid.RESULTS.GRANTED)  // You can now use react-native-wifi-reborn > else  // Permission denied >

Autolinking (React Native 60+)

This library is correctly autolinked on React Native 60+ 🎉 .

React Native Link (for React Native 0.59 and below)

$ react-native link react-native-wifi-reborn

  1. In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project’s name]
  2. Go to node_modules ➜ react-native-wifi-reborn and add RNWifi.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNWifi.a to your project’s Build Phases ➜ Link Binary With Libraries
  4. Run your project ( Cmd+R )
  1. Open up android/app/src/main/java/[. ]/MainActivity.java
  • Add import com.reactlibrary.rnwifi.RNWifiPackage; to the imports at the top of the file
  • Add new RNWifiPackage() to the list returned by the getPackages() method
include ':react-native-wifi-reborn' project(':react-native-wifi-reborn').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-wifi-reborn/android') 
 implementation project(':react-native-wifi-reborn') 

This package cannot be used in the «Expo Go» app because it requires custom native code.

After installing this npm package, add the config plugin to the plugins array of your app.json or app.config.js :

< "expo": < "plugins": ["react-native-wifi-reborn"] > >

Next, rebuild your app as described in the «Adding custom native code» guide.

The plugin provides props for extra customization. Every time you change the props or plugins, you’ll need to rebuild (and prebuild ) the native app. If no extra properties are added, defaults will be used.

  • fineLocationPermission (false | undefined): When false , the android.permission.ACCESS_FINE_LOCATION will not be added to the AndroidManifest.xml .
< "plugins": [ [ "react-native-wifi-reborn", < "fineLocationPermission": false > ] ] >
import WifiManager from "react-native-wifi-reborn"; WifiManager.connectToProtectedSSID(ssid, password, isWep).then( () =>  console.log("Connected successfully!"); >, () =>  console.log("Connection failed!"); > ); WifiManager.getCurrentWifiSSID().then( ssid =>  console.log("Your current connected wifi SSID is " + ssid); >, () =>  console.log("Cannot get current SSID!"); > );

The api documentation is in progress.

The following methods work on both Android and iOS

connectToProtectedSSID(SSID: string, password: string, isWEP: boolean, isHidden: boolean): Promise

Returns a promise that resolves when connected or rejects with the error when it couldn’t connect to the wifi network.

The SSID of the wifi network to connect with.

The password of the wifi network to connect with.

Type: boolean Used on iOS. If true, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network.

Type: boolean Used on Android. If true, the network is a hidden Wi-Fi network.

  • iOS:
    • unavailableForOSVersion : Starting from iOS 11, NEHotspotConfigurationError is available.
    • invalid : If an unknown error is occurred.
    • invalidSSID : If the SSID is invalid.
    • invalidSSIDPrefix : If the SSID prefix is invalid.
    • invalidPassphrase : If the passphrase is invalid.
    • userDenied : If the user canceled the request to join the asked network.
    • locationPermissionDenied : Starting from iOS 13, location permission is denied.
    • locationPermissionRestricted : Starting from iOS 13, location permission is restricted.
    • couldNotDetectSSID : If the SSID couldn’t be detected.
    • locationPermissionMissing : Starting android 6, location permission needs to be granted for wifi scanning.
    • locationServicesOff : Starting Android 6, location services needs to be on to scan for wifi networks.
    • couldNotEnableWifi : Starting Android 10, apps are no longer allowed to enable wifi. User has to manually do this.
    • couldNotScan : Starting Android 9, it’s only allowed to scan 4 times per 2 minuts in a foreground app.
    • didNotFindNetwork : If the wifi network is not in range, the security type is unknown and WifiUtils doesn’t support connecting to the network.
    • authenticationErrorOccurred : Authentication error occurred while trying to connect. The password could be incorrect or the user could have a saved network configuration with a different password!
    • android10ImmediatelyDroppedConnection : Firmware bugs on OnePlus prevent it from connecting on some firmware versions. More info: ThanosFisherman/WifiUtils#63.
    • timeoutOccurred : Could not connect in the timeout window.
    • unableToConnect : When an unknown error occurred.

    Returns the SSID of the current WiFi network.

    The following methods work only on iOS

    connectToSSID(ssid: string): Promise

    connectToSSIDPrefix(ssid: string): Promise

    disconnectFromSSID(ssid: string): Promise

    connectToProtectedSSIDOnce(SSID: string, password: string, isWEP: boolean, joinOnce: boolean): Promise

    connectToProtectedSSIDPrefix(SSIDPrefix: string, password: string, isWep: boolean): Promise

    Use this function when you want to match a known SSID prefix, but don’t have a full SSID. If the system finds multiple Wi-Fi networks whose SSID string matches the given prefix, it selects the network with the greatest signal strength.

    Type: string A prefix string to match the SSID of a Wi-Fi network.

    Type: string The password of the wifi network to connect with.

    Type: boolean Used on iOS. If YES, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network.

    Type: boolean Used on iOS. Optional param. Defaults to false . When joinOnce is set to true, the hotspot remains configured and connected only as long as the app that configured it is running in the foreground. The hotspot is disconnected and its configuration is removed when any of the following events occurs:

    • The app stays in the background for more than 15 seconds.
    • The device sleeps.
    • The app crashes, quits, or is uninstalled.
    • The app connects the device to a different Wi-Fi network.
    • The user connects the device to a different Wi-Fi network.
    • unavailableForOSVersion : Starting from iOS 11, NEHotspotConfigurationError is available.
    • invalid : If an unknown error is occurred.
    • invalidSSID : If the SSID is invalid.
    • invalidSSIDPrefix : If the SSID prefix is invalid.
    • invalidPassphrase : If the passphrase is invalid.
    • userDenied : If the user canceled the request to join the asked network.
    • locationPermissionDenied : Starting from iOS 13, location permission is denied.
    • locationPermissionRestricted : Starting from iOS 13, location permission is restricted.
    • couldNotDetectSSID : If the SSID couldn’t be detected.
    • unableToConnect : When an unknown error occurred.

    The following methods work only on Android

    Returns a list of nearby WiFI networks.

    • SSID : The network name.
    • BSSID : The WiFi BSSID.
    • capabilities : Describes the authentication, key management, and encryption schemes supported by the access point.
    • frequency : The primary 20 MHz frequency (in MHz) of the channel over which the client is communicating with the access point.
    • level : The detected signal level in dBm, also known as the RSSI.
    • timestamp : timestamp in microseconds (since boot) when this result was last seen.
    • locationPermissionMissing : Starting android 6, location permission needs to be granted for wifi
    • locationServicesOff : Starting Android 6, location services needs to be on to scan for wifi networks.
    • exception : Any other caught exception.

    Similar to loadWifiList but it forcefully starts a new WiFi scan and only passes the results when the scan is done.

    Method to check if WiFi is enabled.

    const enabled = await WifiManager.isEnabled(); this.setState(wifiIsEnabled: enabled>);

    Method to set the WiFi on or off on the user’s device.

    WifiManager.setEnabled(true); //set WiFi ON WifiManager.setEnabled(false); //set WiFi OFF

    Returns if the device is currently connected to a WiFi network.

    Disconnect currently connected WiFi network.

    Returns the BSSID (basic service set identifier) of the currently connected WiFi network.

    Returns the RSSI (received signal strength indicator) of the currently connected WiFi network.

    Returns the frequency of the currently connected WiFi network.

    Returns the IP of the currently connected WiFi network.

    isRemoveWifiNetwork(ssid: String): Promise

    This method will remove the wifi network configuration. If you are connected to that network, it will disconnect.

    • locationPermissionMissing : Starting android 6, location permission needs to be granted for wifi

    forceWifiUsage(useWifi: boolean): Promise

    Deprecated; see forceWifiUsageWithOptions.

    forceWifiUsageWithOptions(useWifi: boolean, options): Promise

    Use this to execute api calls to a wifi network that does not have internet access. Useful for commissioning IoT devices. This will route all app network requests to the network (instead of the mobile connection). It is important to disable it again after using as even when the app disconnects from the wifi network it will keep on routing everything to wifi.

    About

    A react-native implementation for viewing and connecting to Wifi networks on Android and iOS devices.

    Источник

    Читайте также:  Контроль вай фай приложение
Оцените статью
Adblock
detector