2

We are implementing a server for app distribution and we need restrict the access to the apps by:

  • mac address
  • ip

At the moment I have not found any module that can obtain this data from the device in nativescript, so i don't know if there's a plugin or how else can I achieve this.

B1-66er
  • 23
  • 4

1 Answers1

2

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.

Community
  • 1
  • 1
Habib Kazemi
  • 2,172
  • 1
  • 24
  • 30
  • Note that I also needed `android.permission.CHANGE_WIFI_STATE` in my manifest before this worked. I was getting `java.lang.SecurityException: WifiService: Neither user 10909 nor current process has android.permission.ACCESS_WIFI_STATE.` without it. – Pistos Jul 08 '19 at 22:40