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.
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
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 ??