2014-02-07 135 views
0

我檢查了我的設備Wifi已啓用,一旦啓用,我嘗試掃描所有可用的Wifi網絡。但無法檢測任何Wifi網絡。我的計劃是在Android中掃描WIFI

 protected void onCreate(Bundle savedInstanceState) { 
      String service1 = Context.WIFI_SERVICE; 
      wifi = (WifiManager)getSystemService(service1); 
      if(!wifi.isWifiEnabled()) 
       wifi.setWifiEnabled(true); 

      receiverWifi = new WifiReceiver(); 
      registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 
      wifi.startScan(); //startScan 
      displayWifiNetworks(); 
     } 

    class WifiReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      Toast.makeText(SDcard_DetailView.this, "in onReceive ...", Toast.LENGTH_LONG).show(); 
      wifiList = wifi.getScanResults(); 
      for(int i = 0; i< wifiList.size(); i++) { 
       System.out.println(wifiList.get(i).toString()); 
      } 

     } 
    } 


    public void displayWifiNetworks() { 
     //Toast.makeText(this, "displaying WiFi information...", Toast.LENGTH_LONG).show(); 
     //if(null != wifi.getScanResults().get(1).SSID)  
     if(wifiList.size() > 0) 
      Toast.makeText(this, "Networks available!", Toast.LENGTH_LONG).show(); 


    } 

接收清單文件

 <!-- in order to access the WifiManage.getScanResults() --> 
     <receiver 
      android:name="com.example.connectivitymanager.MainActivity$WifiReceiver"> 
      <intent-filter> 
       <action android:name="android.net.wifi.STATE_CHANGE"></action> 
       <action android:name="android.net.wifi.SCAN_RESULTS"></action> 
      </intent-filter> 
     </receiver> 

我也有權爲

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

感謝

+0

您是否在清單文件中添加了訪問Wi-Fi的權限? –

回答

1
package wf1.wf2.wf3;  
import java.util.ArrayList; 
import java.util.HashMap;  
import java.util.List;  
import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context;  
import android.content.Intent;  
import android.content.IntentFilter;  
import android.net.wifi.ScanResult;  
import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiManager;  
import android.os.Bundle;  
import android.util.Log; 
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.AdapterView;  
import android.widget.Button;  
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
import android.widget.TextView;  
import android.widget.Toast; 

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) 
     { }   
    }  
} 

使用以下權限

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

你有下面的行嗎?

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

您可能無法訪問WIFI,因爲您甚至不允許訪問互聯網!