Skip to content Skip to sidebar Skip to footer

How To Get Mac Address And Ip In Nativescript?

I want to get IP address from my mobile Android. var context = application.android.context; var wifiMgr = context.getSystemService('wifi'); var wifiInfo = wifiMgr.getConnectionInfo

Solution 1:

You should have ACCESS_WIFI_STATE permission in AndroidManifest.xml for capturing IP address.

<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>

Then all you have to is,

import * as application from'tns-core-modules/application';
declarevar android;

const wifiManager = application.android.context.getSystemService(android.content.Context.WIFI_SERVICE);
const connectionInfo = wifiManager.getConnectionInfo();
const ip = android.text.format.Formatter.formatIpAddress(connectionInfo.getIpAddress());

declare var android; is to avoid TS errors while access native apis. An alternative is to install tns-platform-declarations plugin and point the declaration files in your references.d.ts.

Regarding the Mac Address, since Android 6.0

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

So it doesn't seem officially supported.

Post a Comment for "How To Get Mac Address And Ip In Nativescript?"