From 1067ad6676a2947e85d6367ff39fb6093060b4ff Mon Sep 17 00:00:00 2001 From: William Lachance Date: Wed, 11 Jul 2012 12:51:44 -0400 Subject: [PATCH] Bug 768957 - Fall back to getting mac address using wifiman on older versions of Android;r=jmaher --- .../sutagent/android/SUTAgentAndroid.java | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/build/mobile/sutagent/android/SUTAgentAndroid.java b/build/mobile/sutagent/android/SUTAgentAndroid.java index 4d8aca6e82ba..dc8a5e928860 100755 --- a/build/mobile/sutagent/android/SUTAgentAndroid.java +++ b/build/mobile/sutagent/android/SUTAgentAndroid.java @@ -33,6 +33,7 @@ import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.WifiLock; import android.os.BatteryManager; +import android.os.Build.VERSION; import android.os.Bundle; import android.os.Handler; import android.telephony.TelephonyManager; @@ -125,25 +126,41 @@ public class SUTAgentAndroid extends Activity setUpNetwork(sIniFile); String macAddress = "Unknown"; - try { - NetworkInterface iface = NetworkInterface.getByInetAddress(InetAddress.getAllByName(getLocalIpAddress())[0]); - if (iface != null) - { - byte[] mac = iface.getHardwareAddress(); - if (mac != null) - { - StringBuilder sb = new StringBuilder(); - Formatter f = new Formatter(sb); - for (int i = 0; i < mac.length; i++) - { - f.format("%02x%s", mac[i], (i < mac.length - 1) ? ":" : ""); - } - macAddress = sUniqueID = sb.toString(); - } - } + if (android.os.Build.VERSION.SDK_INT > 8) { + try { + NetworkInterface iface = NetworkInterface.getByInetAddress(InetAddress.getAllByName(getLocalIpAddress())[0]); + if (iface != null) + { + byte[] mac = iface.getHardwareAddress(); + if (mac != null) + { + StringBuilder sb = new StringBuilder(); + Formatter f = new Formatter(sb); + for (int i = 0; i < mac.length; i++) + { + f.format("%02x%s", mac[i], (i < mac.length - 1) ? ":" : ""); + } + macAddress = sUniqueID = sb.toString(); + } + } + } + catch (UnknownHostException ex) {} + catch (SocketException ex) {} } - catch (UnknownHostException ex) {} - catch (SocketException ex) {} + else + { + // Fall back to getting info from wifiman on older versions of Android, + // which don't support the NetworkInterface interface + WifiManager wifiMan = (WifiManager)getSystemService(Context.WIFI_SERVICE); + if (wifiMan != null) + { + WifiInfo wifi = wifiMan.getConnectionInfo(); + if (wifi != null) + macAddress = wifi.getMacAddress(); + if (macAddress != null) + sUniqueID = macAddress; + } + } if (sUniqueID == null) {