我想显示可用的Wifi设备列表。这是我的代码,我不明白这里有什么错误:
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);
}
String[] str1 = null;
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,str1);
lv.setAdapter(adapter);
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());
boolean b=wifi.isWifiEnabled();
if(b){
wifi.setWifiEnabled(false);
Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_SHORT).show();
enter code here
这是我的代码,我希望通过编程方式在Android中专门获取支持Wifi的设备属性。我怎么能得到它?
public void onReceive(Context c, Intent intent)
{
results = wifi.getScanResults();
size = results.size();
int i = 0;
str1 = new String[size];
for (ScanResult result : results){
str1[i] = result.SSID + " " + result.level;
i++;
}
答案 0 :(得分:6)
您可以
获取可用的WIFI扫描结果List<ScanResult> mScanResults = mWifiManager.getScanResults();
然后迭代mScanResults
并使用results.SSID
获取SSID。此外,如果您有兴趣获得最佳网络,可以使用WifiManager.compareSignalLevel(int rssiA, int rssiB)
来比较两个网络。
ScanResult bestResult = null;
for(ScanResult results : mScanResults){
Log.d("result",results.SSID);
if(bestResult == null || WifiManager.compareSignalLevel(bestResult.level,
results.level) < 0){
bestResult = results;
}
}
String message = String.format("%s networks found. %s is the strongest.",
mScanResults.size(), bestResult.SSID);
Log.d("best network",message);
您可以从我的repository
下载完整的演示。
答案 1 :(得分:1)
试试这段代码。
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)
{ }
}
}
答案 2 :(得分:0)
基于android开发者网站的最佳搜索wifi列表的方法如下:
.wrap{
position: absolute;
top: 0;
}
.box1 {
border: 1px solid red;
width: 100px;
word-break: break-all;
}
.box2 {
position:absolute;
margin-top: 50px;
border: 1px solid green;
width: 100px;
}
Android 8.0和Android 8.1:
成功调用WifiManager.getScanResults()需要以下任一权限:
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
CHANGE_WIFI_STATE
如果正在调用的应用没有这些权限,则调用将失败,并显示SecurityException。
或者,在运行Android 8.0(API级别26)及更高版本的设备上,您可以使用CompanionDeviceManager代表您的应用对附近的伴随设备进行扫描,而无需位置许可。
源:https://developer.android.com/guide/topics/connectivity/wifi-scan