-1

I want to get IP address from my mobile Android.

var context = application.android.context;
var wifiMgr = context.getSystemService("wifi");
var wifiInfo = wifiMgr.getConnectionInfo();
var ip = wifiInfo.getIpAddress();
console.log('ip', ip)

The result is: JS: ip -2029999936

But in fact this is not my IP.

Can you ask me any idea?

Update:

I follow this . I have this code:

Step1. In my component add this code:

import app = require("application");
app.android.context;
      constructor() {
      var context = android.content.Context;
      var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
      var wInfo = wifiManager.getConnectionInfo();
      var mac = wInfo.getMacAddress();
        }

Step2. In AndroidManifest.xml add

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

Error: [ts] Cannot find name 'android'. [2304] in this line: var context = android.content.Context; error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.

a.b
  • 15
  • 1
  • 7
web site
  • 99
  • 8
  • Please check this q&a [here](https://stackoverflow.com/questions/40852900/get-mac-address-and-ip-in-nativescript) – Ved_Code_it Feb 04 '19 at 13:22
  • 1
    As this question isn't about Angular but NativeScript, I am removing the Angular tag. Good luck with your question ! –  Feb 04 '19 at 13:22
  • Might your IP be: 248.255.87.64? – jwh20 Feb 04 '19 at 13:37
  • @Ved_Code_it I update post. Can you see again please? – web site Feb 04 '19 at 14:12
  • Possible duplicate of [Get mac address and ip in Nativescript](https://stackoverflow.com/questions/40852900/get-mac-address-and-ip-in-nativescript) – Gourav Feb 04 '19 at 14:17
  • @Gourav read the question in first. I have another error, and I follow this `Possible duplicate of Get mac address and ip in Nativescript that you say` – web site Feb 04 '19 at 14:25
  • `Cannot find name 'android` will be resolved with tns-platform-declarations https://www.npmjs.com/package/tns-platform-declarations – Nick Iliev Feb 04 '19 at 15:38

1 Answers1

1

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

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

Then all you have to is,

import * as application from 'tns-core-modules/application';
declare var 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.

Manoj
  • 21,753
  • 3
  • 20
  • 41