User Learning
User Learning

Reputation: 3473

Wifip2p discover peers not working

I saw the code Here. how does the public void displayPeers(final WifiP2pDeviceList peers) works here. When i run in device it displays in devices in listview , but how it gets device list without initializ method ie. displaypeers() without calling?

After that i have changed the code for my project, Displaypeers method is not working / calling . but when i import it full project it works . How is it working ? please help me . If i made mistakes, can anyone tell me how to call that method ? Displaypeers(final WifiP2pDeviseList peers) in the oncreate function itself. Thanks . Sorry for my bad English.

 public void searchForPeers(View view) {

    //Discover peers, no call back method given
    wifiManager.discoverPeers(wifichannel, null);

}

.... ....

public void displayPeers(final WifiP2pDeviceList peers)
{
    //Dialog to show errors/status
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("WiFi Direct File Transfer");

    //Get list view
    ListView peerView = (ListView) findViewById(R.id.peers_listview);

    //Make array list
    ArrayList<String> peersStringArrayList = new ArrayList<String>();

    //Fill array list with strings of peer names
    for(WifiP2pDevice wd : peers.getDeviceList())
    {
        peersStringArrayList.add(wd.deviceName);
    }

    //Set list view as clickable
    peerView.setClickable(true);

    //Make adapter to connect peer data to list view
    ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, peersStringArrayList.toArray());                

    //Show peer data in listview
    peerView.setAdapter(arrayAdapter);


    peerView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {

            //Get string from textview
            TextView tv = (TextView) view;

            WifiP2pDevice device = null;

            //Search all known peers for matching name
            for(WifiP2pDevice wd : peers.getDeviceList())
            {
                if(wd.deviceName.equals(tv.getText()))
                    device = wd;                        
            }

            if(device != null)
            {
                //Connect to selected peer
                connectToPeer(device);

            }
            else
            {
                dialog.setMessage("Failed");
                dialog.show();

            }                           
        }           
            // TODO Auto-generated method stub              
        });

}

EDIT: Oncreate method

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);
    // getActionBar().setDisplayHomeAsUpEnabled(true);

    wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);

    wifichannel = wifiManager.initialize(this, getMainLooper(), null);
    wifiClientReceiver = new WiFiClientBroadcastReceiver(wifiManager, wifichannel, this);

    wifiClientReceiverIntentFilter = new IntentFilter();
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    wifiClientReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    connectedAndReadyToSendFile = false;
    filePathProvided = false;
    fileToSend = null;
    transferActive = false;
    clientServiceIntent = null;
    targetDevice = null;
    wifiInfo = null;

    registerReceiver(wifiClientReceiver, wifiClientReceiverIntentFilter);

    setClientFileTransferStatus("Client is currently idle");

    //setTargetFileStatus("testing");
}

Upvotes: 0

Views: 1303

Answers (1)

iheanyi
iheanyi

Reputation: 3113

You should look at the entire project. There are more classes than just ClientActivity. They all interact to form a working application.

displayPeers() is called from the WiFiClientBroadcastReceiver. I suggest you look through ALL the files here:

https://github.com/mayfourth/WiFi-Direct-File-Transfer/tree/master/WiFiDirectFileTransfer/src/edu/pdx/cs410/wifi/direct/file/transfer

Upvotes: 1

Related Questions