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/README.md b/README.md index 8bbfde6..2e16f1b 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,15 @@ -# react-native-network-info - -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 ```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 @@ -52,6 +46,11 @@ NetworkInfo.getSSID(ssid => { NetworkInfo.getBSSID(ssid => { console.log(ssid); }); + +// GET Macaddress +NetworkInfo.getIPV4MacAddress(mac => { + console.log(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..4eb7fdd 100644 --- a/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java +++ b/android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java @@ -3,110 +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; - -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(); +import android.net.ConnectivityManager; - // 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); + } + + @ReactMethod + public void getIPV4MacAddress(final Callback callback) { + String mac = null; + + 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(); + } + } } - - callback.invoke(ipAddress); + } + + } catch (Exception ex) { + Log.e(TAG, ex.toString()); } + callback.invoke(mac); + } - 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; - } } diff --git a/package.json b/package.json index dc744b4..d6fcba9 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { - "name": "react-native-network-info", - "version": "3.2.2", + "name": "react-native-networkinfobyroy2651", + "version": "1.0.1", "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" }