From eec529c2682c0b06a30459540896a94387cf9296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=8C=E7=A8=8B=E7=BD=91=E7=BB=9C-=E9=9F=A9=E5=8A=A0?= =?UTF-8?q?=E5=8D=8E22343?= Date: Mon, 7 May 2018 11:28:24 +0800 Subject: [PATCH 1/6] add getMacaddress --- NetworkInfo.js | 4 +++ .../pusherman/networkinfo/RNNetworkInfo.java | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/NetworkInfo.js b/NetworkInfo.js index b6cbc8b..6f14b41 100644 --- a/NetworkInfo.js +++ b/NetworkInfo.js @@ -22,6 +22,10 @@ const NetworkInfo = { getIPV4Address(ip) { RNNetworkInfo.getIPV4Address(ip); + }, + + getIPV4MacAddress(mac) { + RNNetworkInfo.getIPV4MacAddress(mac); } } diff --git a/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java b/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java index e350057..a78be1a 100644 --- a/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java +++ b/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java @@ -93,6 +93,39 @@ public void getIPV4Address(final Callback callback) { callback.invoke(ipAddress); } + @ReactMethod + public void getIPV4MacAddress(final Callback callback) { + String mac = null; + + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { + StringBuffer stringBuffer = new StringBuffer(); + NetworkInterface networkInterface = en.nextElement(); + if (networkInterface != null) { + byte[] bytes = networkInterface.getHardwareAddress(); + if (bytes != null) { + for (int i = 0; i < bytes.length; i++) { + if (i != 0) { + stringBuffer.append("-"); + } + int tmp = bytes[i] & 0xff; + String str = Integer.toHexString(tmp); + if (str.length() == 1) { + stringBuffer.append("0" + str); + } else { + stringBuffer.append(str); + } + } + mac = stringBuffer.toString().toUpperCase(); + } + } + } + } catch (Exception ex) { + Log.e(TAG, ex.toString()); + } + + callback.invoke(mac); + } private List getInetAddresses() { List addresses = new ArrayList<>(); From 73ddfcd769fa4e22dd484afd745f5e2618a98292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=8C=E7=A8=8B=E7=BD=91=E7=BB=9C-=E9=9F=A9=E5=8A=A0?= =?UTF-8?q?=E5=8D=8E22343?= Date: Mon, 7 May 2018 14:17:55 +0800 Subject: [PATCH 2/6] readme --- README.md | 7 ++++++- package.json | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8bbfde6..3c63578 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # react-native-network-info - +I add some APIs to getIPV4MacAddress React Native library for getting information about the devices network ## Requirements @@ -52,6 +52,11 @@ NetworkInfo.getSSID(ssid => { NetworkInfo.getBSSID(ssid => { console.log(ssid); }); + +// GET Macaddress +NetworkInfo.getIPV4MacAddress(mac => { + console.log(mac); +}); ``` diff --git a/package.json b/package.json index dc744b4..86590a8 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { - "name": "react-native-network-info", - "version": "3.2.2", + "name": "react-native-networkinfo", + "version": "1.0.0", "description": "Get local network information", "main": "NetworkInfo.js", "repository": { "type": "git", - "url": "git@github.com:pusherman/react-native-network-info.git" + "url": "git@github.com:roy2651/react-native-network-info.git" }, "keywords": [ "react-component", @@ -17,6 +17,6 @@ "peerDependencies": { "react-native": ">=0.47" }, - "author": "Corey Wilson (https://github.com/powerfulninja)", + "author": "roy2651", "license": "MIT" } From 61793525c071e291dfe03db1e30efc37f3ac754e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=8C=E7=A8=8B=E7=BD=91=E7=BB=9C-=E9=9F=A9=E5=8A=A0?= =?UTF-8?q?=E5=8D=8E22343?= Date: Mon, 7 May 2018 15:00:15 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E6=B4=BB=E5=8A=A8=E7=BD=91=E5=8D=A1=E7=9A=84mac?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pusherman/networkinfo/RNNetworkInfo.java | 225 +++++++++--------- 1 file changed, 111 insertions(+), 114 deletions(-) diff --git a/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java b/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java index a78be1a..ac2a1f6 100644 --- a/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java +++ b/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java @@ -3,143 +3,140 @@ import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; -import android.support.annotation.NonNull; import android.util.Log; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; - -import java.net.Inet4Address; import java.net.InetAddress; -import java.net.InterfaceAddress; +import java.net.Inet4Address; + import java.net.NetworkInterface; -import java.util.ArrayList; import java.util.Enumeration; -import java.util.List; +import android.net.ConnectivityManager; -public class RNNetworkInfo extends ReactContextBaseJavaModule { - WifiManager wifi; - - public static final String TAG = "RNNetworkInfo"; - public RNNetworkInfo(ReactApplicationContext reactContext) { - super(reactContext); - - wifi = (WifiManager) reactContext.getApplicationContext() - .getSystemService(Context.WIFI_SERVICE); - } - - @Override - public String getName() { - return TAG; - } - - @ReactMethod - public void getSSID(final Callback callback) { - 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); - } - - callback.invoke(ssid); - } - - @ReactMethod - public void getBSSID(final Callback callback) { - callback.invoke(wifi.getConnectionInfo().getBSSID()); +public class RNNetworkInfo extends ReactContextBaseJavaModule { + WifiManager wifi; + WifiInfo wifiInfo; + ConnectivityManager conMan; + + public static final String TAG = "RNNetworkInfo"; + + public RNNetworkInfo(ReactApplicationContext reactContext) { + super(reactContext); + + wifi = (WifiManager)reactContext.getApplicationContext() + .getSystemService(Context.WIFI_SERVICE); + + conMan = (ConnectivityManager)reactContext.getApplicationContext() + .getSystemService(Context.CONNECTIVITY_SERVICE); + } + + @Override + public String getName() { + return TAG; + } + + @ReactMethod + public void getSSID(final Callback callback) { + 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); } - @ReactMethod - public void getBroadcast(/*@NonNull String ip, */final Callback callback) { - String ipAddress = null; - - for (InterfaceAddress address : getInetAddresses()) { - if (!address.getAddress().isLoopbackAddress()/*address.getAddress().toString().equalsIgnoreCase(ip)*/) { - ipAddress = address.getBroadcast().toString(); - } + callback.invoke(ssid); + } + + @ReactMethod + public void getBSSID(final Callback callback) { + callback.invoke(wifi.getConnectionInfo().getBSSID()); + } + + @ReactMethod + public void getIPAddress(final Callback callback) { + String ipAddress = null; + + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { + NetworkInterface intf = en.nextElement(); + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress()) { + ipAddress = inetAddress.getHostAddress(); + } } - - callback.invoke(ipAddress); + } + } catch (Exception ex) { + Log.e(TAG, ex.toString()); } - @ReactMethod - public void getIPAddress(final Callback callback) { - String ipAddress = null; - - for (InterfaceAddress address : getInetAddresses()) { - if (!address.getAddress().isLoopbackAddress()) { - ipAddress = address.getAddress().getHostAddress().toString(); - } + callback.invoke(ipAddress); + } + + @ReactMethod + public void getIPV4Address(final Callback callback) { + String ipAddress = null; + + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { + NetworkInterface intf = en.nextElement(); + for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { + InetAddress inetAddress = enumIpAddr.nextElement(); + if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { + ipAddress = inetAddress.getHostAddress().toString(); + } } - - callback.invoke(ipAddress); + } + } catch (Exception ex) { + Log.e(TAG, ex.toString()); } - @ReactMethod - public void getIPV4Address(final Callback callback) { - String ipAddress = null; - - for (InterfaceAddress address : getInetAddresses()) { - if (!address.getAddress().isLoopbackAddress() && address.getAddress() instanceof Inet4Address) { - ipAddress = address.getAddress().getHostAddress().toString(); - } - } - - callback.invoke(ipAddress); - } + callback.invoke(ipAddress); + } - @ReactMethod - public void getIPV4MacAddress(final Callback callback) { - String mac = null; + @ReactMethod + public void getIPV4MacAddress(final Callback callback) { + String mac = null; - try { + try { + boolean isWifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected(); + if(isWifi == true) { + mac = wifi.getConnectionInfo().getMacAddress(); + } else { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { - StringBuffer stringBuffer = new StringBuffer(); - NetworkInterface networkInterface = en.nextElement(); - if (networkInterface != null) { - byte[] bytes = networkInterface.getHardwareAddress(); - if (bytes != null) { - for (int i = 0; i < bytes.length; i++) { - if (i != 0) { - stringBuffer.append("-"); - } - int tmp = bytes[i] & 0xff; - String str = Integer.toHexString(tmp); - if (str.length() == 1) { - stringBuffer.append("0" + str); - } else { - stringBuffer.append(str); - } - } - mac = stringBuffer.toString().toUpperCase(); - } - } - } - } catch (Exception ex) { - Log.e(TAG, ex.toString()); + StringBuffer stringBuffer = new StringBuffer(); + NetworkInterface networkInterface = en.nextElement(); + if (networkInterface != null) { + byte[] bytes = networkInterface.getHardwareAddress(); + if (bytes != null) { + for (int i = 0; i < bytes.length; i++) { + if (i != 0) { + stringBuffer.append("-"); + } + int tmp = bytes[i] & 0xff; + String str = Integer.toHexString(tmp); + if (str.length() == 1) { + stringBuffer.append("0" + str); + } else { + stringBuffer.append(str); + } + } + mac = stringBuffer.toString().toUpperCase(); + } + } } - - callback.invoke(mac); + } + + } catch (Exception ex) { + Log.e(TAG, ex.toString()); } - private List getInetAddresses() { - List addresses = new ArrayList<>(); - try { - for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { - NetworkInterface intf = en.nextElement(); - - for (InterfaceAddress interface_address : intf.getInterfaceAddresses()) { - addresses.add(interface_address); - } - } - } catch (Exception ex) { - Log.e(TAG, ex.toString()); - } - return addresses; - } + callback.invoke(mac); + } + } From 3d25fff459e7c311218f0a94a36aff86502d7488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=8C=E7=A8=8B=E7=BD=91=E7=BB=9C-=E9=9F=A9=E5=8A=A0?= =?UTF-8?q?=E5=8D=8E22343?= Date: Mon, 7 May 2018 15:03:28 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3c63578..1af5af3 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,5 @@ -# react-native-network-info -I add some APIs to getIPV4MacAddress -React Native library for getting information about the devices network - -## Requirements - -Version 3+ requires RN 0.47 or higher -Version 2+ requires RN 0.40 - RN 0.46 +## info +这个包我加了一个api获取当前活动网卡的mac地址 ## Installation From c9341677176765bff653d8e889b4baf53afcefc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=8C=E7=A8=8B=E7=BD=91=E7=BB=9C-=E9=9F=A9=E5=8A=A0?= =?UTF-8?q?=E5=8D=8E22343?= Date: Mon, 7 May 2018 15:30:29 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=8D=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1af5af3..2e16f1b 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ ## Installation ```javascript -npm install react-native-network-info --save +npm install react-native-networkinfobyroy2651 --save ``` or ```javascript -yarn add react-native-network-info +yarn add react-native-networkinfobyroy2651 ``` ### Linking the library diff --git a/package.json b/package.json index 86590a8..a277f14 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "react-native-networkinfo", + "name": "react-native-networkinfobyroy2651", "version": "1.0.0", "description": "Get local network information", "main": "NetworkInfo.js", From c9cb6dd96b164f19ee962e606a9f2eef83cb2d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=8C=E7=A8=8B=E7=BD=91=E7=BB=9C-=E9=9F=A9=E5=8A=A0?= =?UTF-8?q?=E5=8D=8E22343?= Date: Mon, 7 May 2018 15:37:16 +0800 Subject: [PATCH 6/6] update version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update macaddress format (xx:xx....) --- .../src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java b/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java index ac2a1f6..4eb7fdd 100644 --- a/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java +++ b/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java @@ -116,7 +116,7 @@ public void getIPV4MacAddress(final Callback callback) { if (bytes != null) { for (int i = 0; i < bytes.length; i++) { if (i != 0) { - stringBuffer.append("-"); + stringBuffer.append(":"); } int tmp = bytes[i] & 0xff; String str = Integer.toHexString(tmp); diff --git a/package.json b/package.json index a277f14..d6fcba9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-networkinfobyroy2651", - "version": "1.0.0", + "version": "1.0.1", "description": "Get local network information", "main": "NetworkInfo.js", "repository": {