1. MAC アドレスを取得するいくつかの方法
最初の方法: sys/class/net/ パスの下にあるファイルを読み取る
FileInputStream fis_name = null;
FileInputStream fis = null;
try {
//interfaceName 可以直接填写 eth0
String path = "sys/class/net/"+interfaceName+"/address";
fis_name = new FileInputStream(path);
byte[] buffer_name = new byte[1024 * 8];
int byteCount_name = fis_name.read(buffer_name);
if (byteCount_name > 0) {
mac = new String(buffer_name, 0, byteCount_name, "utf-8");
}
if (mac.length() == 0) {
path = "sys/class/net/eth0/wlan0";
fis = new FileInputStream(path);
byte[] buffer = new byte[1024 * 8];
int byteCount = fis.read(buffer);
if (byteCount > 0) {
mac = new String(buffer, 0, byteCount, "utf-8");
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(fis_name != null){
try {
fis_name.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ただし、上記の方法でファイルの読み取り権限と書き込み権限を解放しないと、ファイルを読み取ることができません。
別の方法を紹介します。
方法 2: ConnectivityManager を使用する
1. 接続ネットワーク情報を取得する
ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext().getSystemService(Service.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
上記は現在のコネクションのネットワーク情報オブジェクトを取得するものですが、利用する場合はオブジェクトの空状態とコネクションの状態を判断する必要があります。
現在のネットワーク接続情報をすべて印刷できます
if(activeNetworkInfo != null && activeNetworkInfo.isConnected()){
Log.i("NetworkInfo info : " +cm.getLinkProperties(cm.getActiveNetwork()).toString() );
}
2. MAC アドレスを取得します (イーサネット接続の場合のみ、wifi は現在の wifi 名を取得します)。
String extraInfo = activeNetworkInfo.getExtraInfo();
3.IP情報を取得する
List<LinkAddress> linkAddresses = cm.getLinkProperties(cm.getActiveNetwork()).getLinkAddresses();
//获取当前连接的网络ip地址信息
if(linkAddresses != null && !linkAddresses.isEmpty()){
//注意:同时可以查看到两个网口的信息,但是ip地址不是固定的位置(即下标)
//所以遍历的时候需要判断一下当前获取的ip地址是否符合ip地址的正则表达式
for (int i = 0; i < linkAddresses.size(); i++) {
InetAddress address = linkAddresses.get(i).getAddress();
//判断ip地址的正则表达
if(isCorrectIp(address.getHostAddress())){
Log.i("ip地址"+address.getHostAddress())
}
}
}
4. ゲートウェイ アドレス情報を取得します。
List<RouteInfo> routes = cm.getLinkProperties(cm.getActiveNetwork()).getRoutes();
if(routes != null && !routes.isEmpty()){
for (int i = 0; i < routes.size(); i++) {
//和ip地址一样,需要判断获取的网址符不符合正则表达式
String hostAddress = routes.get(i).getGateway().getHostAddress();
if(isCorrectIp(hostAddress)){
Log.i("网关信息:" + hostAddress);
}
}
}
5. DNS 情報を取得します。
List<InetAddress> dnsServers = cm.getLinkProperties(cm.getActiveNetwork()).getDnsServers();
if(dnsServers != null && dnsServers.size() >= 2){
Log.i("dns1 " + dnsServers.get(0).toString());
Log.i("dns2 " + dnsServers.get(1).toString());
}
6: サブネットマスクアドレスを取得する
/**
* 获取子网掩码
* @param interfaceName
* @return
*/
public static String getIpAddressMaskForInterfaces(String interfaceName) {
//"eth0"
try {
//获取本机所有的网络接口
Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
//判断 Enumeration 对象中是否还有数据
while (networkInterfaceEnumeration.hasMoreElements()) {
//获取 Enumeration 对象中的下一个数据
NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
if (!networkInterface.isUp() && !interfaceName.equals(networkInterface.getDisplayName())) {
//判断网口是否在使用,判断是否时我们获取的网口
continue;
}
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
if (interfaceAddress.getAddress() instanceof Inet4Address) {
//仅仅处理ipv4
//获取掩码位数,通过 calcMaskByPrefixLength 转换为字符串
return calcMaskByPrefixLength(interfaceAddress.getNetworkPrefixLength());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return "0.0.0.0";
}
ネットワーク ポート名を取得して、このメソッドを呼び出します
getIpAddressMaskForInterfaces(cm.getLinkProperties(cm.getActiveNetwork()).getInterfaceName())
上記は、ネットワーク情報を取得する一般的な方法です。wifi とイーサネットの両方を使用できます。試すことができます。
花畑の夕風