可用的Wifi设备列表 [英] List of available Wifi devices
问题描述
我想显示可用的Wifi设备"列表.这是我的代码,我不明白这里有什么错误:
I want to display list of Available Wifi devices. This is my code, I do not understand what mistake is here:
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的设备属性.我该怎么办?
This is my code, I want to get Wifi enabled device properties specifically in Android by programmatically. How can I get that?
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++;
}
推荐答案
您可以通过
List<ScanResult> mScanResults = mWifiManager.getScanResults();
然后遍历 mScanResults
并使用 results.SSID
获得SSID.另外,如果您有兴趣获得最佳网络,可以使用 WifiManager.compareSignalLevel(int rssiA,int rssiB)
比较两个网络.
Then iterating over mScanResults
and getting SSID using results.SSID
. Also if you are interested to get the best network you can use WifiManager.compareSignalLevel(int rssiA, int rssiB)
for comparing two networks.
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);
您可以从我的 存储库
下载完整的演示.
You can download complete demo from my repository
.
这篇关于可用的Wifi设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!