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());
}
}
import javanetInetAddress;
import javanetNetworkInterface;
import javanetSocketException;
import javanetUnknownHostException;
/
物理地址是48位,别和ipv6搞错了
/
public class LOCALMAC {
/
@param args
@throws UnknownHostException
@throws SocketException
/
public static void main(String[] args) throws UnknownHostException, SocketException {
// TODO Auto-generated method stub
//得到IP,输出PC-201309011313/1222067383
InetAddress ia = InetAddressgetLocalHost();
Systemoutprintln(ia);
getLocalMac(ia);
}
private static void getLocalMac(InetAddress ia) throws SocketException {
// TODO Auto-generated method stub
//获取网卡,获取地址
byte[] mac = NetworkInterfacegetByInetAddress(ia)getHardwareAddress();
Systemoutprintln("mac数组长度:"+maclength);
StringBuffer sb = new StringBuffer("");
for(int i=0; i<maclength; i++) {
if(i!=0) {
sbappend("-");
}
//字节转换为整数
int temp = mac[i]&0xff;
String str = IntegertoHexString(temp);
Systemoutprintln("每8位:"+str);
if(strlength()==1) {
sbappend("0"+str);
}else {
sbappend(str);
}
}
Systemoutprintln("本机MAC地址:"+sbtoString()toUpperCase());
}
}
通过设备开通WiFi连接获取Mac地址是最可取的,代码如下:
/
设备开通WiFi连接,通过wifiManager获取Mac地址
/
public static String getMacFromWifi(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) contextgetSystemService(ContextCONNECTIVITY_SERVICE);
State wifiState = connectivityManagergetNetworkInfo(ConnectivityManagerTYPE_WIFI)getState();
if(wifiState == NetworkInfoStateCONNECTED){//判断当前是否使用wifi连接
WifiManager wifiManager = (WifiManager) contextgetSystemService(ContextWIFI_SERVICE);
if (!wifiManagerisWifiEnabled()) { //如果当前wifi不可用
wifiManagersetWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManagergetConnectionInfo();
return wifiInfogetMacAddress();
}
return null;
}
import javanetInetAddress;
import javanetNetworkInterface;
public class TestOne {
public static void main(String[] arguments) throws Exception {
InetAddress ia = InetAddressgetLocalHost();// 获取本地IP对象
Systemoutprintln("MAC " + getMACAddress(ia));
}
// 获取MAC地址的方法
private static String getMACAddress(InetAddress ia) throws Exception {
// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[] mac = NetworkInterfacegetByInetAddress(ia)getHardwareAddress();
// 下面代码是把mac地址拼装成String
StringBuffer sb = new StringBuffer();
for (int i = 0; i < maclength; i++) {
if (i != 0) {
sbappend("-");
}
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = IntegertoHexString(mac[i] & 0xFF);
sbappend(slength() == 1 0 + s : s);
}
// 把字符串所有小写字母改为大写成为正规的mac地址并返回
return sbtoString()toUpperCase();
}
}
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;
}
通过设备开通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) contextgetSystemService(ContextCONNECTIVITY_SERVICE);
State wifiState = connectivityManagergetNetworkInfo(ConnectivityManagerTYPE_WIFI)getState();
if(wifiState == NetworkInfoStateCONNECTED){//判断当前是否使用wifi连接
WifiManager wifiManager = (WifiManager) contextgetSystemService(ContextWIFI_SERVICE);
if (!wifiManagerisWifiEnabled()) { //如果当前wifi不可用
wifiManagersetWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManagergetConnectionInfo();
return wifiInfogetMacAddress();
}
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 = RuntimegetRuntime()exec(busybox ifconfig);
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(processgetInputStream()));
while ((readLine = bufferedReaderreadLine ()) != null) {//执行命令cmd,只取结果中含有HWaddr的这一行
if(readLinecontains(HWaddr)){
return readLinesubstring(readLineindexOf(HWaddr)+6, readLinelength()-1);
}
}
}catch(Exception e) { //如果因设备不支持busybox工具而发生异常。
eprintStackTrace();
}
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 = bufferedReaderreadLine())!=null){
if(rollIndex == 1){
break;
}
rollIndex = rollIndex + 1;
}
} catch (IOException e) {
eprintStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReaderclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
if(readLine !=null && readLinelength()>1){
String[] subReadLineArray = readLinesplit( );
int rollIndex = 1;
for(int i = 0; i < subReadLineArraylength; ++i){
if(!TextUtilsisEmpty(subReadLineArray[i])){
if(rollIndex == 4){
return subReadLineArray[i];
}
rollIndex = rollIndex + 1;
}
}
}
return null;
}
注意:无论在Android Pad中还是在Android手机中,这种方法都无法准确获取到Mac地址。
这个网上很多,主要是机器必须支持ICMP和NETBIOS协议。你参考一下:
public String getIP()
{
InetAddress inet;
try {
inet =
InetAddressgetLocalHost();
InetAddressgetByName("");
return
inetgetHostAddress();
} catch (UnknownHostException e) {
// TODO
Auto-generated catch block
eprintStackTrace();
}
return "";
}
以上就是关于求好人帮助,如何用java语言获取像无线路由器上的MAC地址,我会重赏全部的内容,包括:求好人帮助,如何用java语言获取像无线路由器上的MAC地址,我会重赏、Java web 怎么得到客户端的Mac地址、如何使用Java代码获取Android移动终端Mac地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)