wifi scan is not performed when calling wifi.startScan() outside the MainActivity

811 views Asked by At

I'm working on an application which loads a map ( as an image). I'm using a customized view to load the image. I have to perform a wifi scan and store the results whenever the user touches the map.

I have declared the wifi manager this way:

public static WifiManager wifi;

so that I can use it inside my customized view. When I call wifi.startScan(); from the customized view it doesn't perform the scan. It runs without errors or exceptions but no scan result is available.

This is the code of my broadcast receiver:

   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");


        }
        }

As far as understand how scanning work, whenever wifi.startScan() is called, the code inside onReceiver() should be executed when the scan result is available. But this is not happening.

Can anyone tell me what is wrong here?? or what should I do in order to start the scan from my customized view?

Note: I have added the needed permission and scan works if I call it inside the MainActivity **EDIT: **

I have registered the receiver in the MainActivity using this:

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

I'm calling wifi.startScan() in onTouch() function in my customized view:

    @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;
}

EDIT

I unregister the receiver in onPause() and I register it again in onResume().

2

There are 2 answers

2
Sahil Bahl On BEST ANSWER

Since you unregister your broadcast receiver onPause() of the activity , once your activity is destroyed you will not be able to get the system broadcast for wifi scan results available whenever you call wifi.startScan()

You will need to define a broadcast receiver and register it in the xml to be able to listen to scan results outside the activity.

Make receiver for that:

    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();     
    } 
}

In your Manifest add

<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

Now whenever you call wifiManager.startScan() from any activity or anywhere else in the application you will get a system broadcast

5
Adi Tiwari On

May be this is because you are not registering your reciever like this in your source code

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

It will be helpful in answering if you provide some more information, so if this does not works please provide some more information