设备处于wifi共享模式时获取SSID

时间:2016-03-25 18:46:10

标签: android

请告诉我:当设备处于wifi共享模式时,是否有可能获得其他SSID的列表(我只需要名字,不再需要名字)?

2 个答案:

答案 0 :(得分:1)

您需要为您的Activity注册BroadcastReceiver,但在您需要添加访问网络状态的权限并在清单中修改它之前:

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

在我们创建BroadcastReceiver之前,我们需要设置一些内容:

ArrayList<String> ssidList;
WifiManager wifiManager;
WifiScanReceiver wifiReciever;

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

    //Wifi manager will be used to request a scan
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    //Your boradcast receiver, it will be registered with the system to be notified of SSID's available
    wifiReciever = new WifiScanReceiver();

    //A list to add all the available SSID's
    ssidList = new ArrayList<String>();

    //Requesting a scan, results will be returned through the BroadcastReceiver
    wifiManager.startScan();
}

这是您的BoradcastReceiver

class WifiScanReceiver extends BroadcastReceiver {

    public void onReceive(Context c, Intent intent) {
        List<ScanResult> wifiScanList = wifiManager.getScanResults();
        ssidList.clear(); // clear to make sure we do not get duplicated entries

        for (int i = 0; i < wifiScanList.size(); i++) {
            String ssid = wifiScanList.get(i).SSID; //Get the SSID
            ssidList.add(ssid); //add SSID to the list
            Log.d("SSID", ssid);
        }

        //You can call this to keep scaning if you need to, or you can set up a Timer to scan every X seconds
        //wifiManager.startScan();
    }
}

您需要在应用调用onResume时优先注册BroadcastReceiver,并在调用onPause时取消注册:

protected void onPause() {
    unregisterReceiver(wifiReciever);
    super.onPause();
}

protected void onResume() {
    registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}

这是完整的代码:

public class WifiActivity extends AppCompatActivity {

    ArrayList<String> ssidList;
    WifiManager wifiManager;
    WifiScanReceiver wifiReciever;

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

        //Wifi manager will be used to request a scan
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        //Your boradcast receiver, it will be registered with the system to be notified of SSID's available
        wifiReciever = new WifiScanReceiver();

        //A list to add all the available SSID's
        ssidList = new ArrayList<String>();

        //Requesting a scan, results will be returned through the BroadcastReceiver
        wifiManager.startScan();
    }

    protected void onPause() {
        unregisterReceiver(wifiReciever);
        super.onPause();
    }

    protected void onResume() {
        registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    class WifiScanReceiver extends BroadcastReceiver {

        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            ssidList.clear(); // clear to make sure we do not get duplicated entries

            for (int i = 0; i < wifiScanList.size(); i++) {
                String ssid = wifiScanList.get(i).SSID; //Get the SSID
                ssidList.add(ssid); //add SSID to the list
                Log.d("SSID", ssid);
            }

            //You can call this to keep scaning if you need to, or you can set up a Timer to scan every X seconds
            //wifiManager.startScan();
        }
    }
}

每次onReceive运行时,您都会看到所有可用的SSID列表,这些SSID将保存在ssidList中。

答案 1 :(得分:0)

试试这个answer

public class WiFiDemo extends Activity implements OnClickListener
 {      
    WifiManager wifi;       
    ListView lv;
    TextView textStatus;
    Button buttonScan;
    int size = 0;
    List<ScanResult> results;

    String ITEM_KEY = "key";
    ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
    SimpleAdapter adapter;

    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textStatus = (TextView) findViewById(R.id.textStatus);
        buttonScan = (Button) findViewById(R.id.buttonScan);
        buttonScan.setOnClickListener(this);
        lv = (ListView)findViewById(R.id.list);

        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() == false)
        {
            Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(true);
        }   
        this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
        lv.setAdapter(this.adapter);

        registerReceiver(new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
               results = wifi.getScanResults();
               size = results.size();
            }
        }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));                    
    }

    public void onClick(View view) 
    {
        arraylist.clear();          
        wifi.startScan();

        Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
        try 
        {
            size = size - 1;
            while (size >= 0) 
            {   
                HashMap<String, String> item = new HashMap<String, String>();                       
                item.put(ITEM_KEY, results.get(size).SSID + "  " + results.get(size).capabilities);

                arraylist.add(item);
                size--;
                adapter.notifyDataSetChanged();                 
            } 
        }
        catch (Exception e)
        { }         
    }    
}
相关问题