1 package com.android.pmc; 2 3 import android.app.AlarmManager; 4 import android.app.PendingIntent; 5 import android.content.BroadcastReceiver; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.net.wifi.ScanResult; 9 import android.net.wifi.WifiManager; 10 import android.net.wifi.WifiScanner; 11 import android.net.wifi.WifiScanner.ScanData; 12 import android.net.wifi.WifiScanner.ScanSettings; 13 import android.os.AsyncTask; 14 import android.os.PowerManager; 15 import android.os.SystemClock; 16 import android.util.Log; 17 18 /** 19 * Call wifi Gscan whenever an alarm is received. 20 */ 21 public class WifiGScanReceiver extends BroadcastReceiver { 22 int mScanCount = 0; 23 GScanTask mGScanTask; 24 PMCMainActivity mPMCMainActivity; 25 private WifiManager mWifiManager; 26 private Context mContext; 27 private PowerManager.WakeLock mWakeLock; 28 private WifiScanner mScan; 29 private ScanSettings mScanSettings; 30 private int mAlarmInterval; 31 private AlarmManager mAlarmManager; 32 private PendingIntent mAlarmIntent; 33 34 WifiGScanReceiver(PMCMainActivity activity, ScanSettings settings, int interval, AlarmManager alarmManager, PendingIntent alarmIntent)35 public WifiGScanReceiver(PMCMainActivity activity, ScanSettings settings, int interval, 36 AlarmManager alarmManager, PendingIntent alarmIntent) { 37 mPMCMainActivity = activity; 38 mScanSettings = settings; 39 mScanCount = 0; 40 mAlarmInterval = interval; 41 mAlarmManager = alarmManager; 42 mAlarmIntent = alarmIntent; 43 } 44 45 @Override onReceive(Context context, Intent intent)46 public void onReceive(Context context, Intent intent) { 47 if (mGScanTask != null && mGScanTask.getStatus() != AsyncTask.Status.FINISHED) { 48 Log.e(PMCMainActivity.TAG, "Previous Gscan still running."); 49 try { 50 mGScanTask.get(); 51 } catch (Exception e) { 52 Log.e(PMCMainActivity.TAG, "Gscan cancelled."); 53 } 54 } else { 55 mContext = context; 56 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 57 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WIFITEST"); 58 // Acquire the lock 59 mWakeLock.acquire(); 60 mScan = (WifiScanner) context.getSystemService(Context.WIFI_SCANNING_SERVICE); 61 Log.i(PMCMainActivity.TAG, "Starting GScan Task"); 62 mGScanTask = new GScanTask(); 63 mGScanTask.execute(mScanSettings); 64 } 65 scheduleGscan(); 66 } 67 68 /** 69 * Schedule the next Gscan. 70 */ scheduleGscan()71 public void scheduleGscan() { 72 Log.i(PMCMainActivity.TAG, "Scheduling the next gscan after " + mAlarmInterval); 73 mAlarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, 74 SystemClock.elapsedRealtime() + mAlarmInterval, mAlarmIntent); 75 } 76 77 /** 78 * Cancel the Gscans. 79 */ cancelGScan()80 public void cancelGScan() { 81 mAlarmManager.cancel(mAlarmIntent); 82 if (mGScanTask != null) mGScanTask.cancel(true); 83 } 84 85 class GScanTask extends AsyncTask<ScanSettings, Integer, String> { 86 WifiScanListener mWifiScanListener; 87 Boolean mScanCompleted = false; 88 GScanTask()89 GScanTask() { 90 mWifiScanListener = new WifiScanListener(); 91 } 92 93 @Override doInBackground(ScanSettings... settings)94 protected String doInBackground(ScanSettings... settings) { 95 //android.os.Debug.waitForDebugger(); 96 Log.d(PMCMainActivity.TAG, "Starting background task for gscan with channel"); 97 int waitCount = 0; 98 try { 99 mScanCompleted = false; 100 mScan.startBackgroundScan(settings[0], mWifiScanListener); 101 while (!mScanCompleted) { 102 if (waitCount >= 100) { 103 return "Timeout, scan results avaiable action didn't triggered"; 104 } else { 105 Thread.sleep(100); 106 waitCount += 1; 107 } 108 } 109 mScanCount += 1; 110 waitCount = 0; 111 Log.d(PMCMainActivity.TAG, "Number of scan completed " + mScanCount); 112 publishProgress(mScanCount); 113 } catch (Exception e) { 114 Log.e(PMCMainActivity.TAG, e.toString()); 115 return e.toString(); 116 } finally { 117 mScan.stopBackgroundScan(mWifiScanListener); 118 } 119 return null; 120 } 121 122 @Override onCancelled(String result)123 protected void onCancelled(String result) { 124 mWakeLock.release(); 125 } 126 127 @Override onProgressUpdate(Integer... values)128 protected void onProgressUpdate(Integer... values) { 129 Log.d(PMCMainActivity.TAG, "GScanTask onProgressUpdate updating the UI"); 130 mPMCMainActivity.updateProgressStatus("Total Gscan completed :: " 131 + Integer.toString(values[0].intValue())); 132 } 133 134 @Override onPostExecute(String error)135 protected void onPostExecute(String error) { 136 if (error != null) { 137 Log.e(PMCMainActivity.TAG, error); 138 mPMCMainActivity.updateProgressStatus(error); 139 } 140 mWakeLock.release(); 141 } 142 143 private class WifiScanListener implements WifiScanner.ScanListener { WifiScanListener()144 WifiScanListener() { 145 146 } 147 148 @Override onSuccess()149 public void onSuccess() { 150 Log.d(PMCMainActivity.TAG, "onSuccess called"); 151 } 152 153 @Override onFailure(int reason, String description)154 public void onFailure(int reason, String description) { 155 Log.d(PMCMainActivity.TAG, "onFailure called"); 156 } 157 158 @Override onPeriodChanged(int periodInMs)159 public void onPeriodChanged(int periodInMs) { 160 Log.d(PMCMainActivity.TAG, "onPeriodChanged called"); 161 } 162 163 @Override onFullResult(ScanResult fullScanResult)164 public void onFullResult(ScanResult fullScanResult) { 165 Log.d(PMCMainActivity.TAG, "onFullResult called"); 166 } 167 168 @Override onResults(ScanData[] results)169 public void onResults(ScanData[] results) { 170 Log.d(PMCMainActivity.TAG, "onResult WifiScanListener called"); 171 mScanCompleted = true; 172 } 173 } 174 } 175 } 176