本文介绍了ANDROID:-无法使用ScanResult获取可用的wifi列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过ScanResult列出可用的wifi网络.
I'm trying to list available wifi network by ScanResult.
谁能告诉我我的代码出了什么问题?我没有得到想要的输出.
Can anyone tell me what's wrong in my code? I'm not getting the desired output.
这是我的代码:
public class Activity_scanwifi extends AppCompatActivity {
Switch aSwitch;
TextView textView;
WifiManager wifiManager;
WifiInfo wifiInfo;
String info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scanwifi);
aSwitch=(Switch) findViewById(R.id.s);
textView=(TextView) findViewById(R.id.textView);
wifiManager=(WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// switch on
if(isChecked && ! wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(true);
wifiInfo=wifiManager.getConnectionInfo();
}
// switch off
if (!isChecked && wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
}
});
broadcast b=new broadcast();
registerReceiver(b,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public class broadcast extends BroadcastReceiver{
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceive(Context context, Intent intent) {
// onReceive method updates any change in the available wifi
StringBuffer stringBuffer=new StringBuffer();
/// List<WifiConfiguration> list=wifiManager.getConfiguredNetworks();
List<ScanResult> list=wifiManager.getScanResults();
//for(WifiConfiguration wifiConfiguration : list)
for(ScanResult scanResult : list)
{
stringBuffer.append(scanResult);
}
textView.setText(stringBuffer);
}
}
}
推荐答案
尝试此代码
WifiList.java
WifiList.java
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.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class WifiList extends Activity {
WifiManager mWifiManager;
ArrayList<String> mWifiList;
@BindView(R.id.lst_wifilist)
ListView lst_wifilist;
@BindView(R.id.ll_scan)
LinearLayout ll_scan;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifilist);
ButterKnife.bind(this);
mWifiList = new ArrayList<>();
mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mWifiList.clear();
scanNetwork();
}
private void scanNetwork() {
registerReceiver(mWifiScanReceiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mWifiManager.startScan();
}
private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<ScanResult> mScanResults = mWifiManager.getScanResults();
// add your logic here
mWifiList.clear();
for (int i = 0; i < mScanResults.size(); i++) {
Log.e("Scanned Wifi Name :- ", mScanResults.get(i).SSID);
mWifiList.add(mScanResults.get(i).SSID);
WifiListAdapter adapter = new WifiListAdapter(WifiList.this, mWifiList);
lst_wifilist.setAdapter(adapter);
}
}
}
};
}
AndroidMenifest.xml
AndroidMenifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
这篇关于ANDROID:-无法使用ScanResult获取可用的wifi列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!