Android - 列出wifi接入点

时间:2013-12-23 10:03:41

标签: android wifi

我想获得无线接入点列表。非常感谢。我的代码在下面

public static void backupWifi(Context context) {
        WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        String id = wifiInfo.getBSSID();
        int ip = wifiInfo.getIpAddress();
        String MAC = wifiInfo.getMacAddress();
        String SSID = wifiInfo.getSSID();
        int idnw = wifiInfo.getNetworkId();


            Log.v("ID", id);
            Log.v("IP", String.valueOf(ip));
            Log.v("MAC", MAC);
            Log.v("SSID", SSID);
            Log.v("idnw", String.valueOf(idnw));
        }

2 个答案:

答案 0 :(得分:3)

您需要创建 BroadcastReceiver 来收听Wifi扫描结果:

private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction() == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
            List<ScanResult> mScanResults = wifi.getScanResults();
            // add your logic here
        }
    }
}

onCreate()中,您可以指定 mWifiManager 并启动扫描:

mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
mWifiManager.startScan();

处理扫描结果的代码将在每次新的扫描结果可用时运行,并更新结果。

答案 1 :(得分:2)

首先获取可用的wifi点列表 -

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> apList = wifiManager.getScanResults();

apList - 是最近一次扫描中所有可用wifi点的列表。

您可以将此apList设置为ListView。 注意: - 此方法仅返回最近扫描的wifi点