npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-android-wifi

v0.0.41

Published

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

Downloads

7,767

Readme

react-native-android-wifi

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

example app

Installation

Add it to your android project

npm install react-native-android-wifi --save

Install the native dependencies

Use react-native link to install native dependencies automatically:

react-native link react-native-android-wifi

or do it manually as described here.

Example usage

import wifi from 'react-native-android-wifi';

Permissions: Starting with Android API 25, apps must be granted the ACCESS_COARSE_LOCATION (or ACCESS_FINE_LOCATION) permission in order to obtain results.

try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          'title': 'Wifi networks',
          'message': 'We need your permission in order to find wifi networks'
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("Thank you for your permission! :)");
      } else {
        console.log("You will not able to retrieve wifi available networks list");
      }
    } catch (err) {
      console.warn(err)
    }

Wifi connectivity status:

wifi.isEnabled((isEnabled) => {
  if (isEnabled) {
    console.log("wifi service enabled");
  } else {
    console.log("wifi service is disabled");
  }
});

Enable/Disable wifi service:

//Set TRUE to enable and FALSE to disable; 
wifi.setEnabled(true);

Sign device into a specific network:

This method doesn't have a callback when connection succeeded, check this issue. Added support for 'WPA2 PSK' wifi security mode and handling SSID for Lollipop and Kitkat.

//found returns true if ssid is in the range
wifi.findAndConnect(ssid, password, (found) => {
  if (found) {
    console.log("wifi is in range");
  } else {
    console.log("wifi is not in range");
  }
});

Disconnect from current wifi network

wifi.disconnect();

Get current SSID

wifi.getSSID((ssid) => {
  console.log(ssid);
});

Get current BSSID

wifi.getBSSID((bssid) => {
  console.log(bssid);
});

Get all wifi networks in range

/*
wifiStringList is a stringified JSONArray with the following fields for each scanned wifi
{
  "SSID": "The network name",
  "BSSID": "The address of the access point",
  "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. (Remember its a negative value)",
  "timestamp":"Timestamp in microseconds (since boot) when this result was last seen"
}
*/
wifi.loadWifiList((wifiStringList) => {
  var wifiArray = JSON.parse(wifiStringList);
    console.log(wifiArray);
  },
  (error) => {
    console.log(error);
  }
);

connectionStatus returns true or false depending on whether device is connected to wifi

wifi.connectionStatus((isConnected) => {
  if (isConnected) {
      console.log("is connected");
    } else {
      console.log("is not connected");
  }
});

Get connected wifi signal strength

//level is the detected signal level in dBm, also known as the RSSI. (Remember its a negative value)
wifi.getCurrentSignalStrength((level) => {
  console.log(level);
});

Get connected wifi frequency

wifi.getFrequency((frequency) => {
  console.log(frequency);
})

Get current IP

//get the current network connection IP
wifi.getIP((ip) => {
  console.log(ip);
});

Get DHCP Server Adress

//get the DHCP server IP
wifi.getDhcpServerAddress((ip) => {
  console.log(ip);
});

Remove/Forget the Wifi network from mobile by SSID, returns boolean This method will remove the wifi network as per the passed SSID from the device list.

wifi.isRemoveWifiNetwork(ssid, (isRemoved) => {
  console.log("Forgetting the wifi device - " + ssid);
});

Starts native Android wifi network scanning and returns list Hard refresh the Android wifi scan, implemented using BroadcastReceiver to ensure that it automatically detects new wifi connections available.

wifi.reScanAndLoadWifiList((wifiStringList) => {
  var wifiArray = JSON.parse(wifiStringList);
  console.log('Detected wifi networks - ',wifiArray);
},(error)=>{
  console.log(error);
});

Method to force wifi usage. Android by default sends all requests via mobile data if the connected wifi has no internet connection.

//Set true/false to enable/disable forceWifiUsage.
//Is important to enable only when communicating with the device via wifi
//and remember to disable it when disconnecting from device.
wifi.forceWifiUsage(true);

Method to get connection status of a forced network (because it takes some time to be set up).

//Callback returns true if the process of forcing network usage is finished
wifi.connectionStatusOfBoundNetwork((isBound) => {
    if (isBound) {
        console.log('Network is bound');
    } else {
        console.log('Network isn\'t bound');
    }
});

Add a hidden wifi network and connect to it

//Callback returns true if network added and tried to connect to it successfully
//It may take up to 15s to connect to hidden networks
wifi.connectToHiddenNetwork(ssid, password, (networkAdded) => {});