获取android 设备的cpu gpu信息 mac地址

1.获取cpu信息:cpu信息存在于/proc/cpuinfo文件下,adb shell 进去后用 cat /proc/cpuinfo 可以查看

  

   private String getCpuInfo() {  
        FileReader fr = null;  
        BufferedReader br = null;  
        String text=null; 
        StringBuilder sb=new StringBuilder();
        try {  
            fr = new FileReader("/proc/cpuinfo");  
            br = new BufferedReader(fr);  
            text = br.readLine(); 
            sb.append(text);
            while (text!=null) {
            	text = br.readLine(); 
            	sb.append("\r\n");
                sb.append(text);
			}
            //String[] array = text.split(":\\s+", 2); 
            return sb.toString();
            //return array[1];  
        } catch (FileNotFoundException e) {
        	Log.d("you", "getCpuInfo FileNotFoundException e:"+e.getMessage() );
        } catch (IOException e) { 
        	Log.d("you", "getCpuInfo IOException e:"+e.getMessage() );
        } finally {  
            try {  
                fr.close();  
                br.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
      
        }  
        Log.d("you", "getCpuInfo  return space" );
        return "";  
    }

2.获取gpu信息  Android的GPU信息需要通过OpenGL来获取

   实现一个GLSurfaceView.Renderer 将renderer设置给GLSurfaceView 在Activity中this.setContentView(glView); 

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;

public class DemoActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GLSurfaceView glView = new DemoGLSurfaceView(this);
        this.setContentView(glView);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public class DemoGLSurfaceView extends GLSurfaceView {

        DemoRenderer mRenderer;

        public DemoGLSurfaceView(Context context) {
            super(context);
            setEGLConfigChooser(8, 8, 8, 8, 0, 0);
            mRenderer = new DemoRenderer();
            setRenderer(mRenderer);
        }
    }

    public class DemoRenderer implements GLSurfaceView.Renderer {

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            Log.d("SystemInfo",
                    "GL_RENDERER = " + gl.glGetString(GL10.GL_RENDERER));
            Log.d("SystemInfo", "GL_VENDOR = " + gl.glGetString(GL10.GL_VENDOR));
            Log.d("SystemInfo",
                    "GL_VERSION = " + gl.glGetString(GL10.GL_VERSION));
            Log.i("SystemInfo",
                    "GL_EXTENSIONS = " + gl.glGetString(GL10.GL_EXTENSIONS));
        }

        public void onDrawFrame(GL10 arg0) {
        }

        public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
        }
    }

}


在实际中可以通过在布局中添加一个GLSurfaceView,当获取到信息后将其移除。


3.获取mac地址:分为有线和wifi

private String loadFileAsString(String filePath) throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    }

    /*
    * Get  MacAddress
    */
    private String getMacAddress(String name){//name可能传入eth0   eth1  eth2............
        try {
            return loadFileAsString("/sys/class/net/"+name+ "/address")
                .toUpperCase().substring(0, 17);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }catch (Exception e) {
        	return null;
		}
    }

    private String getMacFromWifi(){
        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        
        int state = wifi.getWifiState();//如果不成功可能尝试去打开wifi再来获取  
        if(state == WifiManager.WIFI_STATE_ENABLED ||state == WifiManager.WIFI_STATE_ENABLING ){
        	 WifiInfo info = wifi.getConnectionInfo();
        	 if(info==null){
        		 return null;
        	 }
        	 return info.getMacAddress();
        }
        
        return null;
    }

wifi也可以获取有线的方式(读取文件的方式)获取,wifi mac地址的文件是:/sys/class/net/wlan0/address


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值