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

add getMacaddress #55

Open
wants to merge 6 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
4 changes: 4 additions & 0 deletions NetworkInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const NetworkInfo = {

getIPV4Address(ip) {
RNNetworkInfo.getIPV4Address(ip);
},

getIPV4MacAddress(mac) {
RNNetworkInfo.getIPV4MacAddress(mac);
}
}

Expand Down
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -52,6 +46,11 @@ NetworkInfo.getSSID(ssid => {
NetworkInfo.getBSSID(ssid => {
console.log(ssid);
});

// GET Macaddress
NetworkInfo.getIPV4MacAddress(mac => {
console.log(mac);
});
```


Expand Down
196 changes: 113 additions & 83 deletions android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> 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<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> 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<NetworkInterface> 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<InterfaceAddress> getInetAddresses() {
List<InterfaceAddress> addresses = new ArrayList<>();
try {
for (Enumeration<NetworkInterface> 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;
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]:pusherman/react-native-network-info.git"
"url": "[email protected]:roy2651/react-native-network-info.git"
},
"keywords": [
"react-component",
Expand All @@ -17,6 +17,6 @@
"peerDependencies": {
"react-native": ">=0.47"
},
"author": "Corey Wilson <[email protected]> (https://github.com/powerfulninja)",
"author": "roy2651",
"license": "MIT"
}