Bug 768957 - Fall back to getting mac address using wifiman on older versions of Android;r=jmaher

This commit is contained in:
William Lachance 2012-07-11 12:51:44 -04:00
Родитель 404c6d88fc
Коммит 1067ad6676
1 изменённых файлов: 35 добавлений и 18 удалений

Просмотреть файл

@ -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)
{