Skip to content

Commit

Permalink
feat(iOS): Connect to SSID prefix functions. (#25)
Browse files Browse the repository at this point in the history
* Added connectToProtectedSSIDPrefix function for iOS users version 13+.

* Added connectToProtectedSSIDPrefix to documentation. Corrected isWeb description to isWep with greater explanation.

* Reverted joinOncne back to true.

* Fixed error message on connectToProtectedSSID.

* connectToSSIDPrefix function and updated README.

Co-authored-by: Juan Sebastian Dussan Cubides <[email protected]>
  • Loading branch information
jkaravakis and JuanSeBestia committed Jan 16, 2020
1 parent 81c9f44 commit 5b52c97
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 18 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,32 @@ Type: `string`

The password of the wifi network to connect with.

#### isWEP
#### isWep
Type: `boolean`
Used on iOS. If YES, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network.

#### Errors:
* `notInRange`: The WIFI network is not currently in range.
* `addOrUpdateFailed`: Could not add or update the network configuration.
* `disconnectFailed`: Disconnecting from the network failed. This is done as part of the connect flow.
* `connectNetworkFailed`: Could not connect to network.

### connectToProtectedSSIDPrefix(SSIDPrefix: string, password: string, isWep: boolean): Promise

Use this function when you want to match a known SSID prefix, but don’t have a full SSID. If the system finds multiple Wi-Fi networks whose SSID string matches the given prefix, it selects the network with the greatest signal strength.

#### SSIDPrefix
Type: `string`
A prefix string to match the SSID of a Wi-Fi network.

#### password
Type: `string`
The password of the wifi network to connect with.

#### isWep
Type: `boolean`
Used on iOS. If YES, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network.

Used on iOS.

#### Errors:

Expand All @@ -169,20 +190,22 @@ The following methods work only on iOS

### `connectToSSID(ssid: string): Promise`

### `connectToSSIDPrefix(ssid: string): Promise`

### `disconnectFromSSID(ssid: string): Promise`

## Only Android
The following methods work only on Android

### `loadWifiList(successCallback: function, errorCallback: function)`
### `loadWifiList(successCallback: function, errorCallback: function)`

Method to get a list of nearby WiFI networks.

#### successCallback( wifiList: string )

Type: `function`

Function to be called if the attempt is successful. It contains a stringified JSONArray of wifiObjects as parameter, each object containing:
Function to be called if the attempt is successful. It contains a stringified JSONArray of wifiObjects as parameter, each object containing:

* `SSID`: The network name.
* `BSSID`: The WiFi BSSID.
Expand All @@ -197,7 +220,7 @@ Type: `function`

Function to be called if any error occurs during the attempt. It contains a `string` as parameter with the error message.

#### Usage
#### Usage

```javascript
WifiManager.loadWifiList(
Expand All @@ -210,7 +233,7 @@ WifiManager.loadWifiList(
error => console.log(error)
);
/**
Result:
Result:
"Wifi 1 - Name of the network"
"Wifi 2 - Name of the network"
"Wifi 3 - Name of the network"
Expand All @@ -222,7 +245,7 @@ Result:

This method is similar to `loadWifiList` but it forcefully starts the wifi scanning on android and in the callback fetches the list.

#### Usage
#### Usage

Same as `loadWifiList`.

Expand Down Expand Up @@ -281,4 +304,4 @@ If you want to use it, you need to add the `android.permission.WRITE_SETTINGS` p

</manifest>

```
```
62 changes: 53 additions & 9 deletions ios/RNWifi.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatu

- (NSString *) getWifiSSID {
NSString *kSSID = (NSString*) kCNNetworkInfoKeySSID;

NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *ifnam in ifs) {
NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
Expand All @@ -53,42 +53,86 @@ + (BOOL)requiresMainQueueSetup
RCT_EXPORT_METHOD(connectToSSID:(NSString*)ssid
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

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

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
reject(@"nehotspot_error", @"Error while configuring WiFi", error);
} else {
resolve(nil);
}
}];

} else {
reject(@"ios_error", @"Not supported in iOS<11.0", nil);
}
}

RCT_EXPORT_METHOD(connectToSSIDPrefix:(NSString*)ssid
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

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

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
reject(@"nehotspot_error", @"Error while configuring WiFi", error);
} else {
resolve(nil);
}
}];

} else {
reject(@"ios_error", @"Not supported in iOS<13.0", nil);
}
}

RCT_EXPORT_METHOD(connectToProtectedSSIDPrefix:(NSString*)ssid
withPassphrase:(NSString*)passphrase
isWEP:(BOOL)isWEP
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

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

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
reject(@"nehotspot_error", @"Error while configuring WiFi", error);
} else {
resolve(nil);
}
}];

} else {
reject(@"ios_error", @"Not supported in iOS<13.0", nil);
}
}

RCT_EXPORT_METHOD(connectToProtectedSSID:(NSString*)ssid
withPassphrase:(NSString*)passphrase
isWEP:(BOOL)isWEP
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

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

[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
if (error != nil) {
reject(@"nehotspot_error", @"Error while configuring WiFi", error);
} else {
resolve(nil);
}
}];

} else {
reject(@"ios_error", @"Not supported in iOS<11.0", nil);
}
Expand All @@ -97,7 +141,7 @@ + (BOOL)requiresMainQueueSetup
RCT_EXPORT_METHOD(disconnectFromSSID:(NSString*)ssid
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

if (@available(iOS 11.0, *)) {
[[NEHotspotConfigurationManager sharedManager] getConfiguredSSIDsWithCompletionHandler:^(NSArray<NSString *> *ssids) {
if (ssids != nil && [ssids indexOfObject:ssid] != NSNotFound) {
Expand All @@ -108,7 +152,7 @@ + (BOOL)requiresMainQueueSetup
} else {
reject(@"ios_error", @"Not supported in iOS<11.0", nil);
}

}


Expand Down

0 comments on commit 5b52c97

Please sign in to comment.