Android:检查wifi列表中是否有特定的SSID

时间:2015-04-01 21:30:25

标签: android

我正在尝试显示当前在范围内的特定Wi-Fi SSID,显示在我的“活动”中,但没有显示任何内容。在当前状态下,我获得了连接的SSID,但我想知道SSID是否在范围内,也是可用并且没有连接。此后,此SSID将显示在我的xml文件中。



	public  String getCurrentSsid(Context context) {
		String ssid = null;
		// Check wheather the WIFI is available.
		ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		if (networkInfo.isAvailable()) {
			// get the wifi name
			final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
			final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
			if(connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())){
				ssid = connectionInfo.getSSID();
				textWifi.setText(ssid);
				
			}
		}
		return ssid;
	}




1 个答案:

答案 0 :(得分:0)

我刚刚测试了你的代码,它对我有用。它显示当前连接的SSID。

编辑:您的代码显示当前连接的SSID(如果已连接),现在看起来您想要获取当前范围内所有SSID的列表。

确保正确设置AndroidManifest.xml:

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

编辑2:现在很清楚你想要获得扫描结果并处理它们,我只是想出了这个简单的例子:

import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.List;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = (TextView) this.findViewById(R.id.ScanResults);

        WifiManager wifiManager = (WifiManager) this.getSystemService(this.WIFI_SERVICE);

        WifiInfo info = wifiManager.getConnectionInfo();

        final List<ScanResult> results = wifiManager.getScanResults();

        if (results != null) {
            StringBuffer buf = new StringBuffer();

            for (int i = 0; i < results.size(); i++) {
                String ssid = results.get(i).SSID;
                if (ssid.startsWith("sv-")) {
                    buf.append(ssid + "\n");
                }
            }

            text.setText(buf.toString());
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

在activity_main.xml中:

<TextView
        android:id="@+id/ScanResults"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />