如何使用Java代码获取Android移动终端Mac地址

如何使用Java代码获取Android移动终端Mac地址,第1张

这个是在android上获取MAC的方法

WifiManager wifi = (WifiManager) getSystemService(ContextWIFI_SERVICE);

WifiInfo info = wifigetConnectionInfo();

String mac = infogetMacAddress();

   Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:

import javanet;

public class NetInfo {

 public static void main(String[] args) {

    new NetInfo()say();

    }

 public void say() {

   try {

   InetAddress i = InetAddressgetLocalHost();

   Systemoutprintln(i);                  //计算机名称和IP

   Systemoutprintln(igetHostName());    //名称

   Systemoutprintln(igetHostAddress()); //只获得IP

   }

   catch(Exception e){eprintStackTrace();}

 }

}

    也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。

import javanet;

public class NetInfo {

public static void main(String[] args) {

new NetInfo()say();

}

public void say() {

try {

InetAddress i = InetAddressgetLocalHost();

Systemoutprintln(i); //计算机名称和ip

Systemoutprintln(igetHostName()); //名称

Systemoutprintln(igetHostAddress()); //只获得ip

}

catch(Exception e){eprintStackTrace();}

}

}

package tmp;

import javanetNetworkInterface;

import javautilEnumeration;

public class MacUtil {

public static String getMacAddress() throws Exception {

String s="";

Enumeration<NetworkInterface> ni = NetworkInterface

getNetworkInterfaces();

while (nihasMoreElements()) {

NetworkInterface netI = ninextElement();

byte[] bytes = netIgetHardwareAddress();

if (netI != null && bytes != null && byteslength == 6) {

StringBuffer sb = new StringBuffer();

for (byte b : bytes) {

// 与11110000作按位与运算以便读取当前字节高4位

sbappend(IntegertoHexString((b & 240) >> 4));

// 与00001111作按位与运算以便读取当前字节低4位

sbappend(IntegertoHexString(b & 15));

sbappend("-");

}

sbdeleteCharAt(sblength() - 1);

s+=sbtoString()toUpperCase()+"\n";

}

}

return s;

}

public static void main(String[] args) throws Exception {

Systemoutprintln(MacUtilgetMacAddress());

}

}

public static String getMacAddressIP(String remotePcIP) {

String str = "";

String macAddress = "";

try {

Process pp = RuntimegetRuntime()exec("nbtstat -A " + remotePcIP);

InputStreamReader ir = new InputStreamReader(ppgetInputStream());

LineNumberReader input = new LineNumberReader(ir);

for (int i = 1; i < 100; i++) {

str = inputreadLine();

if (str != null) {

if (strindexOf("MAC Address") > 1) {

macAddress = strsubstring(

strindexOf("MAC Address") + 14, strlength());

break;

}

}

}

} catch (IOException ex) {

}

return macAddress;

}

以上就是关于如何使用Java代码获取Android移动终端Mac地址全部的内容,包括:如何使用Java代码获取Android移动终端Mac地址、java如何查询本机ip地址和mac地址、java 获取本机Ip地址和对应的mac地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9337085.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存