Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-wifi')
implementation project(':react-native-wifi')
```


Expand All @@ -53,4 +53,4 @@ WifiManager.getCurrentWifiSSID()
console.log('Cannot get current SSID!')
})
```


4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ android {
}

dependencies {
compile 'com.facebook.react:react-native:+'
implementation 'com.facebook.react:react-native:+'
}


129 changes: 62 additions & 67 deletions android/src/main/java/com/reactlibrary/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ public void loadWifiList(Callback successCallback, Callback errorCallback) {

//Method to force wifi usage if the user needs to send requests via wifi
//if it does not have internet connection. Useful for IoT applications, when
//the app needs to communicate and send requests to a device that have no
//the app needs to communicate and send requests to a device that have no
//internet connection via wifi.

//Receives a boolean to enable forceWifiUsage if true, and disable if false.
//Is important to enable only when communicating with the device via wifi
//Is important to enable only when communicating with the device via wifi
//and remember to disable it when disconnecting from device.
@ReactMethod
public void forceWifiUsage(boolean useWifi) {
boolean canWriteFlag = false;

if (useWifi) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

Expand Down Expand Up @@ -207,85 +207,80 @@ public void connectionStatus(Callback connectionStatusResult) {

//Method to connect to WIFI Network
public Boolean connectTo(ScanResult result, String password, String ssid) {
if (!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
}

//Make new configuration
WifiConfiguration conf = new WifiConfiguration();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

conf.SSID = ssid;
} else {
conf.SSID = "\"" + ssid + "\"";
}

String capabilities = result.capabilities;

if (capabilities.contains("WPA") ||
capabilities.contains("WPA2") ||
capabilities.contains("WPA/WPA2 PSK")) {

// appropriate ciper is need to set according to security type used,
// ifcase of not added it will not be able to connect
conf.preSharedKey = "\"" + password + "\"";

conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

conf.status = WifiConfiguration.Status.ENABLED;

conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

} else if (capabilities.contains("WEP")) {
conf.wepKeys[0] = "\"" + password + "\"";
conf.wepTxKeyIndex = 0;

if (capabilities.toUpperCase().contains("WPA")) {
conf.SSID = String.format("\"%s\"", ssid);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.preSharedKey = String.format("\"%s\"", password);
} else if (capabilities.toUpperCase().contains("WEP")) {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
if (password.matches("^[0-9a-fA-F]+$")) {
conf.wepKeys[0] = password;
} else {
conf.wepKeys[0] = "\"".concat(password).concat("\"");
}
} else {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
conf.allowedAuthAlgorithms.clear();
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
}

//Remove the existing configuration for this netwrok
List<WifiConfiguration> mWifiConfigList = wifi.getConfiguredNetworks();

int updateNetwork = -1;

for(WifiConfiguration wifiConfig : mWifiConfigList){
if(wifiConfig.SSID.equals(conf.SSID)){
conf.networkId = wifiConfig.networkId;
updateNetwork = wifi.updateNetwork(conf);
if (mWifiConfigList != null) {
for (WifiConfiguration wifiConfig : mWifiConfigList) {
if (wifiConfig.SSID.equals(conf.SSID)) {
conf.networkId = wifiConfig.networkId;
updateNetwork = wifi.updateNetwork(conf);
}
}
}

// If network not already in configured networks add new network
if ( updateNetwork == -1 ) {
updateNetwork = wifi.addNetwork(conf);
wifi.saveConfiguration();
};

if ( updateNetwork == -1 ) {
return false;
}

boolean disconnect = wifi.disconnect();
if ( !disconnect ) {
return false;
};

boolean enableNetwork = wifi.enableNetwork(updateNetwork, true);
if ( !enableNetwork ) {
/// If network not already in configured networks add new network
if (updateNetwork == -1) {
updateNetwork = wifi.addNetwork(conf);
wifi.saveConfiguration();
}
if (updateNetwork == -1) {
return false;
};
}

return true;
wifi.enableNetwork(updateNetwork, true);
return wifi.reconnect();
}

//Disconnect current Wifi.
Expand Down Expand Up @@ -326,12 +321,12 @@ public void getCurrentSignalStrength(final Callback callback) {
}

//This method will return current wifi frequency
@ReactMethod
public void getFrequency(final Callback callback) {
WifiInfo info = wifi.getConnectionInfo();
int frequency = info.getFrequency();
callback.invoke(frequency);
}
// @ReactMethod
////// public void getFrequency(final Callback callback) {
////// WifiInfo info = wifi.getConnectionInfo();
////// int frequency = info.getFrequency();
////// callback.invoke(frequency);
////// }

//This method will return current IP
@ReactMethod
Expand Down
10 changes: 8 additions & 2 deletions ios/RNWifi.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
//#import <UIKit/UIKit.h>

@implementation WifiManager

+ (BOOL)requiresMainQueueSetup
{
return YES;
}

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(connectToSSID:(NSString*)ssid
Expand All @@ -13,7 +19,7 @@ @implementation WifiManager

if (@available(iOS 11.0, *)) {
NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid];
configuration.joinOnce = true;
configuration.joinOnce = false;

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
Expand All @@ -36,7 +42,7 @@ @implementation WifiManager

if (@available(iOS 11.0, *)) {
NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:passphrase isWEP:isWEP];
configuration.joinOnce = true;
configuration.joinOnce = false;

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
Expand Down