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
Permissions: Starting with Android API 25, apps must be granted the ACCESS_COARSE_LOCATION (or ACCESS_FINE_LOCATION) permission in order to obtain results.
... const permission = PermissionsAndroidPERMISSIONSACCESS_COARSE_LOCATIONconst options = 'title': 'Wifi networks' 'message': 'We need your permission in order to find wifi networks' PermissionsAndroid
Initialize react-native-wifi-direct
. This should be done in your index.js
(index.android.js
) or root level app.js
file.
WifiDirect
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
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
Register an event to listen for when devices are found. This event will be called every time your device updates its network list.
{ WifiDirect} // Don't forget to remove the listener to prevent a memory leak { WifiDirect} { console}
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
The CONNECTION_INFO_UPDATED
event is triggered when a connection is successfully established.
{ WifiDirect} // Don't forget to remove the listener to prevent a memory leak { WifiDirect} { console}
WifiDirect