如何在Android上获取wifi硬件信息

Posted

技术标签:

【中文标题】如何在Android上获取wifi硬件信息【英文标题】:How to get wifi hardware information on Android 【发布时间】:2012-08-29 10:15:14 【问题描述】:

以下将为我提供基本的 wifi 信息,但我想要硬件信息:

WiFiScanReceiver.java

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

public class WiFiScanReceiver extends BroadcastReceiver 

    private static final String TAG = "WiFiScanReceiver";
    systemInfo systemInfo;

    public WiFiScanReceiver(systemInfo systemInfo) 
        super();
        this.systemInfo = systemInfo;
    

    @Override
    public void onReceive(Context c, Intent intent) 
        List<ScanResult> results = systemInfo.wifi.getScanResults();
        ScanResult bestSignal = null;
        for (ScanResult result : results) 
            if (bestSignal == null
                    || WifiManager.compareSignalLevel(bestSignal.level,
                            result.level) < 0) bestSignal = result;
        
        String message = String.format(
                "%s networks found. %s is the strongest.", results.size(),
                bestSignal.SSID);
        Toast.makeText(systemInfo, message, Toast.LENGTH_LONG).show();
        Log.d(TAG, "onReceive() message: " + message);
    

systemInfo.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class systemInfo extends Activity implements OnClickListener 

    private static final String TAG = "WiFiDemo";
    WifiManager wifi;
    BroadcastReceiver receiver;
    TextView textStatus;
    Button buttonScan;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Setup UI
        textStatus = (TextView) findViewById(R.id.textStatus);
        buttonScan = (Button) findViewById(R.id.buttonScan);
        buttonScan.setOnClickListener(this);
        // Setup WiFi
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        // Get WiFi status
        WifiInfo info = wifi.getConnectionInfo();
        textStatus.append("\n\nWiFi Status: " + info.toString());
        // List available networks
        List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
        for (WifiConfiguration config : configs) 
            textStatus.append("\n\n" + config.toString());
        
        // Register Broadcast Receiver
        if (receiver == null) receiver = new WiFiScanReceiver(this);
        registerReceiver(receiver, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        Log.d(TAG, "onCreate()");
        MemoryInfo mi = new MemoryInfo();
        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);
        long availableMegs = mi.availMem / 1048576L;
        System.out.println("availableMegs:::::::::::" + availableMegs);
    

    @Override
    public void onStop() 
        unregisterReceiver(receiver);
    

    public void onClick(View view) 
        Toast.makeText(this, "On Click Clicked. Toast to that!!!",
                Toast.LENGTH_LONG).show();
        if (view.getId() == R.id.buttonScan) 
            Log.d(TAG, "onClick() wifi.startScan()");
            wifi.startScan();
            String ab = getInfo();
            System.out.println("CPU INFORMATON:::::::::::::\n" + ab);
            textStatus.append("CPU INFORMATON:::::::::::::\n" + ab);
        
    

    private String getInfo() 
        StringBuffer sb = new StringBuffer();
        sb.append("abi: ").append(Build.CPU_ABI).append("\n");
        if (new File("/proc/cpuinfo").exists()) 
            try 
                BufferedReader br = new BufferedReader(new FileReader(new File(
                        "/proc/cpuinfo")));
                String aLine;
                while ((aLine = br.readLine()) != null) 
                    sb.append(aLine + "\n");
                
                if (br != null) 
                    br.close();
                
             catch (IOException e) 
                e.printStackTrace();
            
        
        return sb.toString();
    

权限是

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

这给了我基本的wifi信息。但我想要硬件信息..

一个重要的区别是 CPU 架构。虽然几乎所有的Android设备都使用ARM CPU,但它有不同的版本,包括armv5、armv5te、armv6、armv6 with VFP、armv7、armv7 with VFP、armv7 with neon等。

【问题讨论】:

【参考方案1】:

如果您的问题是如何获取有关 WiFi 芯片的硬件信息,那么答案很简单:Android 不提供这种数据,除了 MAC 地址(顺便说一句,它不是真的是硬件信息)。

【讨论】:

以上是关于如何在Android上获取wifi硬件信息的主要内容,如果未能解决你的问题,请参考以下文章

怎么用Java获取iOS设备的硬件信息?

如何使用 C++ 在 Linux 中获取硬件信息

如何用 python 读取硬件信息

Android之Wifi学习

如何通过 .NETCore 获取 Linux,Mac 的硬件信息?

如何使用 C++ 在 Windows 中获取硬件信息?