在MainActivity之外调用wifi.startScan()时不执行wifi扫描

时间:2015-06-17 13:32:49

标签: android android-wifi

我正在处理一个加载地图的应用程序(作为图像)。我使用自定义视图加载图像。我必须执行wifi扫描并在用户触摸地图时存储结果。

我已经用这种方式宣布了wifi管理员:

public static WifiManager wifi;

这样我就可以在自定义视图中使用它了。当我从自定义视图中调用wifi.startScan();时,它不会执行扫描。它运行时没有错误或异常,但没有可用的扫描结果。

这是我的广播接收器的代码:

   public class WifiReceiver extends BroadcastReceiver {


        /*
         * This receiver will set a flag in the main activity to indicate the new scan result has been set
         * it will set the new result too.
         * 
         */
        public void onReceive(Context c, Intent intent) {

            result=wifi.getScanResults();

            Log.d("surveyTool","scanning result set");


        }
        }

了解扫描工作的方式,每当调用wifi.startScan()时,应在扫描结果可用时执行onReceiver()内的代码。但这不会发生。

谁能告诉我这里有什么问题?或者我应该怎么做才能从我的自定义视图开始扫描?

注意:如果我在MainActivity中调用它,我已经添加了所需的权限和扫描  **编辑:**

我已使用以下方法在MainActivity中注册了接收器:

receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi,  new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

我在自定义视图中调用wifi.startScan()函数中的onTouch()

    @Override
public boolean onTouch(View v, MotionEvent event) {

    // the coordinate of the touch point
    x=event.getX();
    y=event.getY();

    try {
        touchManager.update(event);

        // single point touch
        if (touchManager.getPressCount() == 1) {


            position.add(touchManager.moveDelta(0)); // to keep the point drawn at the touch location
            info.addToStartPoint(touchManager.moveDelta(0));

            if((touchManager.moveDelta(0).getX()==0 && touchManager.moveDelta(0).getY()== 0))
            {


                PreviousPoint= CurrentPoint;
                CurrentPoint=info.Map(x, y);


                if(CurrentPoint.getX()!=PreviousPoint.getX() && CurrentPoint.getY()!= PreviousPoint.getY())
                {
                    // the point is on the image
                    if(CurrentPoint.getX()!=-1 && CurrentPoint.getY()!= -1)
                    {
                        MainActivity.wifi.startScan();
                        MainActivity.counter++;
                        Log.d("surveyTool","touch: "+MainActivity.counter);
                        MainActivity.location=CurrentPoint;
                        MainActivity.newLocationAvailable=true; 
                        MainActivity.AddNewFingerprin();
                    }

                //  Log.d("surveyTool", " new touch event occurred");
                    // here do the surveying and other stuff    
                }

            }
        }
        else {
            if (touchManager.getPressCount() == 2) {

                Vector2D current = touchManager.getVector(0, 1);
                Vector2D previous = touchManager.getPreviousVector(0, 1);
                float currentDistance = current.getLength();
                float previousDistance = previous.getLength();

                if (previousDistance != 0) {
                    scale *= currentDistance / previousDistance; // for the zoom
                    info.setZoomFactor(scale);

                }// end of != 0
            }// end of if ==2
        }// end of else

        invalidate(); // will cause a redraw
    }
    catch(Throwable t) {

    }
    return true;
}

修改

我在onPause()取消注册接收器,然后在onResume()再次注册。

2 个答案:

答案 0 :(得分:1)

可能是因为您没有在源代码中注册这样的收件人

WifiReceiver wifiReciever = new WifiReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

如果您提供更多信息,将有助于回答,如果这不起作用,请提供更多信息

答案 1 :(得分:1)

由于您取消注册活动的广播接收器onPause(),一旦您的活动被销毁,无论何时调用wifi.startScan()

,您都无法获得系统广播的wifi扫描结果。

您需要定义一个广播接收器并在xml中注册它,以便能够在活动之外收听扫描结果。

为此制作接收器:

    public class ScanResultsReceiver extends BroadcastReceiver {

    @Override 
    public void onReceive(Context context, Intent intent) {

     //You need to initialise the wifimanager like this 
     private WifiManager wifiManager;
     wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
     //Now get Scan Results
     List<ScanResult> results = wifiManager.getScanResults();     
    } 
}

在你的清单中添加

<receiver android:name=".wifiReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.net.wifi.SCAN_RESULTS"></action>
        </intent-filter>
    </receiver>
//Note: android:name  may differ according to your package structure

现在,无论何时从任何活动或应用程序中的任何其他地方调用wifiManager.startScan(),您都将获得系统广播