安卓获取设备信息
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MiddleCS
{
public static int GetAndroidBatteryLevel(Activity unityActivity)
{
IntentFilter iFilter = new IntentFilter("android.intent.action.BATTERY_CHANGED");
Intent intent = unityActivity.getApplication().registerReceiver(null, iFilter);
int rawLevel = intent.getIntExtra("level", -1);
int scale = intent.getIntExtra("scale", -1);
int status = intent.getIntExtra("status", 0);
int level = -1;
if ((rawLevel > 0) && (scale > 0)) {
level = rawLevel * 100 / scale;
}
Log.d("pow", level + "|" + scale + "|" + status);
return level;
}
//获取品牌
public static String GetBrand()
{
return android.os.Build.BRAND;
}
//获取手机型号
public static String GetModel()
{
return android.os.Build.MODEL;
}
//secure android id
public static String GetSecureID(Activity unityActivity)
{
return Settings.Secure.getString(unityActivity.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
//设备id
public static String GetDeveiceId(Activity unityActivity)
{
String deviceId = ((TelephonyManager) unityActivity.getSystemService(unityActivity.TELEPHONY_SERVICE)).getDeviceId();
Log.d("pow", deviceId);
return deviceId;
}
//获取mac地址
public static String GetMac()
{
String address = "";
try{
// 把当前机器上的访问网络接口的存入 Enumeration集合中
Enumeration<NetworkInterface> interfaces = null;
interfaces = NetworkInterface.getNetworkInterfaces();
Log.d("pow"," interfaceName = " + interfaces);
while (interfaces.hasMoreElements()) {
NetworkInterface netWork = interfaces.nextElement();
// 如果存在硬件地址并可以使用给定的当前权限访问,则返回该硬件地址(通常是 MAC)。
byte[] by = netWork.getHardwareAddress();
if (by == null || by.length == 0) {
continue;
}
StringBuilder builder = new StringBuilder();
for (byte b : by) {
builder.append(String.format("%02X:", b));
}
if (builder.length() > 0) {
builder.deleteCharAt(builder.length() - 1);
}
String mac = builder.toString();
Log.d("pow","interfaceName=" + netWork.getName() + ", mac=" + mac);
// 从路由器上在线设备的MAC地址列表,可以印证设备Wifi的 name 是 wlan0
if (netWork.getName().equals("wlan0")) {
Log.d("pow"," interfaceName =" + netWork.getName() + ", mac=" + mac);
address = mac;
}
}
}catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
}
return address.toUpperCase();
}