Platform: RK3288
OS: Android 7.1.2
Kernel: v4.4.143
需求:
最新经常有客户问在哪里可以查看以太网的MAC地址,由于Android原生系统更多应用于手机等使用WIFI环境的手持设备,没有以太网相关信息很正常,我们在状态信息下直接加一栏以太网的MAC地址信息;
方法:
参考WLANMAC地址,编写xml代码;
文件:package/app/Settings/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml
diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml
index 704b3bb..f50ad22 100755
--- a/res/values-zh-rCN/strings.xml
+++ b/res/values-zh-rCN/strings.xml
@@ -1059,6 +1059,7 @@<string name="status_roaming" msgid="2638800467430913403">"漫游"</string><string name="status_operator" msgid="2274875196954742087">"网络"</string><string name="status_wifi_mac_address" msgid="2202206684020765378">"WLANMAC 地址"</string>
+ <string name="status_eth_mac_address">"以太网MAC地址"</string><string name="status_bt_address" msgid="4195174192087439720">"蓝牙地址"</string><string name="status_serial_number" msgid="2257111183374628137">"序列号"</string><string name="status_unavailable" msgid="7862009036663793314">"不可用"</string>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 712fa4f..4e60f86 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2442,6 +2442,7 @@<string name="status_operator">Network</string><!-- About phone, status item title. The MAC address of the Wi-Fi network adapter. --><string name="status_wifi_mac_address">Wi\u2011Fi MAC address</string>
+ <string name="status_eth_mac_address">ETH MAC address</string><!-- About phone, status item title. The bluetooth adapter's hardware address--><string name="status_bt_address">Bluetooth address</string><!-- About phone, status item title. The hardware serial number. [CHAR LIMIT=30]-->
diff --git a/res/xml/device_info_status.xml b/res/xml/device_info_status.xml
old mode 100644
new mode 100755
index a0b7590..b2e85a6
--- a/res/xml/device_info_status.xml
+++ b/res/xml/device_info_status.xml
@@ -53,6 +53,12 @@android:title="@string/status_wifi_mac_address"android:summary="@string/device_info_not_available"android:persistent="false" />
+ <com.android.settings.CopyablePreference android:key="eth_mac_address"
+ android:enabled="false"
+ android:shouldDisableView="false"
+ android:title="@string/status_eth_mac_address"
+ android:summary="@string/device_info_not_available"
+ android:persistent="false" /><com.android.settings.CopyablePreference android:key="bt_address"android:enabled="false"android:shouldDisableView="false"
Android没有提供获取以太网MAC地址的API,但是通过adb可以获取以太网MAC地址;
ADB获取以太网MAC地址两个不同的命令:
cat /sys/class/net/eth0/address
busybox ifconfig eth0 | grep 'HWaddr' | busybox awk '{print $5}'
小知识:ADB获取WLAN MAC地址两个不同的命令:
cat /sys/class/net/wlan0/address
busybox ifconfig wlan0 | grep 'HWaddr' | busybox awk '{print $5}'
最后通过RootCmd工具类调用adb命令,附上patch;
diff --git a/src/com/android/settings/deviceinfo/Status.java b/src/com/android/settings/deviceinfo/Status.java
old mode 100644
new mode 100755
index 1ba3978..15387bb
--- a/src/com/android/settings/deviceinfo/Status.java
+++ b/src/com/android/settings/deviceinfo/Status.java
@@ -45,6 +45,10 @@ import com.android.settings.SettingsPreferenceFragment;import com.android.settings.Utils;import java.lang.ref.WeakReference;
+import java.io.IOException;
+import java.io.DataOutputStream;
+import java.io.DataInputStream;
+import android.util.Log;/*** Display the following information
@@ -55,7 +59,7 @@ import java.lang.ref.WeakReference;**/public class Status extends SettingsPreferenceFragment {
-
+ private static final String TAG = "Status";private static final String KEY_BATTERY_STATUS = "battery_status";private static final String KEY_BATTERY_LEVEL = "battery_level";private static final String KEY_IP_ADDRESS = "wifi_ip_address";
@@ -65,7 +69,7 @@ public class Status extends SettingsPreferenceFragment {private static final String KEY_WIMAX_MAC_ADDRESS = "wimax_mac_address";private static final String KEY_SIM_STATUS = "sim_status";private static final String KEY_IMEI_INFO = "imei_info";
-
+ private static final String KEY_ETH_MAC_ADDRESS = "eth_mac_address";// Broadcasts to listen to for connectivity changes.private static final String[] CONNECTIVITY_INTENTS = {BluetoothAdapter.ACTION_STATE_CHANGED,
@@ -93,7 +97,7 @@ public class Status extends SettingsPreferenceFragment {private Preference mIpAddress;private Preference mWifiMacAddress;private Preference mWimaxMacAddress;
-
+ private Preference mEthMacAddress;private Handler mHandler;private static class MyHandler extends Handler {
@@ -170,7 +174,7 @@ public class Status extends SettingsPreferenceFragment {mWifiMacAddress = findPreference(KEY_WIFI_MAC_ADDRESS);mWimaxMacAddress = findPreference(KEY_WIMAX_MAC_ADDRESS);mIpAddress = findPreference(KEY_IP_ADDRESS);
-
+ mEthMacAddress = findPreference(KEY_ETH_MAC_ADDRESS);mRes = getResources();mUnknown = mRes.getString(R.string.device_info_default);mUnavailable = mRes.getString(R.string.status_unavailable);
@@ -283,7 +287,61 @@ public class Status extends SettingsPreferenceFragment {String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();mWifiMacAddress.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : mUnavailable);}
+ private void setEthStatus() {
+ String macAddress = getMacAddress("eth0");
+ mEthMacAddress.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : mUnavailable);
+ }
+
+ private String getMacAddress(String iface) {
+ String cmd = "busybox ifconfig "+iface+" | grep 'HWaddr' | busybox awk '{print $5}'";
+ String str = execRootCmd(cmd);
+ if(str != null )
+ {
+ return str;
+ }
+ return null;
+ }
+ public static String execRootCmd(String cmd) {
+ String result = "";
+ DataOutputStream dos = null;
+ DataInputStream dis = null;+ try {
+ Process p = Runtime.getRuntime().exec("su");// The Android system processed by root has Su command
+ dos = new DataOutputStream(p.getOutputStream());
+ dis = new DataInputStream(p.getInputStream());
+
+ Log.i(TAG, cmd);
+ dos.writeBytes(cmd + "\n");
+ dos.flush();
+ dos.writeBytes("exit\n");
+ dos.flush();
+ String line = null;
+ while ((line = dis.readLine()) != null) {
+ Log.d("result", line);
+ result += line;
+ }
+ p.waitFor();
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ if (dos != null) {
+ try {
+ dos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (dis != null) {
+ try {
+ dis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ return result;
+ }private void setIpAddressStatus() {String ipAddress = Utils.getDefaultIpAddresses(this.mCM);if (ipAddress != null) {
@@ -309,6 +367,7 @@ public class Status extends SettingsPreferenceFragment {void updateConnectivity() {setWimaxStatus();setWifiStatus();
+ setEthStatus();setBtStatus();setIpAddressStatus();}
效果图:
小知识:Linux awk 命令