1

I need to locate my current location in a building by scanning the strongest available Wifi to launch an activity. I want to locate only 3 places in that building. So if I am currently near any of those locations, I will click a button to scan the strongest available Wifi to launch an activity.

  1. I need to scan available Wifi in a building.
  2. Get the 3 strongest signal.
  3. Then detect whether any of those 3 strongest signal's SSID is the same with the specific SSID that I want to locate.
  4. If my SSID is one of the strongest 3 signals, then the application will launch a xml layout activity,

So far I already have the coding to scan strongest available Wifi. But I don't know how to launch an activity when the strongest specific Wifi SSID detected. I do not need the information about the Wifi, just to launch my activity layout that has my information.

At the moment I'm using the following bit of code to scan the strongest Wifi signal that I got from here:

WiFiDemo.java

public class WiFiDemo extends Activity implements OnClickListener {
private static final String TAG = "WiFiDemo";
WifiManager wifi;
BroadcastReceiver receiver;

TextView textStatus;
Button buttonScan;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Setup UI
    textStatus = (TextView) findViewById(R.id.textStatus);
    buttonScan = (Button) findViewById(R.id.buttonScan);
    buttonScan.setOnClickListener(this);

    // Setup WiFi
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    // Get WiFi status
    WifiInfo info = wifi.getConnectionInfo();
    textStatus.append("\n\nWiFi Status: " + info.toString());

    // List available networks
    List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
    for (WifiConfiguration config : configs) {
        textStatus.append("\n\n" + config.toString());
    }

    // Register Broadcast Receiver
    if (receiver == null)
        receiver = new WiFiScanReceiver(this);

    registerReceiver(receiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    Log.d(TAG, "onCreate()");
}

@Override
public void onStop() {
    unregisterReceiver(receiver);
}

public void onClick(View view) {
    Toast.makeText(this, "On Click Clicked. Toast to that!!!",
            Toast.LENGTH_LONG).show();

    if (view.getId() == R.id.buttonScan) {
        Log.d(TAG, "onClick() wifi.startScan()");
        wifi.startScan();
    }
} }

WiFiScanReceiver.java

      public class WiFiScanReceiver extends BroadcastReceiver {
        private static final String TAG = "WiFiScanReceiver";
        WiFiDemo wifiDemo;

      public WiFiScanReceiver(WiFiDemo wifiDemo) {
        super();
        this.wifiDemo = wifiDemo;
      }

      @Override
      public void onReceive(Context c, Intent intent) {
        List<ScanResult> results = wifiDemo.wifi.getScanResults();
        ScanResult bestSignal = null;
        for (ScanResult result : results) {
          if (bestSignal == null
              || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
            bestSignal = result;
        }

        String message = String.format("%s networks found. %s is the strongest.",
            results.size(), bestSignal.SSID);
        Toast.makeText(wifiDemo, message, Toast.LENGTH_LONG).show();

        Log.d(TAG, "onReceive() message: " + message);
      }

    }
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
fadzlinaz
  • 11
  • 1
  • 2
  • Isn't the strongest available wifi detected the same as the stongest wifi ssid detected??? In other words, isn't your solution the right one already? – Stephan Branczyk Dec 17 '12 at 18:55
  • Yes. But I want to detect whether i am currently near the specific wifi access point to locate my location. So i need to detect the strongest wifi SSID is the same as my own specific wifi SSID. – fadzlinaz Dec 17 '12 at 19:10

1 Answers1

0

You just need to start your activity from onReceive. Unless I'm not understanding your question, you just need to fire off your activity. Since you have the context on your receiver's onReceive you just need to startActivity()

Start Activity inside onReceive BroadcastReceiver

 @Override
    public void onReceive(Context context, Intent intent) {
        //start activity
        Intent i = new Intent();
        i.setClassName("com.test", "com.test.MainActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

Don't forget to define a proper filter for it in your manifest. http://developer.android.com/reference/android/content/BroadcastReceiver.html

Community
  • 1
  • 1
Nelson Ramirez
  • 7,864
  • 7
  • 28
  • 34