Why I got null Value instead of the string value always when calling a method from android native modules

Viewed 137

I m trying to use native modules on react native to get the device name and package installer from android , but I got always a null value ?

my code like the following

the result always is null !

// DeviceInfoModule.java

package com.example;


import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactMethod;

import android.annotation.SuppressLint;
import android.os.Build;
import android.content.Context;
import android.os.AsyncTask;

public class DeviceInfoModule extends ReactContextBaseJavaModule {

  public DeviceInfoModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "DeviceInfoModule";
  }

  @ReactMethod
  public void getDeviceName(final Callback callback) {
    getDeviceNameHandler(callback);
  }

  @ReactMethod
  public void getDeviceInstaller(final Callback callback) {
    getDevicePackager(callback);
  }

  private void getDeviceNameHandler(final Callback callback) {
   @SuppressLint("StaticFieldLeak") AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
      @SuppressLint("StaticFieldLeak")
      @Override
      protected Void doInBackground(final Void ... params) {
        String device_Name = Build.MANUFACTURER + "-" + Build.MODEL;

        callback.invoke( device_Name, device_Name);
        return null;
      }
    };
    myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  }

  private void getDevicePackager(final Callback callback) {
     @SuppressLint("StaticFieldLeak") AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
      @Override
      protected Void doInBackground(final Void ... params) {
        Context context = getReactApplicationContext();
        String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());

        callback.invoke(null, installer);
        return null;
      }
    };
    myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  }

}


and packages are :

// ReactNativePackages.java

package com.example;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.carexs.DeviceInfoModule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReactNativePackages implements ReactPackage {

  @Override
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
  }

  @Override
  public List<NativeModule> createNativeModules(
    ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    modules.add(new DeviceInfoModule(reactContext));
    return modules;
  }
}

in the Main Application class I added the package

 protected List<ReactPackage> getPackages() {
      List<ReactPackage> packages = new PackageList(this).getPackages();
      packages.add(new CxsNativeUtilsPackage());
      packages.add(new ReactNativePackages());

      return packages;
    }

in my app I have the following code


 const DeviceInfo = NativeModules.DeviceInfoModule;
        DeviceInfo.getDeviceInstaller((err: string, name: string) => {
          if (err) {
            console.log('error', err);
          } else {
            console.log('device name', name);
          }
        });

any help? , thank you.

1 Answers

I found a solution and maybe someone will need it someday , I found that react native methods that I have created is not working maybe because of some depraceted modules or other issue on the code I m not sure exactly , I have changed those methods to new version , for example:

@ReactMethod
getDevicePackager() // (renamed to getDeviceInstaller()) method in DeviceInfoModule is changed to the following

// will be

 @ReactMethod(isBlockingSynchronousMethod = true)
  public String getInstallerPackageNameSync() {
    String packageName = getReactApplicationContext().getPackageName();
    String installerPackageName = getReactApplicationContext().getPackageManager().getInstallerPackageName(packageName);

    if (installerPackageName == null) {
      return "unknown";
    }

    return installerPackageName;
  }


 @ReactMethod
  public void getDeviceInstaller(Promise p) { p.resolve(getInstallerPackageNameSync()); }

Related