Coder Social home page Coder Social logo

react-native-wifi-direct's Introduction

react-native-wifi-direct

This currently only supports Android

A react native module for connecting and disconnecting from Android Wi-Fi Direct networks (p2p). This was built to be used with IOT devices that broadcast a Wi-Fi Direct network.

Installation

Install library from npm

npm install react-native-wifi-direct

Then link the library:

react-native link react-native-wifi-direct

Example Usage

import WifiDirect from 'react-native-wifi-direct'

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

import { PermissionsAndroid } from 'react-native'

...

const permission = PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION
const options = {
  'title': 'Wifi networks',
  'message': 'We need your permission in order to find wifi networks'
}

PermissionsAndroid.request(permission, options).then((granted) => {
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
    console.log("Permission granted!");
  } else {
    console.log("You will not able to retrieve wifi available networks list")
  }
}).catch((error) => {
  console.warn(error)
})

Initialize react-native-wifi-direct. This should be done in your index.js (index.android.js) or root level app.js file.

WifiDirect.initialize()

Start scanning for Wi-Fi Direct networks in the area. The scan will continue until a connection is initiated. The scan does not return any results. It only returns whether it successfully started.

WifiDirect.discoverPeers().then((success) => {
  if (success) {
    console.log("Peer discovery has initiated successfully.")
  } else {
    console.log("Peer discover failed to initiate.  Is your Wi-Fi on?")
  }
})

If you need to stop peer discovery to do non Wi-Fi Direct scan, you can call stopPeerDiscovery. Note that discoverPeers will stop on it's own if you connect to a Wi-Fi Direct network.

WifiDirect.stopPeerDiscovery().then((success) => {
  if (success) {
    console.log("Peer discovery will stop.")
  } else {
    console.log("Peer discovery can not be stopped.")
  }
})

Register an event to listen for when devices are found. This event will be called every time your device updates its network list.

componentWillMount () {
  WifiDirect.addListener('PEERS_UPDATED', this.peersUpdated)
}

// Don't forget to remove the listener to prevent a memory leak
componentWillUnmount () {
  WifiDirect.removeListener('PEERS_UPDATED', this.peersUpdated)
}

peersUpdated = (event) => {
  console.log("Devices found!", event.devices)
}

Connect to a Wi-Fi Direct network. The devices found from event.devices in the PEERS_UPDATED listener, will have a MAC address that is used to connect.

WifiDirect.connect(device.address).then((success) => {
  if (success) {
    console.log("Connection has initiated.")
  } else {
    console.log("Connection failed to initiated.  Check your Wi-Fi.")
  }
})

The CONNECTION_INFO_UPDATED event is triggered when a connection is successfully established.

componentWillMount () {
  WifiDirect.addListener('CONNECTION_INFO_UPDATED', this.connectionInfoUpdated)
}

// Don't forget to remove the listener to prevent a memory leak
componentWillUnmount () {
  WifiDirect.removeListener('CONNECTION_INFO_UPDATED', this.connectionInfoUpdated)
}

connectionInfoUpdated = (event) => {
  console.log("Connection established!", event.connectionInfo)
}
WifiDirect.disconnect().then((success) => {
  if (success) {
    console.log("Disconnecting initiated.")
  } else {
    console.log("Disconnect initiation failed.  Are you already disconnected?")
  }
})

react-native-wifi-direct's People

Contributors

csto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-native-wifi-direct's Issues

Support creating groups

I really like how focused this module is, but it's missing the ability to create groups which makes it less useful for situations where you want your app to act as a host.

Would a PR that adds support for that functionality be welcome?

did it work with expo?

please provide some details about how we can use react-native wifi-direct with expo???

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.