forked from reuniware/Android-DvxWifiScan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
775 lines (645 loc) · 27.7 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/**
* Investdata / Reuniware - email: [email protected]
* Developed by Investdata Systems
*/
package eu.iledelareunion.www.dvxwifiscan;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends ActionBarActivity {
WifiManager myWifiManager = null;
private DvxWifiScanDbHelper dbHelper;
public void dropTable() {
SQLiteDatabase dbw = dbHelper.getWritableDatabase();
dbw.execSQL("drop table if exists accesspoint", new String[]{});
dbw.close();
}
public void deleteAllRecords(){
SQLiteDatabase dbw = dbHelper.getWritableDatabase();
//dbw.execSQL("delete from accesspoint", new String[]{});
dbw.delete("accesspoint", "", new String[]{});
dbw.close();
}
public void insertRecord(String ssid, String bssid, String level, String freq, String caps) {
SQLiteDatabase dbw = dbHelper.getWritableDatabase();
dbw.execSQL("insert into accesspoint (ssid, bssid, level, freq, caps) values (?,?,?,?,?)", new String[]{ssid, bssid, level, freq, caps});
dbw.close();
}
public void updateRecord(String ssid, String bssid, String level, String freq, String caps) {
SQLiteDatabase dbw = dbHelper.getWritableDatabase();
dbw.execSQL("update accesspoint set ssid = ?, level = ?, freq = ?, caps = ? where bssid = ?", new String[]{ssid, level, bssid, freq, caps});
dbw.close();
}
public void getAllRecords() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = db.rawQuery("select * from accesspoint", new String[]{});
//log("nb rec in db = " + c.getCount());
for(int i=0;i<c.getCount();i++) {
c.moveToNext();
log("DB:" + c.getString(0) + " " + c.getString(1) + " " + c.getString(2) + " " + c.getString(3));
}
c.close();
db.close();
}
public int getRecordsCount(){
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = db.rawQuery("select * from accesspoint", new String[]{});
int nb = c.getCount();
c.close();
return nb;
}
public boolean bssidExistsInDb(String bssid){
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = db.rawQuery("select * from accesspoint where bssid=?", new String[]{bssid});
if (c.getCount()>0){
c.close();
db.close();
return true;
}
else {
c.close();
db.close();
return false;
}
}
private long scanPeriod = 2500; // toutes les 1 seconde
private long timetowaitBetweenEachScan = 2500;
private int nbLastSsidShown = 8; // nombre de ssid affichés (tous les ssid sont stockés dans une arrayListScanResultSsid)
Timer timerLoopScan = null;
private boolean clearScanLogFileWhenLoopScanLaunched = false;
private boolean clearDbUponStartup = false;
public void loopScanLauncher() {
if (timerLoopScan != null) {
showToastMessage("Loop Scan already started. Stopping it.");
timerLoopScan.cancel();
timerLoopScan.purge();
timerLoopScan = null;
if ((myWifiManager != null) && (myWifiManager.isWifiEnabled()))
myWifiManager.reconnect();
log("There are " + getRecordsCount() + " records in db");
//getAllRecords();
/*if (gpsProcessingIsWellStarted) {
stopGpsProcessing();
}*/
return;
}
//clearMainLogFile();
//gpsProcessingIsWellStarted = startGpsProcessing();
log("New scan started at " + getTimeStamp());
logScan("New scan started at " + getTimeStamp());
if (clearScanLogFileWhenLoopScanLaunched) clearScanLogFile();
showToastMessage("Scan log file = " + getScanLogFilePath());
if (clearDbUponStartup) {
deleteAllRecords();
}
if (timerLoopScan != null) {
timerLoopScan.cancel();
timerLoopScan.purge();
timerLoopScan = null;
}
timerLoopScan = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
//clearScreen();
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.post(new Runnable() {
@Override
public void run() {
loopScan();
}
});
findViewById(R.id.scrollView).post(new Runnable() {
public void run() {
((ScrollView) findViewById(R.id.scrollView)).fullScroll(View.FOCUS_DOWN);
}
});
}
};
timerLoopScan.scheduleAtFixedRate(timerTask, 0, scanPeriod);
}
//--
ArrayList<String> arrayListScanResultSsid = new ArrayList<String>();
ArrayList<String> arrayListScanResultBssid = new ArrayList<String>();
ArrayList<String> arrayListLevel = new ArrayList<String>();
ArrayList<String> arrayListFreq = new ArrayList<String>();
ArrayList<String> arrayListCaps = new ArrayList<String>();
ArrayList<Location> arrayListLocation = new ArrayList<Location>();
boolean forceDisconnectFromKnownNetwork = false;
public void loopScan() {
//Scan en boucle
if (myWifiManager.isWifiEnabled()) {
//clearScreen();
if ((myWifiManager != null) && (myWifiManager.isWifiEnabled())) {
try {
//log("Scanning every " + (scanPeriod + timetowaitBetweenEachScan) + "ms. Last " + nbLastSsidShown + " shown.");
myWifiManager.startScan();
List<ScanResult> lstScanResult = myWifiManager.getScanResults();
ArrayList<Integer> arrayListIndexOfSsidToLogToScanLogFile = new ArrayList<Integer>();
if (lstScanResult != null) {
for (int i = 0; i < lstScanResult.size(); i++) {
String strSsid = lstScanResult.get(i).SSID;
String strBssid = lstScanResult.get(i).BSSID;
String strLevel = "" + lstScanResult.get(i).level;
String strFrequency = "" + lstScanResult.get(i).frequency;
String strCaps = lstScanResult.get(i).capabilities;
if (!arrayListScanResultBssid.contains(strBssid)) { // la clé unique est le bssid (=mac address?)
if (!arrayListScanResultSsid.contains(strSsid)) {
arrayListScanResultSsid.add(strSsid);
if (!strBssid.isEmpty()) {
arrayListScanResultBssid.add(strBssid);
} else {
arrayListScanResultBssid.add("n/a");
}
if (!strLevel.isEmpty()) {
arrayListLevel.add(strLevel);
} else {
arrayListLevel.add("n/a");
}
if (!strFrequency.isEmpty()) {
arrayListFreq.add(strFrequency);
} else {
arrayListFreq.add("n/a");
}
if (!strCaps.isEmpty()) {
arrayListCaps.add(strCaps);
} else {
arrayListCaps.add("n/a");
}
int indexOfLastArrayListScanResultSsid = arrayListScanResultSsid.size() - 1;
//logDebug("New SSID/BSSID scanned = " + arrayListScanResultSsid.get(indexOfLastArrayListScanResultSsid) + " ; " + arrayListScanResultBssid.get(indexOfLastArrayListScanResultSsid));
//logDebug("--> this SSID/BSSID will be added to ScanLogFile");
arrayListIndexOfSsidToLogToScanLogFile.add(indexOfLastArrayListScanResultSsid);
}
}
}
}
// Logguer dans ScanLogFile si nécessaire
for (int i = 0; i < arrayListIndexOfSsidToLogToScanLogFile.size(); i++) {
int indexOfSsidToLog = arrayListIndexOfSsidToLogToScanLogFile.get(i);
String ssid = arrayListScanResultSsid.get(indexOfSsidToLog);
String bssid = arrayListScanResultBssid.get(indexOfSsidToLog);
String level = arrayListLevel.get(indexOfSsidToLog);
String freq = arrayListFreq.get(indexOfSsidToLog);
String caps = arrayListCaps.get(indexOfSsidToLog);
logScan(String.format("%03d", indexOfSsidToLog) + " | " + ssid + " | " + bssid.replace(":", "") + " | " + level + "dB" + " | " + freq + "MHz" + " | " + caps);
log(String.format("%03d", indexOfSsidToLog) + " | " + ssid + " | " + bssid.replace(":", "") + " | " + level + "dB" + " | " + freq + "MHz" + " | " + caps);
if(bssidExistsInDb(bssid))
{
updateRecord(ssid, bssid, level, freq, caps);
}
else
{
insertRecord(ssid, bssid, level, freq, caps);
}
}
int arrayListsSize = arrayListScanResultSsid.size();
//log(arrayListScanResultSsid.size() + " networks scanned. " + getTimeStamp());
//myWifiManager.reconnect();
//Thread.sleep(timetowaitBetweenEachScan);
} catch (Exception e) {
log("loopScan:Exception:" + e.getMessage());
}
}
} else {
log("Wifi is not started.");
}
}
public void clearScreen() {
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.post(new Runnable() {
@Override
public void run() {
linearLayout.removeAllViews();
}
});
}
public void showToastMessage(String str) {
Toast toast = new Toast(this);
TextView textView = new TextView(this);
textView.setText(str);
textView.setBackgroundColor(Color.BLACK);
textView.setTextColor(Color.WHITE);
toast.setView(textView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
public String getTimeStamp() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String s = simpleDateFormat.format(new Date());
return s;
}
public void logDebug(String str) {
Log.d("DvxWifiScan", str);
}
public void clearMainLogFile() {
File file = Environment.getExternalStorageDirectory();
String pathToMntSdCard = file.getPath();
File myFile = new File(pathToMntSdCard + "/" + mainLogFileName);
boolean deleted = false;
if (myFile.exists()) {
deleted = myFile.delete();
} else {
showToastMessage("Main log file does not exist.");
}
if (deleted == true) {
showToastMessage("Main log file has been deleted.");
}
}
public void clearScanLogFile() {
File file = Environment.getExternalStorageDirectory();
String pathToMntSdCard = file.getPath();
File myFile = new File(pathToMntSdCard + "/" + scanLogFileName);
boolean deleted = false;
if (myFile.exists()) {
deleted = myFile.delete();
} else {
showToastMessage("Scan log file does not exist.");
}
if (deleted == true) {
showToastMessage("Scan log file has been deleted.");
}
}
String mainLogFileName = "dvxwifiscan.log";
String scanLogFileName = "dvxwifiscan-scan.log";
public String getMainLogFilePath() {
File file = Environment.getExternalStorageDirectory();
String pathToMntSdCard = file.getPath();
String pathToLogFile = pathToMntSdCard + "/" + mainLogFileName;
return pathToLogFile;
}
public String getScanLogFilePath() {
File file = Environment.getExternalStorageDirectory();
String pathToMntSdCard = file.getPath();
String pathToLogFile = pathToMntSdCard + "/" + scanLogFileName;
return pathToLogFile;
}
private int currentTextColor = Color.GREEN;
/**
* Loggue à l'écran (systématiquement) et dans le fichier wififun.log (si la checkbox Log to File est cochée)
* @param str
*/
public void log(String str) {
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
// Si plus de 1000 lignes dans le log à l'écran alors effacer la zone de log
if (linearLayout.getChildCount() > 1024) {
linearLayout.removeAllViews();
}
final TextView textView = new TextView(MainActivity.this);
textView.setTextColor(currentTextColor);
if (currentTextColor == Color.GREEN) currentTextColor = Color.LTGRAY; else currentTextColor = Color.GREEN;
textView.setTextSize(12);
textView.setText(str);
linearLayout.addView(textView);
/*final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
boolean logToFile = checkBox.isChecked();
if (logToFile == true && timerLoopScan == null) { // ne pas logguer dans wififun.log si le LoopScan est activé (mais loggué ailleurs dans wififun-scan.log)
// loggue dans le fichier /mnt/sdcard/wififun.log ; Utiliser Speed Software File Explorer pour accéder par exemple
try {
File myFile = new File(getMainLogFilePath());
if (!myFile.exists()) {
myFile.createNewFile();
}
FileWriter fileWriter = new FileWriter(myFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(str + "\r\n");
bufferedWriter.close();
fileWriter.close();
} catch (Exception e) {
}
}*/
((ScrollView) findViewById(R.id.scrollView)).post(new Runnable() {
public void run() {
((ScrollView) findViewById(R.id.scrollView)).fullScroll(View.FOCUS_DOWN);
}
});
}
/*
Loggue uniquement dans le fichier wififun-scan.log (objectif = ce fichier fait office de rapport de scan)
*/
public void logScan(String str) {
// loggue dans le fichier /mnt/sdcard/wififun-scan.log ; Utiliser Speed Software File Explorer pour accéder par exemple
try {
File myFile = new File(getScanLogFilePath());
if (!myFile.exists()) {
myFile.createNewFile();
}
FileWriter fileWriter = new FileWriter(myFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(str + "\r\n");
bufferedWriter.close();
fileWriter.close();
} catch (Exception e) {
}
}
boolean enableGpsLogToScreen = true;
public void logGps(String str) {
// loggue dans le fichier /mnt/sdcard/wififun-gps.log ; Utiliser Speed Software File Explorer pour accéder par exemple
if (enableGpsLogToScreen == true) {
log(str);
}
try {
File myFile = new File(getGpsLogFilePath());
if (!myFile.exists()) {
myFile.createNewFile();
}
FileWriter fileWriter = new FileWriter(myFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(str + "\r\n");
bufferedWriter.close();
fileWriter.close();
} catch (Exception e) {
}
}
//--
@Override
protected void onSaveInstanceState(Bundle outState) {
// Sauvegarder chaque contenu texte des textviews dans le linearlayout (pour restauration en cas de rotation par ex)
super.onSaveInstanceState(outState);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
int childCount = linearLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
TextView textView = (TextView) linearLayout.getChildAt(i);
outState.putString("textview" + i, textView.getText().toString());
//Log.d("wififun:saving:",textView.getText().toString());
}
}
//--
public boolean gpsProcessingIsWellStarted = false;
//--
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.setBackgroundColor(Color.BLACK);
// restaurer chaque textview dans le linearlayout ; à priori pas besoin de restaurer l'état de la checkbox
if (savedInstanceState != null) {
for (int i = 0; i < savedInstanceState.size(); i++) {
if (savedInstanceState.containsKey("textview" + i)) {
TextView textView = new TextView(this);
textView.setText(savedInstanceState.getString("textview" + i));
linearLayout.addView(textView);
}
}
}
dbHelper = new DvxWifiScanDbHelper(this.getApplicationContext());
try {
myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
} catch (Exception e) {
log("Exception:" + e.getMessage());
}
/*CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b == true) {
showToastMessage("Log file = " + getMainLogFilePath());
}
}
});*/
Button btnStartWifi = (Button) findViewById(R.id.button);
Button btnStopWifi = (Button) findViewById(R.id.button2);
Button btnQuit = (Button) findViewById(R.id.button3);
Button btnGetIp = (Button) findViewById(R.id.button4);
Button btnScanSsid = (Button) findViewById(R.id.button5);
Button btnClearScreen = (Button) findViewById(R.id.button9);
//Button btnClearLogFile = (Button) findViewById(R.id.button6);
Button btnStartLoopScan = (Button) findViewById(R.id.button8);
//Button btnTest = (Button) findViewById(R.id.button7);
/*btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
test();
}
});*/
btnStartLoopScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loopScanLauncher();
}
});
/*btnClearLogFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clearMainLogFile();
}
});*/
btnClearScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clearScreen();
}
});
btnScanSsid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scan();
}
});
btnStartWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startWifi();
}
});
btnStopWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopWifi();
}
});
btnQuit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exitApplication();
}
});
btnGetIp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getIpAddress();
}
});
}
//--
public void startWifi() {
//log("startwifi");
try {
//WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean wasEnabled = myWifiManager.isWifiEnabled();
if (wasEnabled == false) {
myWifiManager.setWifiEnabled(true);
log("Enabling Wifi...");
while (!myWifiManager.isWifiEnabled()) {
// attendre
}
if (myWifiManager.isWifiEnabled() == true) {
log("Wifi has been started.");
WifiInfo wifiInfo = myWifiManager.getConnectionInfo();
if (wifiInfo != null) {
//log("wifiInfo is not null");
log("MAC=" + wifiInfo.getMacAddress());
} else {
log("Cannot retrieve MAC.");
}
} else {
log("Wifi has not been started.");
}
} else {
log("Wifi is already started.");
}
} catch (Exception e) {
log("Exception:" + e.getMessage());
}
}
public void stopWifi() {
try {
WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean isEnabled = myWifiManager.isWifiEnabled();
if (isEnabled == true) {
myWifiManager.setWifiEnabled(false);
log("Stopping Wifi...");
while (myWifiManager.isWifiEnabled()) {
// attendre
}
if (myWifiManager.isWifiEnabled() == true) {
log("Wifi has not been stopped.");
} else {
myWifiManager = null;
log("Wifi has been stopped.");
}
} else {
log("Wifi is not started.");
}
} catch (Exception e) {
log("Exception:" + e.getMessage());
}
}
public void scan() {
if ((myWifiManager != null) && (myWifiManager.isWifiEnabled())) {
log("**** SCAN SSIDs : START ****");
boolean wasConnectedToSSID = false;
try {
if (!getIpAddr().equals("0.0.0.0")) {
log("Wifi seems connected to SSID : " + myWifiManager.getConnectionInfo().getSSID());
log("Disconnecting...");
boolean disconnected = myWifiManager.disconnect();
if (disconnected == true) {
log("Disconnected.");
}
wasConnectedToSSID = true;
}
log("Starting SSID scan...");
myWifiManager.startScan();
List<ScanResult> lstScanResult = myWifiManager.getScanResults();
if (lstScanResult != null) {
log("----");
for (int i = 0; i < lstScanResult.size(); i++) {
log("SSID=" + lstScanResult.get(i).SSID);
log(" BSSID=" + lstScanResult.get(i).BSSID);
log(" CAPS=" + lstScanResult.get(i).capabilities);
log(" FREQ=" + lstScanResult.get(i).frequency);
log(" LEVEL=" + lstScanResult.get(i).level);
log("----");
}
}
if (lstScanResult != null) {
log(lstScanResult.size() + " Wifi networks detected around.");
}
if (wasConnectedToSSID) {
myWifiManager.reconnect();
}
} catch (Exception e) {
log("Exception:" + e.getMessage());
}
log("**** SCAN SSIDs : END ****");
} else {
log("SSID Scan : Please start wifi.");
}
}
private void getIpAddress() {
if ((myWifiManager != null) && (myWifiManager.isWifiEnabled() == true)) {
try {
if (myWifiManager.isWifiEnabled()) {
log("Get IP Address: IP=" + getIpAddr());
}
} catch (Exception e) {
log("Get IP Address:Exception:" + e.getMessage());
}
} else {
//Log.d("wififun","please enable wifi");
log("Get IP Address:Please start Wifi.");
}
}
public String getIpAddr() {
WifiManager wifiManager = myWifiManager;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipString = String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
return ipString;
}
private void exitApplication() {
/*if (myWifiManager != null) {
try {
myWifiManager.disconnect();
myWifiManager = null;
} catch (Exception e) {
log("Exception:" + e.getMessage());
}
}*/
System.exit(0);
}
//--
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}