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"/>
感謝
您是否在清單文件中添加了訪問Wi-Fi的權限? –