做為識別(IP為DHCP派發),網路上查詢,JAVA並沒有提供此種連線
方式取得目地方的MAC Address,所幸網路上有神人分享讀取arp table
的方式,實作上也可行。
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import android.util.Log; public class GetMACAddress{ public String getMacFromArpCache(String ip) { if (ip == null) return null; BufferedReader br = null; try { br = new BufferedReader(new FileReader("/proc/net/arp")); String line; while ((line = br.readLine()) != null) { String[] splitted = line.split(" +"); if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) { // Basic sanity check String mac = splitted[3]; Log.d("go", "mac " +mac +" ip "+ ip); if (mac.matches("..:..:..:..:..:..")) { return mac; } else { return null; } } } } catch (Exception e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
參考來源:
http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/
沒有留言:
張貼留言