wifiManager.startScan没有返回任何结果(需要一些指导) [英] wifiManager.startScan not returning any results (need some guidance please)
问题描述
myWifiMan.startScan();
列表< ScanResult> wifiList = myWifiMan.getScanResults();
if(wifiList!= null){
//构造线索
for(int i = 0; i< wifiList.size(); i ++){
message = message +'+ wifiList.get(i).SSID +':+ Integer.toString(wifiList.get(i).level);
if((i + 1)< wifiList.size())
message = message +,;
}
message = message +}];
由于下面给出的答案,以下代码会给我所需的结果?
private void initializeWiFiListener(){
System.out.println(execution initializeWiFiListener);
String connectivity_context = Context.WIFI_SERVICE;
final WifiManager wifi =(WifiManager)getSystemService(connectivity_context);
if(!wifi.isWifiEnabled()){
if(wifi.getWifiState()!= WifiManager.WIFI_STATE_ENABLING){
wifi.setWifiEnabled(true);
}
}
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context,Intent intent){
WifiManager wifiManager =(WifiManager)getSystemService(Context.WIFI_SERVICE);
boolean a = wifiManager.startScan(); //请求扫描访问点
final List< ScanResult> results = wifiManager.getScanResults() ; //从最后一次扫描获得的访问点列表
(最终的ScanResult结果:结果){
System.out.println(ScanResult级别:+ result.level);
}
}
},新的IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
可能是因为 startScan
方法仅触发附近访问点的发现,并且不会立即将其提供给您。
Android文档说明了此方法它:
请求扫描接入点。立即返回结果的
可用性通过在扫描完成时发送的
异步事件在之后已知。
所以,你必须将 BroadcastReceiver
注册到 SCAN_RESULTS_AVAILABLE_ACTION
,以获得扫描完成后,可用的访问点列表。
此时,您可以调用 getScanResults()
方法将返回找到的接入点列表。
编辑:还要确保在启动扫描之前在设备上启用Wi-Fi。
At this moment in time I am trying to build an Android application that will be able to locate a wifi enabled device indoors. So I have broken it down into various steps in order and 1 of the steps is to scan for available wireless networks and return the signal strength etc associated with each access point that it discovers (it doesn't have to connect to the access points but just has to ping them for information). Below shows the snippet of code that I have created and when the code is debugged it isn't identifying an wifi access points so could someone tell me where the problem is or point me in the right direction.
myWifiMan.startScan();
List<ScanResult> wifiList = myWifiMan.getScanResults();
if (wifiList != null) {
//Construct Clue
for(int i = 0; i < wifiList.size(); i++) {
message = message + "'" + wifiList.get(i).SSID +"':" + Integer.toString(wifiList.get(i).level);
if((i+1) < wifiList.size())
message = message + ",";
}
message = message + "}]";
due to the answer given below would the following code give me the desired results?
private void initializeWiFiListener(){
System.out.println("executing initializeWiFiListener");
String connectivity_context = Context.WIFI_SERVICE;
final WifiManager wifi = (WifiManager)getSystemService(connectivity_context);
if(!wifi.isWifiEnabled()){
if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
wifi.setWifiEnabled(true);
}
}
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean a= wifiManager.startScan();//request a scan for access points
final List<ScanResult> results= wifiManager.getScanResults();//list of access points from the last scan
for(final ScanResult result : results){
System.out.println("ScanResult level: "+ result.level);
}
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
That's probably because the startScan
method only triggers the discovery of the nearby access points, and doesn't make them available to you immediatly.
The Android documentation states about this method that it :
Request a scan for access points. Returns immediately. The availability of the results is made known later by means of an asynchronous event sent on completion of the scan.
So, you'd have to register a BroadcastReceiver
to the SCAN_RESULTS_AVAILABLE_ACTION
in order to get the list of the available access points as soon as the scan completes.
At this time, you can call the getScanResults()
method which will return the list of the found access points.
EDIT : Also make sure that the Wi-Fi is enabled on the device before launching a scan.
这篇关于wifiManager.startScan没有返回任何结果(需要一些指导)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!