Get mac address and ip in Nativescript

可紊 提交于 2019-12-07 01:04:34
Habib Kazemi

In nativescript you can access native apis of device so if there isn't any module/plugin for it you can use this option for accessing native apis. https://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript

for example there is solution for mac adress here in JAVA:

WifiManager wifiManager = (WifiManager)  getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();

we can write it in javascript like this:

first we should fine where is this getSystemService:

after searching in documentation of android we found:

getSystemService is in android.content.Context for accessing context in nativescript http://docs.nativescript.org/cookbook/application

we can do:

import app = require("application");
app.android.context;

so let's write it in javascript:

we don't have types in javascript so we use var instead;

  var context = android.content.Context;
  var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
  var wInfo = wifiManager.getConnectionInfo();
  var mac = wInfo.getMacAddress();

NOTE1 : as mentioned in above java solution link you should add this permision <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> to app/App_Resources/AndroidManifest

NOTE2 : the above solution was for android,for ios you should find the solution with objective_c and convert to javascript with help of nativescript documentation.

NOTE3:In android 6 you might need request permision

You can also use this method to create a plugin for nativescript.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!