diff --git a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java index eed7325bb..5fee2e294 100644 --- a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java +++ b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java @@ -40,10 +40,6 @@ public RNDeviceModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; - - - WifiManager manager = (WifiManager) reactContext.getSystemService(Context.WIFI_SERVICE); - this.wifiInfo = manager.getConnectionInfo(); } @Override @@ -51,6 +47,14 @@ public String getName() { return "RNDeviceInfo"; } + private WifiInfo getWifiInfo() { + if ( this.wifiInfo == null ) { + WifiManager manager = (WifiManager) reactContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + this.wifiInfo = manager.getConnectionInfo(); + } + return this.wifiInfo; + } + private String getCurrentLanguage() { Locale current = getReactApplicationContext().getResources().getConfiguration().locale; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -89,19 +93,19 @@ private Boolean isTablet() { @ReactMethod public void isPinOrFingerprintSet(Callback callback) { - KeyguardManager keyguardManager = (KeyguardManager) this.reactContext.getSystemService(Context.KEYGUARD_SERVICE); //api 16+ + KeyguardManager keyguardManager = (KeyguardManager) this.reactContext.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); //api 16+ callback.invoke(keyguardManager.isKeyguardSecure()); } @ReactMethod public void getIpAddress(Promise p) { - String ipAddress = Formatter.formatIpAddress(wifiInfo.getIpAddress()); + String ipAddress = Formatter.formatIpAddress(getWifiInfo().getIpAddress()); p.resolve(ipAddress); } @ReactMethod public void getMacAddress(Promise p) { - String macAddress = wifiInfo.getMacAddress(); + String macAddress = getWifiInfo().getMacAddress(); p.resolve(macAddress); } @@ -161,7 +165,7 @@ public void getMacAddress(Promise p) { (getCurrentActivity().checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED || getCurrentActivity().checkCallingOrSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED || getCurrentActivity().checkCallingOrSelfPermission("android.permission.READ_PHONE_NUMBERS") == PackageManager.PERMISSION_GRANTED)) { - TelephonyManager telMgr = (TelephonyManager) this.reactContext.getSystemService(Context.TELEPHONY_SERVICE); + TelephonyManager telMgr = (TelephonyManager) this.reactContext.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); constants.put("phoneNumber", telMgr.getLine1Number()); } return constants;