Search available wifi networks in sleep mode in android

225 Views Asked by At

I want to check available networks bssids "just check no connecting" for that i use this code

wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);     
wifiManager.startScan();     
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(myrec, filter);     
------------------------

private BroadcastReceiver myrec=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(tag+" myrec","scan complete");

        }

    };     

its work fine in many phone i tested but in galaxytab4 "smt231" in sleep mode not working and in normal mode take too long time to complete scan "more than 2 min" but when i go to settings>wifi my brodcast registered right away.
can anyone help me about this problem.

update
i found wifilock and now my code is

wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

_wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, this.getClass().getName() + ".WIFI_LOCK");
_wifiLock.setReferenceCounted(true);

if(!_wifiLock.isHeld()){
    _wifiLock.acquire();
}     
wifiManager.startScan();
-----------------      
private BroadcastReceiver myrec=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(tag+" myrec","scan complete");

        }

    };     

but still not working in sleep mode.
and i am sure about running my code but scan reciver broadcast never going to run in sleep mode.

intersting thing is when you turn your wifi on even when your phone is in sleep mode when your going to range of known wifi network its going to connect to network and get notifications like emails. so its possible to check for wifi networks in sleep mode the thing is how?

0

There are 0 best solutions below