標籤:android style blog http io ar color os 使用
【Android】擷取Mac地址【2】
之前寫了【Android】擷取Mac地址【1】有些不夠詳細,現在貼上一些其他代碼,僅供參考。
(1) 調用android 的API: NetworkInterface. getHardwareAddress ()
該API的level為9,只有android 2.3以上才有該介面
(2) 調用java 的方法: nbtstat/arp
一般android不支援這兩個命令。該方法沒有試過。
(3) 調用Android的API: WifiManager
許可權:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
代碼:
/** * 通過wifiManager擷取mac地址 * @attention Wifi * @return Mac Address */private static String getMacFromWifi(Context context){ WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String mResult = wifiInfo.getMacAddress(); Log.i(TAG_NETWORK,"Mac address(wifi): "+mResult); return mResult;}
這個是需要裝置開通Wifi串連,擷取到網卡的MAC地址
另,貼上,判斷當前是否為wifi串連方式:
//判斷當前是否使用wifi串連 private static boolean isWifiConnected(Context context){ ConnectivityManager cm; cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); boolean result = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ? true : false ; Log.i(TAG_NETWORK,"isWifiConnected: "+result); return result; }
(4) 調用Linux的busybox
/** * 通過callCmd("busybox ifconfig","HWaddr")擷取mac地址 * @attention 需要裝置裝有busybox工具 * @return Mac Address */private static String getMacFromCallCmd(){String result = ""; result = callCmd("busybox ifconfig","HWaddr"); if(result == null || result.length() <= 0){ Log.i(TAG_NETWORK,"callCmd returns null or empty"); return null; } //對該行資料進行解析 //例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67 if(result.length()>0 && result.contains("HWaddr")==true){ String Mac = result.substring(result.indexOf("HWaddr")+6, result.length()-1); Log.i(TAG_NETWORK,"Mac:"+Mac+" Mac.length: "+Mac.length()); if(Mac.length()>1){ Mac = Mac.replaceAll(" ", ""); result = ""; String[] tmp = Mac.split(":"); for(int i = 0;i<tmp.length;++i){ result +=tmp[i]; } } Log.i(TAG_NETWORK,result+" result.length: "+result.length()); } Log.i(TAG_NETWORK,"Mac address(CallCmd): "+result); return result;}
其他相關函數:
public static String callCmd(String cmd,String filter) { String result = ""; String line = ""; try { Process proc = Runtime.getRuntime().exec(cmd); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); //執行命令cmd,只取結果中含有filter的這一行 while ((line = br.readLine ()) != null && line.contains(filter)== false) { //result += line; Log.i("test","line: "+line); } result = line; Log.i("test","result: "+result); } catch(Exception e) { e.printStackTrace(); } return result; }
這個需要裝置支援busybox工具。現在發現一些裝置是沒有安裝該工具的,這時使用該方法,會報錯。
(5)查詢記錄了MAC地址的檔案“/proc/net/arp”
需要有這個檔案,並且記錄了相應的內容
/** * get the Mac Address from the file /proc/net/arp * @param context * @attention the file /proc/net/arp need exit * @return Mac Address */ private static String getMacFromFile(Context context){ String mIP = Config.getIpAddress(context); if(mIP == null || mIP.length()<=0) return null; List<String> mResult = readFileLines("/proc/net/arp"); Log.d(TAG_NETWORK,"======= /proc/net/arp ========="); for(int i =0;i<mResult.size();++i) Log.d("line",mResult.get(i)); Log.d(TAG_NETWORK,"==========================="); if(mResult !=null && mResult.size()>1){ for(int j =1;j<mResult.size();++j){ List<String> mList = new ArrayList<String>(); String[] mType = mResult.get(j).split(" "); for(int i =0;i<mType.length;++i){ if(mType[i]!=null && mType[i].length()>0) mList.add(mType[i]); } if(mList!=null && mList.size()>4 && mList.get(0).equalsIgnoreCase(mIP)){ String result=""; String[] tmp = mList.get(3).split(":"); for(int i = 0;i<tmp.length;++i){ result +=tmp[i]; } result = result.toUpperCase(); Log.i(TAG_NETWORK,"Mac address(file): "+result); return result; } } } return null; }
/** * 以行為單位讀取檔案,常用於讀面向行的格式檔案 */ private static List<String> readFileLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; String tempString =""; List<String> mResult = new ArrayList<String>(); try { Log.i("result","以行為單位讀取檔案內容,一次讀一整行:"); reader = new BufferedReader(new FileReader(file)); while((tempString = reader.readLine())!=null){ mResult.add(tempString); } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return mResult; }
記錄了MAC地址的檔案“/proc/net/arp”內容大致如下:
IP address HW type Flags HW address Mask Device
10.63.253.193 0x1 0x2 00:11:92:06:85:3f * eth0
10.63.253.194 0x1 0x2 00:11:92:06:85:3a * eth1
10.63.253.195 0x1 0x2 00:11:92:06:85:3b * eth2
Done!!睡覺嘍~
(轉)【Android】擷取Mac地址【2】