我试图通过ScanResult列出可用的无线网络。
有人能告诉我代码中的错误吗?我没有得到所需的输出。
这是我的代码:
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);
}
}
}
答案 0 :(得分:1)
试用此代码
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
<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"/>
答案 1 :(得分:0)
试用此代码:
清单许可:
机器人:名称=&#34; android.permission.ACCESS_WIFI_STATE&#34; 机器人:名称=&#34; android.permission.CHANGE_WIFI_STATE&#34;
<强>活动强>
public class MainActivity extends Activity implements OnClickListener {
Button setWifi;
ListView listViwProvider;
WifiManager wifiManager;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;
List<String> listOfProvider;
ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfProvider = new ArrayList<String>();
listViwProvider = (ListView) findViewById(R.id.list_view_wifi);
setWifi = (Button) findViewById(R.id.btn_wifi);
setWifi.setOnClickListener(this);
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled() == true) {
setWifi.setText("ON");
scaning();
}else {
wifiManager.setWifiEnabled(true);
setWifi.setText("ON");
scaning();
}
listViwProvider.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ImportDialog action = new ImportDialog(MainActivity.this, (wifiList.get(position)).toString());
action.showDialog();
}
});
}
private void scaning() {
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
}
@Override
public void onClick(View arg0) {
if (wifiManager.isWifiEnabled() == true) {
wifiManager.setWifiEnabled(false);
setWifi.setText("OFF");
listViwProvider.setVisibility(ListView.GONE);
}
else if (wifiManager.isWifiEnabled() == false) {
wifiManager.setWifiEnabled(true);
setWifi.setText("ON");
listViwProvider.setVisibility(ListView.VISIBLE);
scaning();
}
}
protected void onPause() {
super.onPause();
unregisterReceiver(receiverWifi);
}
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
wifiList = wifiManager.getScanResults();
Collections.sort(wifiList, new Comparator<ScanResult>() {
@Override
public int compare(ScanResult lhs, ScanResult rhs) {
return (lhs.level > rhs.level ? -1
: (lhs.level == rhs.level ? 0 : 1));
}
});
listOfProvider.clear();
String providerName;
for (int i = 0; i < wifiList.size(); i++) {
providerName = (wifiList.get(i).SSID).toString()
+"\n"+(wifiList.get(i).BSSID).toString();
listOfProvider.add(providerName);
}
adapter = new ListAdapter(MainActivity.this, listOfProvider);
listViwProvider.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
<强>适配器强>
public class ListAdapter extends ArrayAdapter<String> {
Activity mActivity;
List<String> wifiName;
private LayoutInflater inflater;
public ListAdapter(Activity a, List<String> wifiName) {
super(a, R.layout.single_list, wifiName);
mActivity = a;
inflater = LayoutInflater.from(mActivity);
this.wifiName = wifiName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.single_list, parent, false);
TextView wifiProvider = (TextView) convertView
.findViewById(R.id.txt_wifi_provider);
wifiProvider.setText(wifiName.get(position));
return convertView;
}
}
答案 2 :(得分:0)
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends Activity {
private StringBuilder sb = new StringBuilder();
List<ScanResult> scanList;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
getWifiNetworksList();
}
private void getWifiNetworksList(){
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
final WifiManager wifiManager =
(WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
sb = new StringBuilder();
scanList = wifiManager.getScanResults();
sb.append("\n Number Of Wifi connections :" + " " +scanList.size()+"\n\n");
for(int i = 0; i < scanList.size(); i++){
sb.append(new Integer(i+1).toString() + ". ");
sb.append((scanList.get(i)).toString());
sb.append("\n\n");
}
Toast.makeText(getApplicationContext(),""+sb.toString(),Toast.LENGTH_LONG).show();
textView.setText(sb);
}
},filter);
}
}
Manifest:
<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"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />