WifiScanner

Quickly share your Android Code Snippets here...

WifiScanner

Postby nas061000 » Mon Dec 22, 2008 3:15 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.android.wifitester;
  2.  
  3.  
  4.  
  5. import java.util.List;
  6.  
  7. import android.app.Activity;
  8.  
  9. import android.content.BroadcastReceiver;
  10.  
  11. import android.content.Context;
  12.  
  13. import android.content.Intent;
  14.  
  15. import android.content.IntentFilter;
  16.  
  17. import android.net.wifi.ScanResult;
  18.  
  19. import android.net.wifi.WifiManager;
  20.  
  21. import android.os.Bundle;
  22.  
  23. import android.view.Menu;
  24.  
  25. import android.view.MenuItem;
  26.  
  27. import android.widget.TextView;
  28.  
  29.  
  30.  
  31. public class WifiTester extends Activity {
  32.  
  33.         TextView mainText;
  34.  
  35.         WifiManager mainWifi;
  36.  
  37.         WifiReceiver receiverWifi;
  38.  
  39.         List<ScanResult> wifiList;
  40.  
  41.         StringBuilder sb = new StringBuilder();
  42.  
  43.        
  44.  
  45.     public void onCreate(Bundle savedInstanceState) {
  46.  
  47.         super.onCreate(savedInstanceState);
  48.  
  49.         setContentView(R.layout.main);
  50.  
  51.         mainText = (TextView) findViewById(R.id.mainText);
  52.  
  53.         mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  54.  
  55.        
  56.  
  57.         receiverWifi = new WifiReceiver();
  58.  
  59.         registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  60.  
  61.         mainWifi.startScan();
  62.  
  63.         mainText.setText("\nStarting Scan...\n");
  64.  
  65.     }
  66.  
  67.  
  68.  
  69.         public boolean onCreateOptionsMenu(Menu menu) {
  70.  
  71.                 menu.add(0, 0, 0, "Refresh");
  72.  
  73.                 return super.onCreateOptionsMenu(menu);
  74.  
  75.         }
  76.  
  77.  
  78.  
  79.         public boolean onMenuItemSelected(int featureId, MenuItem item) {
  80.  
  81.                 mainWifi.startScan();
  82.  
  83.                 mainText.setText("Starting Scan");
  84.  
  85.                 return super.onMenuItemSelected(featureId, item);
  86.  
  87.         }
  88.  
  89.  
  90.  
  91.         protected void onPause() {
  92.  
  93.                 unregisterReceiver(receiverWifi);
  94.  
  95.                 super.onPause();
  96.  
  97.         }
  98.  
  99.  
  100.  
  101.         protected void onResume() {
  102.  
  103.                 registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  104.  
  105.                 super.onResume();
  106.  
  107.         }
  108.  
  109.        
  110.  
  111.         class WifiReceiver extends BroadcastReceiver {
  112.  
  113.                 public void onReceive(Context c, Intent intent) {
  114.  
  115.                         sb = new StringBuilder();
  116.  
  117.                         wifiList = mainWifi.getScanResults();
  118.  
  119.                 for(int i = 0; i < wifiList.size(); i++){
  120.  
  121.                         sb.append(new Integer(i+1).toString() + ".");
  122.  
  123.                         sb.append((wifiList.get(i)).toString());
  124.  
  125.                         sb.append("\n");
  126.  
  127.                 }
  128.  
  129.                 mainText.setText(sb);
  130.  
  131.                 }
  132.  
  133.         }
  134.  
  135.  
  136.  
  137. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Though I'd post this. It's a simple program that'll Scan Wifi in the area. I think I have it right, it works, but if there is room for improvement, comment please...
nas061000
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Dec 13, 2008 11:00 pm

Top

Postby nas061000 » Tue Dec 23, 2008 10:32 pm

Random Question, but I've tried recreating this with a XML Receiver(in the Manifest) and It doesn't seem to work, can anyone give me an example of it(if I used an externel BroadcastReceiver class).
nas061000
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Dec 13, 2008 11:00 pm

Postby cyberrob » Tue Feb 10, 2009 4:06 am

I try your code, but Got a error msg: The application wifiTest has Stopped unexpectedly.Plz try again.

And a Force Quit button..

Dont know why it happened..some say we cant test any wifi app on emulator,right?

Sorry to something naive :oops:
User avatar
cyberrob
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Dec 13, 2007 3:01 am

Postby nas061000 » Tue Feb 17, 2009 9:07 pm

Make sure you have permissions set. The program will work in the emulator, but it won't return results, there is no reason it should be crashing.
nas061000
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Dec 13, 2008 11:00 pm

Postby cyberrob » Wed Feb 18, 2009 4:04 am

nas061000 wrote:Make sure you have permissions set. The program will work in the emulator, but it won't return results, there is no reason it should be crashing.


the program didnt work in the emulator also, after I add some user permissions like

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

Is there any other permissions needed to add? or something I still missed.
User avatar
cyberrob
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Dec 13, 2007 3:01 am

Postby nas061000 » Wed Feb 18, 2009 4:08 am

It should get stuck at the Starting Scan Phase because it's waiting for the event to fire back from wifi, but since the emulator can't get wifi info, it'll be stuck there... now if it is crashing, that's another thing. Make sure the check logcat.
nas061000
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Dec 13, 2008 11:00 pm

Top

Postby cyberrob » Tue Feb 24, 2009 3:57 pm

After I set permission like this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  2. <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"></uses-permission>
  3. <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
  4. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  5. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  6. <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


While debugging, logcat tell me that I didnt set android.permission.ACCESS_WIFI_STATE...

02-24 14:29:11.753: WARN/ActivityManager(57): Permission Denial: starting Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={org.itri.android/org.itri.android.wifiTester} } from ProcessRecord{4354bce0 153:android.process.acore/10003} (pid=153, uid=10003) requires android.permission.ACCESS_WIFI_STATE

WHY IS THAT??
User avatar
cyberrob
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Dec 13, 2007 3:01 am

Postby nas061000 » Tue Feb 24, 2009 5:11 pm

IT needs to be able to check whether the WiFi is up or down, IE the state.
nas061000
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Dec 13, 2008 11:00 pm

Postby hoodie » Wed Apr 01, 2009 12:07 pm

cyberrob wrote:While debugging, logcat tell me that I didnt set android.permission.ACCESS_WIFI_STATE...

02-24 14:29:11.753: WARN/ActivityManager(57): Permission Denial: starting Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={org.itri.android/org.itri.android.wifiTester} } from ProcessRecord{4354bce0 153:android.process.acore/10003} (pid=153, uid=10003) requires android.permission.ACCESS_WIFI_STATE

WHY IS THAT??


You may have put the permissions inside the <application></application>, rather than just after it - I got the same error. Instead of
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. <manifest>
  3.         <application abc>
  4.                 <activity xyz>
  5.                         ...
  6.                 </activity>
  7.                 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  8.                 ...
  9.         <application>
  10. </manifest>
  11.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

try
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. <manifest>
  3.         <application abc>
  4.                 <activity xyz>
  5.                         ...
  6.                 </activity>
  7.         </application>
  8.         <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  9.         ...
  10. </manifest>
  11.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Hope this helps.
H
hoodie
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Apr 01, 2009 11:55 am

Postby abhishekrs » Tue Aug 11, 2009 4:15 pm

Hi,

I tried this code on the newest SDK 1.5r3 but it looks like it doesn't recognize WifiReceiver (error: 'WifiReceiver cannot be resolved to a type')

Is it not a part of cupcake? I don't see it in the list of Wifi classes in the documentation. In that case, how am I supposed to make your code work for cupcake?

Thanks,

Abhi
abhishekrs
Freshman
Freshman
 
Posts: 3
Joined: Tue Aug 11, 2009 4:10 pm

Postby abhishekrs » Wed Aug 12, 2009 2:15 pm

Hey,

I just realized I didn't have all the parts in the code right... just got it rectified.

But now, when I try running it on the emulator, the app force closes with the following on the Eclipse console:

[2009-08-12 08:54:30 - WiFiProject] Android Launch!
[2009-08-12 08:54:30 - WiFiProject] adb is running normally.
[2009-08-12 08:54:30 - WiFiProject] Performing com.example.WiFiProject.WiFiProject activity launch
[2009-08-12 08:54:30 - WiFiProject] Automatic Target Mode: launching new emulator with compatible AVD 'Android'
[2009-08-12 08:54:30 - WiFiProject] Launching a new emulator with Virtual Device 'Android'
[2009-08-12 08:54:36 - WiFiProject] New emulator found: emulator-5554
[2009-08-12 08:54:36 - WiFiProject] Waiting for HOME ('android.process.acore') to be launched...
[2009-08-12 08:56:34 - WiFiProject] HOME is up on device 'emulator-5554'
[2009-08-12 08:56:34 - WiFiProject] Uploading WiFiProject.apk onto device 'emulator-5554'
[2009-08-12 08:56:34 - WiFiProject] Installing WiFiProject.apk...
[2009-08-12 08:56:44 - WiFiProject] Application already exists. Attempting to re-install instead...
[2009-08-12 08:56:48 - WiFiProject] Success!
[2009-08-12 08:56:48 - WiFiProject] Starting activity com.example.WiFiProject.WiFiProject on device
[2009-08-12 08:56:50 - WiFiProject] ActivityManager: Starting: Intent { comp={com.example.WiFiProject/com.example.WiFiProject.WiFiProject} }

Please help me out with this.

Thanks,

Abhi
abhishekrs
Freshman
Freshman
 
Posts: 3
Joined: Tue Aug 11, 2009 4:10 pm

Re: WifiScanner

Postby xsway » Thu Dec 10, 2009 1:17 pm

Hi guys!

I want to implement the continuous wifi scanning. With this code it gives you only one snapshot. Has anyone done this? i tried to make it somehow but it didn't work. Is there any trick?

Thanks
xsway
Freshman
Freshman
 
Posts: 2
Joined: Thu Dec 10, 2009 1:15 pm

Postby Draffodx » Mon Jan 04, 2010 3:16 pm

Runs fine for me, make sure you have the correct permissions in the manifest

The startscan method should call the scan periodically until stopscan is called I think
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

How can I get the specific infos?

Postby Hill » Wed May 05, 2010 8:03 pm

Hi everybody,

I tried this code and it works on my phone. Now I want to modify the code:

I want to illustrate somehow all (or at least the first 10 or 20 for example) access points graphically with circles. For example the ones with the biggest signal strength will be drawn on a circle which is near the center. (the center represents the phone/user itself). The weaker the signal strength of an access point is the further away from the center will it be.

I have to check dynamically in a loop what the signal strength of a point is. But how exactly can I get the info? Where is the name and the signal strength saved exactly?

When I look at this part of the code:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. class WifiReceiver extends BroadcastReceiver {
  2.  
  3.           public void onReceive(Context c, Intent intent) {
  4.  
  5.                sb = new StringBuilder();
  6.  
  7.                wifiList = mainWifi.getScanResults();
  8.  
  9.              for(int i = 0; i < wifiList.size(); i++){
  10.  
  11.                sb.append(new Integer(i+1).toString() + ".");
  12.  
  13.                sb.append((wifiList.get(i)).toString());
  14.  
  15.                sb.append("\n");
  16.  
  17.                
  18.  
  19.                
  20.  
  21.              }
  22.  
  23.              mainText.setText(sb);
  24.  
  25.              
  26.  
  27.              
  28.  
  29.           }
  30.  
  31.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


where are these informations? How can I use them and compare? For exampe:

if(blablabla.signal_strength > 1.....)
{

}

else ......

I hope somebody can help.

thanks
Hill
Once Poster
Once Poster
 
Posts: 1
Joined: Wed May 05, 2010 5:42 pm

Re: WifiScanner

Postby sudsey999 » Wed Oct 13, 2010 6:23 pm

Hi I've tried using this code but I keep getting the error "R.id cannot be resolved", why is that :(


sudsey

edit: problem resolved but after the emulator has been running for a minute or so this message appears in Eclipse:
-10-13 20:42:29 - WifiTester] emulator-5554 disconnected! Cancelling 'com.android.wifitester.WifiTester activity launch'!

What's happening there?

sudsey
sudsey999
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 13, 2010 6:20 pm

Top
Next

Return to Code Snippets for Android

Who is online

Users browsing this forum: Google [Bot] and 2 guests