Search code examples
androidrunnablewifi-direct

Google's Direct WiFi Demo remove discovery button


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


Solution

  • I would ditch the code, and re-design the class for doing the discovery. Basically what you need to do, is to implement broadcast receiver to know when different things happen, for example:

    • WIFI_P2P_PEERS_CHANGED_ACTION tells you that peers discovery information for peers around you have changed
    • WIFI_P2P_DISCOVERY_CHANGED_ACTION tells you that peer discovery state has changed

    So if you want to discover just peers, then in start just call discoverPeers, and once the WIFI_P2P_DISCOVERY_CHANGED_ACTION is called, see if its off, and call discoverPeers again.

    And once you get WIFI_P2P_PEERS_CHANGED_ACTION , just call requestPeers to get the peers list.

    Then of course if you also want to discover Services on those peers, you would need to modify the logic a bit, anyway, my WifiServiceSearcher.java sample code illustrates the logic pretty nicely.