I need to use for my app WiFi Direct and I found Google's Demo but it needs many, many changes. The first problem that I have encounter is that I cannot make the app auto scan for devices because there are fragments all over the place and every time I try to create an infinite loop with Runnable and place that loop into the second Thread so the UI Thread can be free to run it crashes. Can you point out my mistakes and help me remove the scan button?
The code that I believe involves the button is the below:
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;
And here as well:
public void onInitiateDiscovery() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel", "finding peers", true,
true, new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}
});
}
I have tried many things but I think I am really close with something like this:
Runnable myRunnable = new Runnable() {
@Override
public void run() {
while (testByte == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
myRunnable = new Runnable() {
@Override
public void run() {
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();
}
});
}
});
}
}
and then I add:
Thread myThread = new Thread(myRunnable);
myThread.start();
I am sorry for the code because it's posted really messy and for the long post as well but I wanted to be clear