如何在 Nativescript 中获取 mac 地址和 ip? [英] How to get mac address and ip in Nativescript?

查看:74
本文介绍了如何在 Nativescript 中获取 mac 地址和 ip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的移动 Android 获取 IP 地址.

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

结果为:JS: ip -2029999936

但实际上这不是我的IP.

你能问我任何想法吗?

更新:

我关注this.我有这个代码:

第一步.在我的组件中添加以下代码:

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

步骤 2.在 AndroidManifest.xml 添加

<块引用>

错误:[ts] 找不到名称android".[2304] 在这一行:var上下文 = android.content.Context;错误 TS1202:定位 ECMAScript 模块时无法使用导入分配.考虑使用 'import * as ns from "mod"'、'import {a} from "mod"'、'import d from "mod"' 或其他模块格式.

解决方案

您应该在 AndroidManifest.xml 中拥有 ACCESS_WIFI_STATE 权限以捕获 IP 地址.

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

那么你所要做的就是,

import * as application from 'tns-core-modules/application';声明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; 是为了避免访问原生api时出现TS错误.另一种方法是安装 tns-platform-declarations 插件并将声明文件指向 references.d.ts.

关于 Mac 地址,因为 Android6.0

<块引用>

为了向用户提供更好的数据保护,从本版本开始,Android 移除了使用 Wi-Fi 和蓝牙 API 的应用对设备本地硬件标识符的编程访问.WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法现在返回一个常量值 02:00:00:00:00:00.

所以它似乎没有得到官方支持.

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.

解决方案

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.

这篇关于如何在 Nativescript 中获取 mac 地址和 ip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆