native. js Acquisition Mobile Phone Hardware Basic Information Example Code android Version

  • 2021-10-15 11:39:16
  • OfStack

I would like to share with you some android public method native. js implementation codes, such as obtaining the basic information of mobile phone hardware such as MAC address, memory size, storage space size and CPU information of mobile phone

native. js Get the address of mobile phone MAC


/* Get a cell phone MAC Address */
function getMac() {
 var mac = "xxx-xxx-xxx-xxx";
 if (plus.os.name == "Android") {
  //WifiManager
  var Context = plus.android.importClass("android.content.Context");
  var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");
  var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);
  var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");
  var wifiInfo = wifiManager.getConnectionInfo();
  mac = wifiInfo.getMacAddress();
 }
 return mac;
}

native. js to get memory information of mobile phone


/* Get memory information of mobile phone */
function getMemorySize() {
 var memoryInfo = '';
 if (plus.os.name == "Android") {
  var Context = plus.android.importClass("android.content.Context");
  var ActivityManager = plus.android.importClass("android.app.ActivityManager");
 var mi = new ActivityManager.MemoryInfo();
  var activityService = plus.android.runtimeMainActivity().getSystemService(Context.ACTIVITY_SERVICE);
  activityService.getMemoryInfo(mi);
  memoryInfo = mi.plusGetAttribute("availMem");
 }
 return memoryInfo;
}

native. js Get the total storage space inside the mobile phone


/* Get the total storage space inside the mobile phone */
function getTotalInternalMemorySize() {
 var internalMemSize = 0;
 if (plus.os.name == "Android") {
  var environment = plus.android.importClass("android.os.Environment");
  var statFs = plus.android.importClass("android.os.StatFs");
  var files = plus.android.importClass("java.io.File");

  var Files = environment.getDataDirectory();
  var StatFs = new statFs(Files.getPath());
  var blockSize = parseFloat(StatFs.getBlockSize());
  var blockCount = parseFloat(StatFs.getBlockCount());
  internalMemSize = blockSize * blockCount;
 }
 return internalMemSize;
}

native. js Get the total memory of mobile phone


/* Get total memory */
function getTotalRamSize() {
 var memInfo = '/proc/meminfo';
 var temp = '',
  ramSize = '',
  arrays, initMemory;
 var fileReader = plus.android.importClass("java.io.FileReader");
 var bufferedReader = plus.android.importClass("java.io.BufferedReader");
 var FileReader = new fileReader(memInfo);
 var BufferedReader = new bufferedReader(FileReader, 8192);
 while ((temp = BufferedReader.readLine()) != null) {
  if (-1 != temp.indexOf('MemTotal:')) {
   var value = temp.replace(/[^0-9]/ig, "");
   ramSize = Math.floor(parseInt(value) / (1024));
  }
 }

 return ramSize;
}

native. js to get CPU information of mobile phone


/* Get a mobile phone CPU Information */
function getCpuInfo() {
 var cpuInfo = '/proc/cpuinfo';
 var temp = '',
  cpuHardware;
 var fileReader = plus.android.importClass("java.io.FileReader");
 var bufferedReader = plus.android.importClass("java.io.BufferedReader");
 var FileReader = new fileReader(cpuInfo);
 var BufferedReader = new bufferedReader(FileReader, 8192);
 while ((temp = BufferedReader.readLine()) != null) {
  if (-1 != temp.indexOf('Hardware')) {
   cpuHardware = temp.substr(parseInt(temp.indexOf(":")) + 1);
  }
 }
 return cpuHardware;
}

native. js Get CPU kernel number


/* Get CPU Audit */
function getCpuCount() {
 var Runtime = plus.android.importClass("java.lang.Runtime");
 var cpuCount = Runtime.getRuntime().availableProcessors();
 return cpuCount;
}

native. js Get ANDROID_ID for Android devices


var mainActivity = plus.android.runtimeMainActivity();
  var Settings= plus.android.importClass("android.provider.Settings");
  console.log(Settings.Secure.getString(mainActivity.getContentResolver(),Settings.Secure.ANDROID_ID));

Detailed introduction and explanation of API supported by Native. js attached to Dcloud

www.dcloud.io/docs/api/index.html

For more information about native. js to get basic information about mobile phone hardware example code content, please see the related links below


Related articles: