1.建立一個class並etxends BroadcastReceiver,當搜尋結束後會Toast出基地台SSID
class WifiReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { result_list = wifi_mng.getScanResults(); String ssid; for (int i = 0; i < result_list.size(); i++) { ssid = result_list.get(i).SSID; Toast.makeText(FirstActivity.this, ssid, Toast.LENGTH_LONG).show(); } } }2.主要程式碼,建立WifiManager後,宣告一個WifiReceiver,並且註冊此Receiver後啟動scan
wifi_mng = (WifiManager) getSystemService(FirstActivity.this.WIFI_SERVICE); wifi_rec = new WifiReceiver(); registerReceiver(wifi_rec, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); wifi_mng.startScan();3.變數宣告
private WifiManager wifi_mng; private WifiReceiver wifi_rec; private List<ScanResult> result_list;4.使用完後記得unregisterReceiver
unregisterReceiver(wifi_rec);
沒有留言:
張貼留言