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

throw error to JS and log it #104

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
16 changes: 8 additions & 8 deletions NetworkInfo.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export namespace NetworkInfo {
function getSSID(): Promise<string | null>;
function getBSSID(): Promise<string | null>;
function getBroadcast(): Promise<string | null>;
function getIPAddress(): Promise<string | null>;
function getIPV4Address(): Promise<string | null>;
function getSubnet(): Promise<string | null>;
function getGatewayIPAddress(): Promise<string | null>;
function getFrequency(): Promise<number | null>;
function getSSID(fallback: any): Promise<string | null>;
function getBSSID(fallback: any): Promise<string | null>;
function getBroadcast(fallback: any): Promise<string | null>;
function getIPAddress(fallback: any): Promise<string | null>;
function getIPV4Address(fallback: any): Promise<string | null>;
function getSubnet(fallback: any): Promise<string | null>;
function getGatewayIPAddress(fallback: any): Promise<string | null>;
function getFrequency(fallback: any): Promise<number | null>;
}
52 changes: 31 additions & 21 deletions NetworkInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,56 @@
import { NativeModules, Platform } from "react-native";
const { RNNetworkInfo } = NativeModules;

async function handleError(callback, fallback = null) {
try {
return await callback();
} catch (error) {
if (__DEV__) console.log(error);

return fallback;
}
}

const NetworkInfo = {
async getSSID() {
return await RNNetworkInfo.getSSID();
async getSSID(fallback) {
return await handleError(RNNetworkInfo.getSSID, fallback);
},

async getBSSID() {
return await RNNetworkInfo.getBSSID();
async getBSSID(fallback) {
return await handleError(RNNetworkInfo.getBSSID, fallback);
},

async getBroadcast() {
return await RNNetworkInfo.getBroadcast();
async getBroadcast(fallback) {
return await handleError(RNNetworkInfo.getBroadcast, fallback);
},

async getIPAddress() {
return await RNNetworkInfo.getIPAddress();
async getIPAddress(fallback) {
return await handleError(RNNetworkInfo.getIPAddress, fallback);
},

async getIPV4Address() {
const wifiIP = await RNNetworkInfo.getWIFIIPV4Address();
if (wifiIP && wifiIP !== '0.0.0.0') {
async getIPV4Address(fallback) {
const wifiIP = await handleError(RNNetworkInfo.getWIFIIPV4Address);
if (wifiIP) {
return wifiIP;
}
return await RNNetworkInfo.getIPV4Address();

return await handleError(RNNetworkInfo.getIPV4Address, fallback);
},

async getGatewayIPAddress() {
return await RNNetworkInfo.getGatewayIPAddress();
async getGatewayIPAddress(fallback) {
return await handleError(RNNetworkInfo.getGatewayIPAddress, fallback);
},

async getSubnet() {
return await RNNetworkInfo.getSubnet();
async getSubnet(fallback) {
return await handleError(RNNetworkInfo.getSubnet, fallback);
},

async getFrequency() {
async getFrequency(fallback) {
if (Platform.OS !== 'android') {
return null;
return fallback;
}
return await RNNetworkInfo.getFrequency();
}
return await handleError(RNNetworkInfo.getFrequency, fallback);
},
};

module.exports = { NetworkInfo };
49 changes: 33 additions & 16 deletions android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import androidx.annotation.NonNull;

import android.net.wifi.SupplicantState;
import android.util.Log;

Expand Down Expand Up @@ -57,10 +60,12 @@ public void run() {
if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
ssid = ssid.substring(1, ssid.length() - 1);
}
} else {
throw new Exception("The Supplicant State has not been completed");
}
promise.resolve(ssid);
} catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}

}
Expand All @@ -77,11 +82,13 @@ public void run() {
// https://stackoverflow.com/a/34848930/5732760
String bssid = null;
if (info.getSupplicantState() == SupplicantState.COMPLETED) {
bssid = wifi.getConnectionInfo().getBSSID();
bssid = info.getBSSID();
} else {
throw new Exception("The Supplicant State has not been completed");
}
promise.resolve(bssid);
} catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}

}
Expand All @@ -101,9 +108,12 @@ public void run() {
ipAddress = address.getBroadcast().toString();
}
}
if (ipAddress == null) {
throw new Exception("Broadcast address not found");
}
promise.resolve(ipAddress);
} catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}

}
Expand All @@ -126,9 +136,12 @@ public void run() {
}
}
}
if (ipAddress == null) {
throw new Exception("IP address not found");
}
promise.resolve(ipAddress);
} catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}

}
Expand All @@ -152,9 +165,12 @@ public void run() {
}
}
}
if (ipAddress == null) {
throw new Exception("IPV4 address not found");
}
promise.resolve(ipAddress);
} catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}

}
Expand All @@ -176,7 +192,7 @@ public void run() {
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
promise.resolve(stringip);
}catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}
}
}).start();
Expand Down Expand Up @@ -205,8 +221,9 @@ public void run() {
return;
}
}
throw new Exception("Subnet not found");
} catch (Exception e) {
promise.resolve("0.0.0.0");
promise.reject(e);
}
}
}).start();
Expand All @@ -220,15 +237,15 @@ public void run() {
DhcpInfo dhcpInfo = wifi.getDhcpInfo();
int gatewayIPInt = dhcpInfo.gateway;
String gatewayIP = String.format(
"%d.%d.%d.%d",
((gatewayIPInt) & 0xFF),
((gatewayIPInt >> 8 ) & 0xFF),
((gatewayIPInt >> 16) & 0xFF),
((gatewayIPInt >> 24) & 0xFF)
"%d.%d.%d.%d",
((gatewayIPInt) & 0xFF),
((gatewayIPInt >> 8 ) & 0xFF),
((gatewayIPInt >> 16) & 0xFF),
((gatewayIPInt >> 24) & 0xFF)
);
promise.resolve(gatewayIP);
} catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}
}
}).start();
Expand All @@ -243,7 +260,7 @@ public void run() {
final float frequency = info.getFrequency();
promise.resolve(frequency);
} catch (Exception e) {
promise.resolve(null);
promise.reject(e);
}
}
}).start();
Expand All @@ -270,7 +287,7 @@ private String intToIP(int ip) {

private Boolean inDSLITERange(String ip) {
// Fixes issue https://github.com/pusherman/react-native-network-info/issues/43
// Based on comment
// Based on comment
// https://github.com/pusherman/react-native-network-info/issues/43#issuecomment-358360692
// added this check in getIPAddress and getIPV4Address
return RNNetworkInfo.DSLITE_LIST.contains(ip);
Expand Down
Loading