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-wifi-direct

v0.2.3

Published

A react native module for connecting and disconnecting from Android Wi-Fi Direct networks (p2p).

Downloads

23

Readme

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?")
  }
})