Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let this lib compatible with simulator #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml
*.log

# node.js
#
node_modules
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

*/fastlane/report.xml
*/fastlane/Preview.html
*/fastlane/screenshots

# Bundle artifact
*.jsbundle

# vscode
.vscode
.project
org.eclipse.*

# Android/res
android/app/src/main/res/drawable*
48 changes: 38 additions & 10 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
/* buildscript {
repositories {
jcenter()
google()
}

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}

repositories {
jcenter()
google()
maven {
url "$rootDir/../node_modules/react-native/android"
}
maven { url 'https://jitpack.io' }
}*/

apply plugin: 'com.android.library'

def _ext = rootProject.ext;

def _reactNativeVersion = _ext.has('reactNative') ? _ext.reactNative : '+';
def _compileSdkVersion = _ext.has('compileSdkVersion') ? _ext.compileSdkVersion : 28;
def _buildToolsVersion = _ext.has('buildToolsVersion') ? _ext.buildToolsVersion : '26.0.3';
def _minSdkVersion = _ext.has('minSdkVersion') ? _ext.minSdkVersion : 16;
def _targetSdkVersion = _ext.has('targetSdkVersion') ? _ext.targetSdkVersion : 22;

android {
compileSdkVersion _compileSdkVersion
buildToolsVersion _buildToolsVersion

defaultConfig {
minSdkVersion _minSdkVersion
targetSdkVersion _targetSdkVersion
versionCode 20190325
versionName "1.1"
}
}

dependencies {
compile 'com.facebook.react:react-native:+'
}
Expand Down
49 changes: 43 additions & 6 deletions android/src/main/java/com/reactlibrary/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,49 @@ public void disconnect() {
//This method will return current ssid
@ReactMethod
public void getCurrentWifiSSID(final Promise promise) {
WifiInfo info = wifi.getConnectionInfo();
/* WifiInfo info = wifi.getConnectionInfo();

// This value should be wrapped in double quotes, so we need to unwrap it.
String ssid = info.getSSID();
if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
ssid = ssid.substring(1, ssid.length() - 1);
String ssid = info.getSSID(); */
String ssid = null;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O||Build.VERSION.SDK_INT==Build.VERSION_CODES.P) {

WifiManager mWifiManager = wifi;

assert mWifiManager != null;
WifiInfo info = mWifiManager.getConnectionInfo();

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
ssid = info.getSSID();
} else {
int networId = info.getNetworkId();
List<WifiConfiguration> netConfList = mWifiManager.getConfiguredNetworks();
for(WifiConfiguration wificonf:netConfList){
if(wificonf.networkId == networId){
ssid = wificonf.SSID.replace("\"", "");
}
}
// ssid = info.getSSID().replace("\"", "");
}

} else if (Build.VERSION.SDK_INT==Build.VERSION_CODES.O_MR1){

ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
assert connManager != null;
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo.isConnected()) {
if (networkInfo.getExtraInfo()!=null){
ssid = networkInfo.getExtraInfo().replace("\"","");
}
}

/* ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
String ssid = networkInfo.getExtraInfo();

if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
ssid = ssid.substring(1, ssid.length() - 1);
} */
}

promise.resolve(ssid);
Expand Down Expand Up @@ -335,10 +372,10 @@ public void getFrequency(final Callback callback) {

//This method will return current IP
@ReactMethod
public void getIP(final Callback callback) {
public void getIP(final Promise promise) {
WifiInfo info = wifi.getConnectionInfo();
String stringip = longToIP(info.getIpAddress());
callback.invoke(stringip);
promise.resolve(stringip);
}

//This method will remove the wifi network as per the passed SSID from the device list
Expand Down
89 changes: 83 additions & 6 deletions ios/RNWifi.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
#import "RNWifi.h"
#if TARGET_IPHONE_SIMULATOR
#else
#import <NetworkExtension/NetworkExtension.h>
#endif
#import <SystemConfiguration/CaptiveNetwork.h>
// If using official settings URL
//#import <UIKit/UIKit.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
#include <net/if.h>

#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
//#define IOS_VPN @"utun0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"

@implementation WifiManager
RCT_EXPORT_MODULE();

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

#if TARGET_IPHONE_SIMULATOR
reject(@"ios_error", @"Not supported in simulator", nil);
#else
if (@available(iOS 11.0, *)) {
NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid];
configuration.joinOnce = true;
Expand All @@ -26,14 +40,17 @@ @implementation WifiManager
} else {
reject(@"ios_error", @"Not supported in iOS<11.0", nil);
}
#endif
}

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

#if TARGET_IPHONE_SIMULATOR
reject(@"ios_error", @"Not supported in simulator", nil);
#else
if (@available(iOS 11.0, *)) {
NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:passphrase isWEP:isWEP];
configuration.joinOnce = true;
Expand All @@ -49,12 +66,15 @@ @implementation WifiManager
} else {
reject(@"ios_error", @"Not supported in iOS<11.0", nil);
}
#endif
}

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

#if TARGET_IPHONE_SIMULATOR
reject(@"ios_error", @"Not supported in simulator", nil);
#else
if (@available(iOS 11.0, *)) {
[[NEHotspotConfigurationManager sharedManager] getConfiguredSSIDsWithCompletionHandler:^(NSArray<NSString *> *ssids) {
if (ssids != nil && [ssids indexOfObject:ssid] != NSNotFound) {
Expand All @@ -65,7 +85,7 @@ @implementation WifiManager
} else {
reject(@"ios_error", @"Not supported in iOS<11.0", nil);
}

#endif
}

RCT_REMAP_METHOD(getCurrentWifiSSID,
Expand All @@ -86,12 +106,69 @@ @implementation WifiManager
reject(@"cannot_detect_ssid", @"Cannot detect SSID", nil);
}

- (NSDictionary*)constantsToExport {
RCT_EXPORT_METHOD(getIP:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSArray *searchArray = @[ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv4 ];
NSDictionary *addresses = [self getAllIPAddresses];
NSLog(@"addresses: %@", addresses);

__block NSString *address;
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
{
address = addresses[key];
if(address) *stop = YES;
} ];
NSString *addressToReturn = address ? address : @"0.0.0.0";
resolve(@[addressToReturn]);
}

- (NSDictionary *)getAllIPAddresses
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];

// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}

/* - (NSDictionary*)constantsToExport {
// Officially better to use UIApplicationOpenSettingsURLString
return @{
@"settingsURL": @"App-Prefs:root=WIFI"
};
}
} */

@end