「java手机mac」Java手机模拟器

博主:adminadmin 2022-11-23 04:58:08 62

本篇文章给大家谈谈java手机mac,以及Java手机模拟器对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA如何获取局域网内所有安卓设备的ip地址,MAC以及序列号?

1.得到局域网网段,可由自己机器的IP来确定 (也可以手动获取主机IP-CMD-ipconfig /all)

2.根据IP类型,一次遍历局域网内IP地址

JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用你自己编写相应代码调用便可

代码如下::

package bean;

import java.io.*;

import java.util.*;

public class Ip{

static public HashMap ping; //ping 后的结果集

public HashMap getPing(){ //用来得到ping后的结果集

return ping;

}

//当前线程的数量, 防止过多线程摧毁电脑

static int threadCount = 0;

public Ip() {

ping = new HashMap();

}

public void Ping(String ip) throws Exception{

//最多30个线程

while(threadCount30)

Thread.sleep(50);

threadCount +=1;

PingIp p = new PingIp(ip);

p.start();

}

public void PingAll() throws Exception{

//首先得到本机的IP,得到网段

InetAddress host = InetAddress.getLocalHost();

String hostAddress = host.getHostAddress();

int k=0;

k=hostAddress.lastIndexOf(".");

String ss = hostAddress.substring(0,k+1);

for(int i=1;i =255;i++){ //对所有局域网Ip

String iip=ss+i;

Ping(iip);

}

//等着所有Ping结束

while(threadCount0)

Thread.sleep(50);

}

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

Ip ip= new Ip();

ip.PingAll();

java.util.Set entries = ping.entrySet();

Iterator iter=entries.iterator();

String k;

while(iter.hasNext()){

Map.Entry entry=(Map.Entry)iter.next();

String key=(String)entry.getKey();

String value=(String)entry.getValue();

if(value.equals("true"))

System.out.println(key+"--"+value);

}

}

class PingIp extends Thread{

public String ip; // IP

public PingIp(String ip){

this.ip=ip;

}

public void run(){

try{

Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");

InputStreamReader ir = new InputStreamReader(p.getInputStream());

LineNumberReader input = new LineNumberReader (ir);

//读取结果行

for (int i=1 ; i 7; i++)

input.readLine();

String line= input.readLine();

if (line.length() 17 || line.substring(8,17).equals("timed out"))

ping.put(ip,"false");

else

ping.put(ip,"true");

//线程结束

threadCount -= 1;

}catch (IOException e){}

}

}

}

java如何对路由器实时获取连接到的手机的mac地址

package tmp;

import java.net.NetworkInterface;

import java.util.Enumeration;

public class MacUtil {

public static String getMacAddress() throws Exception {

String s="";

EnumerationNetworkInterface ni = NetworkInterface

.getNetworkInterfaces();

while (ni.hasMoreElements()) {

NetworkInterface netI = ni.nextElement();

byte[] bytes = netI.getHardwareAddress();

if (netI != null bytes != null bytes.length == 6) {

StringBuffer sb = new StringBuffer();

for (byte b : bytes) {

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

sb.append(Integer.toHexString((b 240) 4));

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

sb.append(Integer.toHexString(b 15));

sb.append("-");

}

sb.deleteCharAt(sb.length() - 1);

s+=sb.toString().toUpperCase()+"\n";

}

}

return s;

}

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

System.out.println(MacUtil.getMacAddress());

}

}

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

通过设备开通WiFi连接获取Mac地址是最可取的,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* 设备开通WiFi连接,通过wifiManager获取Mac地址

*

* @author 高焕杰

*/

public static String getMacFromWifi(Context context){

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

State wifiState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

if(wifiState == NetworkInfo.State.CONNECTED){//判断当前是否使用wifi连接

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

if (!wifiManager.isWifiEnabled()) { //如果当前wifi不可用

wifiManager.setWifiEnabled(true);

}

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

return wifiInfo.getMacAddress();

}

return null;

}

除了上面这种方法,网上还给出了另外两种方法:

1、通过调用Linux的busybox命令获取Mac地址:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

* 通过调用Linux的busybox命令获取Mac地址

*

* @author 高焕杰

*/

private static String getMacFromCallCmd(){

try {

String readLine = ;

Process process = Runtime.getRuntime().exec(busybox ifconfig);

BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(process.getInputStream()));

while ((readLine = bufferedReader.readLine ()) != null) {//执行命令cmd,只取结果中含有HWaddr的这一行

if(readLine.contains(HWaddr)){

return readLine.substring(readLine.indexOf(HWaddr)+6, readLine.length()-1);

}

}

}catch(Exception e) { //如果因设备不支持busybox工具而发生异常。

e.printStackTrace();

}

return null;

}

注意:这种方法在Android Pad中可以准确获取到的Mac地址,但是在Android手机中无法准确获取到。

2、通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

/**

* 通过查询记录了MAC地址的文件(文件路径:“/proc/net/arp”)获取Mac地址

*

* @author 高焕杰

*/

private static String getMacFromFile(Context context){

String readLine =;

BufferedReader bufferedReader = null;

try {

bufferedReader = new BufferedReader(new FileReader(new File(/proc/net/arp)));

int rollIndex = 0;

while((readLine = bufferedReader.readLine())!=null){

if(rollIndex == 1){

break;

}

rollIndex = rollIndex + 1;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (bufferedReader != null) {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

if(readLine !=null readLine.length()1){

String[] subReadLineArray = readLine.split( );

int rollIndex = 1;

for(int i = 0; i subReadLineArray.length; ++i){

if(!TextUtils.isEmpty(subReadLineArray[i])){

if(rollIndex == 4){

return subReadLineArray[i];

}

rollIndex = rollIndex + 1;

}

}

}

return null;

}

注意:无论在Android Pad中还是在Android手机中,这种方法都无法准确获取到Mac地址。

请问java如何获取手机mac地址?

以windows举例。

运行命令" cmd ipconfig /all"就会出现以下结果

Physical Address. . . . . . . . . : 20-CF-30-9A-60-EE

java就能过这样的命令来获取。以下是示例。

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class TestMac

{

public static void main(String[] args) {

System.out.println("Operation System=" + getOsName());

System.out.println("Mac Address=" + getMACAddress());

System.out.println("通过ip获取mac"+getMACAddress("192.168.1.101"));

}

public static String getOsName() {

String os = "";

os = System.getProperty("os.name");

return os;

}

public static String getMACAddress() {

String address = "";

String os = getOsName();

if (os.startsWith("Windows")) {

try {

String command = "cmd.exe /c ipconfig /all";

Process p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

if (line.indexOf("Physical Address") 0) {

int index = line.indexOf(":");

index += 2;

address = line.substring(index);

break;

}

}

br.close();

return address.trim();

} catch (IOException e) {

}

} else if (os.startsWith("Linux")) {

String command = "/bin/sh -c ifconfig -a";

Process p;

try {

p = Runtime.getRuntime().exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

if (line.indexOf("HWaddr") 0) {

int index = line.indexOf("HWaddr") + "HWaddr".length();

address = line.substring(index);

break;

}

}

br.close();

} catch (IOException e) {

}

}

address = address.trim();

return address;

}

public static String getMACAddress(String ipAddress) {

String str = "", strMAC = "", macAddress = "";

try {

Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);

InputStreamReader ir = new InputStreamReader(pp.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

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

str = input.readLine();

if (str != null) {

if (str.indexOf("MAC Address") 1) {

strMAC = str.substring(str.indexOf("MAC Address") + 14,

str.length());

break;

}

}

}

} catch (IOException ex) {

return "Can't Get MAC Address!";

}

//

if (strMAC.length() 17) {

return "Error!";

}

macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)

+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)

+ ":" + strMAC.substring(12, 14) + ":"

+ strMAC.substring(15, 17);

//

return macAddress;

}

}

剑天梦的回答原理和我这个一样,都是通过Process 执行命令。 我直接补充到答案里了。不过

我这边运行那个命令出来的结果很多,那么花的时间就长了。优点是能够获取别人的mac地址 。

java手机mac的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java手机模拟器、java手机mac的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。