Skip to content

Commit

Permalink
Added isWiFiEnabled implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
caiocruz committed May 20, 2016
1 parent a1a4beb commit c9c47d6
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions src/ios/NXWWifiWizard.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#import "NXWWifiWizard.h"
#include <ifaddrs.h>
#import <net/if.h>
#import <SystemConfiguration/CaptiveNetwork.h>


@implementation NXWWifiWizard

- (id)fetchSSIDInfo {
Expand All @@ -16,56 +19,74 @@ - (id)fetchSSIDInfo {
return info;
}

- (BOOL) isWiFiEnabled {
// see http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick
NSCountedSet * cset = [NSCountedSet new];

struct ifaddrs *interfaces = NULL;
// retrieve the current interfaces - returns 0 on success
int success = getifaddrs(&interfaces);
if(success == 0){
for( struct ifaddrs *interface = interfaces; interface; interface = interface->ifa_next) {
if ( (interface->ifa_flags & IFF_UP) == IFF_UP ) {
[cset addObject:[NSString stringWithUTF8String:interface->ifa_name]];
}
}
}

return [cset countForObject:@"awdl0"] > 1 ? YES : NO;
}

- (void)addNetwork:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (void)removeNetwork:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (void)connectNetwork:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (void)disconnectNetwork:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (void)listNetworks:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (void)getScanResults:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}
Expand All @@ -81,9 +102,9 @@ - (void)startScan:(CDVInvokedUrlCommand*)command {

- (void)disconnect:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}
Expand All @@ -107,20 +128,26 @@ - (void)getConnectedSSID:(CDVInvokedUrlCommand*)command {
- (void)getConnectedBSSID:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;
NSDictionary *r = [self fetchSSIDInfo];

NSString *bssid = [r objectForKey:(id)kCNNetworkInfoKeyBSSID]; //@"SSID"

if (bssid && [bssid length]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:bssid];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"];
}

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}
- (void)isWifiEnabled:(CDVInvokedUrlCommand*)command {

CDVPluginResult *pluginResult = nil;
NSString *isWifiOn = [self isWiFiEnabled] ? @"1" : @"0";

[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:isWifiOn];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (void)setWifiEnabled:(CDVInvokedUrlCommand*)command {
Expand Down

0 comments on commit c9c47d6

Please sign in to comment.