0

I am following https://android.googlesource.com/platform/development/+/master/samples/WiFiDirectDemo.

Due to my own requirements ,I just want to run this following code multiple times. enter image description here

When we click on the search button (cicled in red),The app starts searching for the available wifi peers as shown in the below image

enter image description here

I just want to automate this process every 2 seconds whether it has found some peer or not.

In the code of this Activity:

                case R.id.atn_direct_discover:
            if (!isWifiP2pEnabled) {
                Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning,
                        Toast.LENGTH_SHORT).show();
                return true;
            }
            final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
                    .findFragmentById(R.id.frag_list);
            fragment.onInitiateDiscovery();
            manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {

                @Override
                public void onSuccess() {
                    Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated",
                            Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(int reasonCode) {
                    Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode,
                            Toast.LENGTH_SHORT).show();
                }
            });
            return true;

This is the code.

One thing I tried is put everthing in a while(true) loop but i always crash the app. Then I used a stop button and a flag.This button sets flag to false but it still is not working.

I tried the solution of this :How to automatically Click a Button in Android after a 5 second delay It does not crash the app but the button only works when i manually click it.

Any suggestions Please ??

somerandomguy
  • 323
  • 4
  • 13

2 Answers2

0

What I understood from your problem is you want the task to run without any user intervention to detect new peers and update your UI accordingly.

Run a Handler every 5 seconds when your app is in foreground to start a bound service for searching any new peer, the service will communicate the information back to the activity if it has found anything new or not. When you get the message on your Activity, stop the service and unbind it.

Please see the details about bound service here - https://developer.android.com/guide/components/bound-services.html

Asutosh Panda
  • 1,463
  • 2
  • 13
  • 25
0

You can use Timer class & schedule it to run every 5 seconds. The idea is the timer task doesn't simulate button pressed, but it should does exactly same thing as when the button is pressed.

Assume your Activity class is named MyActivity. Place this in onCreate() of MyActivity class.

Timer myTimer = new Timer();

// schedule to run every 5000 milli seconds
myTimer.schedule(new TimerTask() {          
    @Override
    public void run() {

        MyActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
               // any code that update UI goes here
               // Eg: displaying progress indicator, show "finding Peers" text
               ....
            }
        });

        // the rest of "discover" logic goes here

    }

}, 0, 5000);
Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42