Cyber Sezar
Cyber Sezar

Reputation: 5

scan available wireless networks every 5 second java android

I Want To Check For Available Wireless Networks Every 5 Second In My Phone.

so i write This code for my app but this code check WIFI Networks every 15 second !

This Is a service , that scan WIFI Networks every 15 second and if founds a without password WIFI vibrates the phone an play a sound...

How Can I Check Wireless Networks every 5 or 3 seconds ?

package com.sirbl4ck.wififinder;

import java.util.List;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.Vibrator;
import android.os.PowerManager;
import android.widget.Toast;
import android.media.MediaPlayer;

public class MyService extends Service {
    WifiManager mainWifiObj;
    WifiScanReceiver wifiReciever;
    PowerManager pm;
    PowerManager.WakeLock wl;

    public MyService() {
    }
    public IBinder onBind(Intent arg0) {        
        return null;
    }
    @Override
    public void onCreate() {
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"Log");
        wl.acquire();
        mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();
        mainWifiObj.startScan();
        registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        Toast.makeText(this, "WIFI Finder Service Started", Toast.LENGTH_LONG).show();
    }
    public void onDestroy() {
        wl.release();
        Toast.makeText(this, "WIFI Finder Service Stoped", Toast.LENGTH_LONG).show();
    }
    public void alert(){
        final MediaPlayer als = MediaPlayer.create(this, R.raw.alertm);
        als.start();
    }
    class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        public void onReceive(Context c, Intent intent) {
           List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
           for(int iss=0;iss<wifiScanList.size();iss++){
             String uug=wifiScanList.get(iss).toString();
             if(uug.contains("WPA") && !uug.contains("WEP")){
                 v.vibrate(1500);
                 alert();
                 Toast.makeText(MyService.this, "WIFI Found !", Toast.LENGTH_LONG).show();
             }
           }
        }
     }
}

Upvotes: 0

Views: 1970

Answers (1)

Wagner Michael
Wagner Michael

Reputation: 2192

You could try use a recursive Thread:

public void startScan(){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mainWifiObj.startScan();
            startScan();
        }
    }, 5*1000);
}

Upvotes: 2

Related Questions