Coder Social home page Coder Social logo

mvpdream / react-native-android-wifi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from devstepbcn/react-native-android-wifi

1.0 2.0 0.0 18 KB

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

License: ISC License

Java 98.04% JavaScript 1.96%

react-native-android-wifi's Introduction

react-native-android-wifi

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

Installation

Add it to your android project

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

Install the native dependencies

You can use rnpm to add native dependencies automatically:

$ rnpm link

or do it manually as described below:

  • In android/setting.gradle
...
include ':app'
include ':react-native-android-wifi'
project(':react-native-android-wifi').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-wifi/android')
  • In android/app/build.gradle
...
dependencies {
    ...
  compile project(':react-native-android-wifi')
}
  • register module (in MainActivity.java)

On newer versions of React Native (0.18+):

import com.devstepbcn.wifi.AndroidWifiPackage;  // <--- import

public class MainActivity extends ReactActivity {
  ......
  
  /**
   * A list of packages used by the app. If the app uses additional views
   * or modules besides the default ones, add more packages here.
   */
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new AndroidWifiPackage(), // <------ add here
        new MainReactPackage());
    }
}

On older versions of React Native:

import com.devstepbcn.wifi.AndroidWifiPackage;  // <--- import

public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
  ......

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mReactRootView = new ReactRootView(this);

    mReactInstanceManager = ReactInstanceManager.builder()
      .setApplication(getApplication())
      .setBundleAssetName("index.android.bundle")
      .setJSMainModuleName("index.android")
      .addPackage(new MainReactPackage())
      .addPackage(new AndroidWifiPackage())              // <------ add here
      .setUseDeveloperSupport(BuildConfig.DEBUG)
      .setInitialLifecycleState(LifecycleState.RESUMED)
      .build();

    mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);

    setContentView(mReactRootView);
  }

  ......

}

Example usage

var wifi = require('react-native-android-wifi')

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:

//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");
  }
});

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);
});

react-native-android-wifi's People

Contributors

devstepbcn avatar gsinovsky avatar mvpdream avatar ymrdf avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.