Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-wifi-reborn

JuanSeBestia35.1kISC4.13.4TypeScript support: included

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

react, react-native, android, ios, wifi, connect

readme

react-native-wifi-reborn

ISC license Current npm package version Maintenance Semantic Release Downloads Total downloads Follow @JuanSeBestia

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

Getting started

$ npm install react-native-wifi-reborn

iOS

Your Info.plist will need to include NSLocalNetworkUsageDescription permission to join other networks.

Beforehand in XCode, you need use enable Access WIFI Information to access Wi-Fi information in project settings - '+ Capability'

image

Hotspot Configuration is also required in order to connect to networks. image

Please make sure your profile support these two capabilities above.

iOS 13

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

Android

ACCESS_FINE_LOCATION permission

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.

Example:

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 react-native-wifi-reborn

Manual linking

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-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 PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java

  2. Add import com.reactlibrary.rnwifi.RNWifiPackage; to the imports at the top of the file

  3. Add new RNWifiPackage() to the list returned by the getPackages() method

  4. Append the following lines to android/settings.gradle:

    include ':react-native-wifi-reborn'
    project(':react-native-wifi-reborn').projectDir = new File(rootProject.projectDir,     '../node_modules/react-native-wifi-reborn/android')
  5. Insert the following lines inside the dependencies block in android/app/build.gradle:
       implementation project(':react-native-wifi-reborn')

Prebuild Plugin

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": {
    "ios": {
      "infoPlist": {
        "NSLocalNetworkUsageDescription": "The app requires access to the local network so it can..."
      }
    },
    "plugins": ["react-native-wifi-reborn"]
  }
}

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

Props

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.

Example

{
  "plugins": [
    [
      "react-native-wifi-reborn",
      {
        "fineLocationPermission": false
      }
    ]
  ]
}

Usage then/catch

import WifiManager from "react-native-wifi-reborn";

WifiManager.connectToProtectedSSID(ssid, password, isWep, isHidden).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!");
  }
);

Usage async/await

import WifiManager from "react-native-wifi-reborn";

async function main(ssid, password, isWep, isHidden) {
  try {
    await WifiManager.connectToProtectedSSID(ssid, password, isWep, isHidden);
    console.log("Connected successfully!");
  } catch (error) {
    console.log("Connection failed!");
  }

  try {
    const currentSSID = await WifiManager.getCurrentWifiSSID();
    console.log(`Your current Wi-Fi SSID is ${currentSSID}`);
  } catch (error) {
    console.log("Cannot get current SSID!");
  }
}

IoT devices (Android)

If you need to connect and send data to IoT devices, add a network_security_config.xml file with the following content:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">IP_ADDRESS</domain>
    </domain-config>
</network-security-config>

Replace IP_ADDRESS with your device IP for example 192.168.4.1.

Edit also your AndroidManifest.xml and add this line into <application:

<application
  ...
  android:networkSecurityConfig="@xml/network_security_config"
>

Methods

The api documentation is in progress.

Android & iOS

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.

SSID

Type: string

The SSID of the wifi network to connect with.

password

Type: string

The password of the wifi network to connect with.

isWep

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

isHidden

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

connectToProtectedWifiSSID({ ssid: string, password: string, isWEP: boolean, isHidden: boolean, timeout: number }): Promise

Same params as above but passed as an object and with a timeout parameter added.

timeout

Type: number Used on Android to set a timeout in seconds. Default 15 seconds.

Errors:

  • 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.
  • Android:
    • 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: https://github.com/ThanosFisherman/WifiUtils/issues/63.
    • timeoutOccurred: Could not connect in the timeout window. - ONLY NEW VERSION
  • Both:
    • unableToConnect: When an unknown error occurred.

getCurrentWifiSSID(): Promise

Returns the SSID of the current WiFi network.

Errors:

  • couldNotDetectSSID: Not connected or connecting.

Only iOS

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

connectToProtectedSSIDPrefixOnce(SSIDPrefix: string, password: string, isWep: boolean, joinOnce: 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.

SSIDPrefix

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

password

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

isWep

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

joinOnce

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.

Errors:

  • 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.

Only Android

The following methods work only on Android

loadWifiList(): Promise<Array<WifiEntry>>

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.

Errors:

  • 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.

reScanAndLoadWifiList(): Promise<Array<WifiEntry>>

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

isEnabled(): Promise<boolean>

Method to check if WiFi is enabled.

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

setEnabled(enabled: boolean)

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

connectionStatus(): Promise<boolean>

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

disconnect()

Disconnect currently connected WiFi network.

getBSSID(): Promise<string>

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

getCurrentSignalStrength(): Promise<number>

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

getFrequency(): Promise<number>

Returns the frequency of the currently connected WiFi network.

getIP(): Promise<number>

Returns the IP of the currently connected WiFi network.

isRemoveWifiNetwork(ssid: String): Promise<boolean>

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

Errors:

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

forceWifiUsage(useWifi: boolean): Promise<void>

Deprecated; see forceWifiUsageWithOptions.

forceWifiUsageWithOptions(useWifi: boolean, options<Record<string, unknown>): Promise<void>

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.

options

  • noInternet: Boolean: Indicate the wifi network does not have internet connectivity.

Conventions

changelog

4.13.4 (2025-01-23)

Bug Fixes

  • types: Align types, index and native module interfaces (#416) (f15187e)

4.13.3 (2025-01-20)

Bug Fixes

  • ios: prevent crash when canceling WiFi connection (#425) (e398406), closes #424

4.13.2 (2025-01-18)

Bug Fixes

  • android: connection status support for per-app wifi connections (#420) (a0b8fb6), closes #419

4.13.1 (2024-12-30)

Bug Fixes

  • Android: properly check on pollForValidSSID if network connected (#399) (0371acd)

4.13.0 (2024-09-17)

Bug Fixes

  • add semicolon (561ea0a)
  • fix error argument is marked as nullable but React requires that all NSNumber arguments are nonnull (b3f4e84)
  • use node 20 for semantic-release (f516e8d)

Features

  • iOS: Add new method to connect to a SSIDPrefix protected only once, Android: Try to address issue #303 (d98ccfc)

4.12.1 (2024-03-28)

Bug Fixes

  • crash on Android when isHidden params not passed (e3d7dcc)

4.12.0 (2024-03-26)

Bug Fixes

Features

  • add new method connectToProtectedWifiSSID (1813073)
  • add nullable password (75ff05f)
  • android: add timeout parameter (9426913)
  • update README (8970ba4)

4.11.0 (2024-02-12)

Features

  • iOS, Loop patch: check the connected WiFi network repeatedly until it's the one we requested (#301) (e7088be)

4.10.1 (2023-11-06)

Bug Fixes

  • npm: remove engines from package.json (8bd1385)

4.10.0 (2023-10-27)

Features

  • android: 🌟 add support for React Native 0.73 (#312) (451b92c)

4.9.0 (2023-10-27)

Bug Fixes

  • Android: Open the Wifi settings panel on Android 10 and above. (#319) (bd89902)

Features

4.8.3 (2023-05-26)

Bug Fixes

  • iOS: add missing isHidden flag to connectToProtectedSSID (#306) (11a703d), closes #287

4.8.2 (2023-05-26)

Bug Fixes

  • prefer not to mutate the ScanResult object (#268) (840c80d)

4.8.1 (2023-05-26)

Bug Fixes

  • update android native dependencies (472896d)

4.8.0 (2023-05-26)

Features

  • Android: added feature to connect to hidden wifi on android 10 (#287) (32b5db2)

4.7.1 (2023-05-26)

Bug Fixes

  • Android: * add location permission check for getCurrentWifiSSID and reScanAndLoadWifiList and change error message for clarity (#264) (c3ba4e5)

4.7.0 (2022-07-12)

Features

  • Android: Allows to show hidden WiFI AP while scanning (#255) (29d364a)

4.6.0 (2022-06-09)

Features

  • Minor bug fix, iOS 15 getWifiSSID fix, connect to Android Wifi without scanning (because of the limit) (#249) (3389baf)

4.5.1 (2022-06-09)

Bug Fixes

  • Use enum types instead of const in the TypeScript declaration file (5e6dfeb)

4.5.0 (2021-12-16)

Features

4.4.2 (2021-12-16)

Bug Fixes

  • iOS: Revert wrong removal of promise resolution (#225) (89eda2d)

4.4.1 (2021-11-04)

Bug Fixes

  • Android: Replace jCenter with mavenCentral (#216) (b047208)
  • iOS: Fix disconnectFromSSID having no effect (#214) (0d55c08)
  • iOS: Fix prefix check in connectToProtectedSSIDPrefix (#220) (d94f356)

4.4.0 (2021-09-29)

Bug Fixes

  • Fix return type for forceWifiUsageWithOptions (#170) (6de4204)

Features

  • iOS: allow joinOnce property to be set for iOS devices (#187) (991f9cc)

4.3.8 (2021-05-26)

Bug Fixes

  • iOS: Reject after not connecting to WiFi with SSID-prefix (#183) (f255caa), closes #182

4.3.7 (2021-01-14)

Bug Fixes

  • Android: Replace Kotlin files with Java (#154) (1e15eac)

4.3.6 (2021-01-10)

Bug Fixes

  • Android: getCurrentWifiSSID return null if '<unknown ssid>' (#148) (336668a)

4.3.5 (2021-01-07)

Bug Fixes

  • Android: Add elvis dependency to build.gradle (#152) (1ab284a)

4.3.4 (2020-12-19)

Bug Fixes

4.3.3 (2020-10-19)

Bug Fixes

  • Add compileOptions JavaVersion.VERSION_1_8 (#133) (f28a8dc)

4.3.2 (2020-10-01)

Bug Fixes

4.3.1 (2020-09-22)

Bug Fixes

4.3.0 (2020-09-15)

Features

  • Android: Add forceWifiUsageWithOptions & add noInternet option (#115) (df1b73a)

4.2.2 (2020-08-27)

Bug Fixes

  • Fix reScanAndLoadWifiList() return type (#108) (431f624)

4.2.1 (2020-08-25)

Bug Fixes

  • loadWifiList and reScanAndLoadWifiList not returning a JSON array but a string (#104) (9dce618)

4.2.0 (2020-06-08)

Features

4.1.0 (2020-05-27)

Features

  • Add loadWifiList meaningfull Exceptions (#81) (cc8f7c1)

4.0.2 (2020-05-23)

Bug Fixes

  • iOS: Consistent joinOnce=false configuration (#72) (50cfc20)

4.0.1 (2020-05-23)

Bug Fixes

  • Fix Android 10 no-internet support (#82) (45d626c)

4.0.0 (2020-05-01)

  • Merge pull request #56 from inthepocket/feature/android-use-promises (83e30cd), closes #56

Features

  • promise: change isEnabled from callback to promise. (5631fe8)
  • promise: return the current signal strength as a promise (7ef9a40)
  • promise: use a promise instead of callsbacks for isRemoveWifiNetwork (abb3be2)
  • promise: use a promise to resolve the frequency of the currently connected WiFi network (79dc3bf)
  • promise: use a promise to return results for reScanAndLoadWifiList() (dcdeb0c)
  • promise: use a promise to return the bssid of the currenlty connected network (8d39c67)
  • promise: use a promise to return the loadWifiList results (e0fe2b9)

BREAKING CHANGES

  • Use promises instead of callbacks

3.1.2 (2020-04-16)

Bug Fixes

  • Add null type to password on connectToProtectedSSID (#60) (9ad28cc)

3.1.1 (2020-03-22)

Bug Fixes

  • add location permission explanation on the readme (12810b8)

3.1.0 (2020-03-22)

Features

  • forceWifiUsage: remove need of the WRITE_SETTINGS. Api calls to a wifi network without internet access can perfectly be done without (7c9daef)

3.0.0 (2020-03-17)

  • Merge pull request #46 from inthepocket/feature/use-wifiutils-for-android-with-semantics (eeea1af), closes #46

BREAKING CHANGES

  • Use WifiUtils to connect with a wifi network

2.4.0 (2020-03-04)

Features

2.3.4 (2020-03-04)

Performance Improvements

2.3.3 (2020-02-27)

Bug Fixes

  • iOS: Fix joinOnce for connectToSSIDPrefix (#40) (61185a9)

2.3.2 (2020-01-21)

Bug Fixes

  • android: Verify connection after enabling network (#31) (9fa86ee)

2.3.1 (2020-01-16)

Bug Fixes

2.3.0 (2020-01-16)

Features

  • iOS: Connect to SSID prefix functions. (#25) (5b52c97)

2.2.4 (2019-12-18)

Performance Improvements

  • test: test perf semantic-release (0d42570)

2.2.3 (2019-12-12)

Bug Fixes

  • iOS: getCurrentWifiSSID when is deneided or restrited (1c6d1fd)

2.2.2 (2019-12-11)

Bug Fixes

2.2.1 (2019-12-10)

Bug Fixes

  • android: Added network compatibility (331a40f)

2.2.0 (2019-11-19)

Features

  • Android: Update all library (f4cc526)

2.1.1 (2019-11-15)

Bug Fixes

  • Android: Rename package to avoid colisions (17da67e)

2.1.0 (2019-11-03)

Features

  • Android: Update gradle configuration (475cc30), closes #5

2.0.4 (2019-11-03)

Bug Fixes

  • ios: Change settingsURL value so Apple accepts it. Referencing comments from: (653330e)

2.0.3 (2019-10-25)

Bug Fixes

2.0.2 (2019-10-24)

Bug Fixes

  • semantic-release: Add more extensions (40f0e01)