From f0759c3f5559d1d9d7345ff2bbfb170159ec99c6 Mon Sep 17 00:00:00 2001 From: Mehdi Achour Date: Mon, 5 Mar 2018 21:58:45 +0100 Subject: [PATCH] fix(android): fix Android > 6 compatibility for getMacAddress() --- .../learnium/RNDeviceInfo/RNDeviceModule.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java index 84c528bb9..2676a4f1c 100644 --- a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java +++ b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java @@ -26,11 +26,14 @@ import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Promise; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; import java.util.TimeZone; import java.lang.Runtime; +import java.net.NetworkInterface; import javax.annotation.Nullable; @@ -130,7 +133,38 @@ public void getIpAddress(Promise p) { @ReactMethod public void getMacAddress(Promise p) { String macAddress = getWifiInfo().getMacAddress(); - p.resolve(macAddress); + + String permission = "android.permission.INTERNET"; + int res = this.reactContext.checkCallingOrSelfPermission(permission); + + if (res == PackageManager.PERMISSION_GRANTED) { + try { + List all = Collections.list(NetworkInterface.getNetworkInterfaces()); + for (NetworkInterface nif : all) { + if (!nif.getName().equalsIgnoreCase("wlan0")) continue; + + byte[] macBytes = nif.getHardwareAddress(); + if (macBytes == null) { + macAddress = ""; + } else { + + StringBuilder res1 = new StringBuilder(); + for (byte b : macBytes) { + res1.append(String.format("%02X:",b)); + } + + if (res1.length() > 0) { + res1.deleteCharAt(res1.length() - 1); + } + + macAddress = res1.toString(); + } + } + } catch (Exception ex) { + } + } + + p.resolve(macAddress); } @ReactMethod