android判断以太网还是wifi,Android获取Ethernet、WIFI的ip和mac地址

/**

* 获取本地ip

* @return

*/

private String getLocalIpAddress() {

try {

String ipv4 = null;

List nilist = Collections.list(NetworkInterface.getNetworkInterfaces());

for (NetworkInterface ni : nilist) {

List ialist = Collections.list(ni.getInetAddresses());

for (InetAddress address : ialist) {

ipv4 = address.getHostAddress();

if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4)) {

return ipv4;

}

}

}

} catch (SocketException ex) {

}

return "0.0.0.0";

}

通过IP获取MAC地址/**

* 通过本地ip获取mac地址

* @return

*/

@SuppressWarnings("finally")

private String getLocalMacAddressFromIp() {

String mac_s = "";

try {

byte[] mac;

String ip = getLocalIpAddress();

if (!InetAddressUtils.isIPv4Address(ip)) {

return mac_s;

}

InetAddress ipAddress = InetAddress.getByName(ip);

if (ipAddress == null) {

return mac_s;

}

NetworkInterface ne = NetworkInterface.getByInetAddress(ipAddress);

mac = ne.getHardwareAddress();

if (mac.length > 0) {

mac_s = byte2mac(mac);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

return mac_s;

}

}

private String byte2mac(byte[] b) {

StringBuffer hs = new StringBuffer(b.length);

String stmp = "";

int len = b.length;

for (int n = 0; n 

stmp = Integer.toHexString(b[n] & 0xFF);

if (stmp.length() == 1) {

hs = hs.append("0").append(stmp);

} else {

hs = hs.append(stmp);

}

}

StringBuffer str = new StringBuffer(hs);

for (int i = 0; i 

if (i % 3 == 0) {

str.insert(i, ':');

}

}

return str.toString().substring(1);

}

因为是通过ip获取的mac地址,所以当是wifi连接时的ip获取到的则是WIFI的mac,如果是Ethernet连接时则获取的是Ethernet的mac地址

下面的方法则是直接获取Ethernet的mac/**

* 获取Ethernet的MAC地址

* @return

*/

private String getMacAddress() {

try {

return loadFileAsString("/sys/class/net/eth0/address").toUpperCase(Locale.ENGLISH).substring(0, 17);

} catch (IOException e) {

return null;

}

}

private String loadFileAsString(String filePath) throws java.io.IOException{

StringBuffer fileData = new StringBuffer(1000);

BufferedReader reader = new BufferedReader(new FileReader(filePath));

char[] buf = new char[1024]; int numRead=0;

while((numRead=reader.read(buf)) != -1){

String readData = String.valueOf(buf, 0, numRead);

fileData.append(readData);

}

reader.close();

return fileData.toString();

}

还有一种更简单的方式获取Ethernet的mac/**

* 获取Ethernet的MAC地址

* @return

*/

private String getMacAddress() {

EthernetManager ethManager = (EthernetManager) MainActivity.this.getSystemService("ethernet");

return ethManager.getMacAddr()==null?"":ethManager.getMacAddr();

}

获取wifi的mac地址/**

* 获取wifi mac

* @return

*/

private String getWifiMac(){

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

WifiInfo info = wifi.getConnectionInfo();

return info.getMacAddress()==null?"":info.getMacAddress();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值