How to get direct wifi device name from WifiP2pDeviceList

I want to get the wi-fi name right when making requests, here is my code:

if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

    Log.d(tag, "success discover the peers ");

    if (mManager != null) {
        mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {

            @Override
            public void onPeersAvailable(WifiP2pDeviceList peers) {
                // TODO Auto-generated method stub

                if (peers != null) {
                    if (device.deviceName.equals("ABC")) {
                        Log.d(tag, "found device!!! ");

                        Toast.makeText(getApplicationContext(), "FOUND!!", Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });
    } else {
        Log.d(tag, "mMaganger == null");
    }
}

      

I want to get the deviceName from the peer list so that I can find the one called ABC. Any idea?

+3


source to share


2 answers


If you want a different device name:

wifiP2pManager.requestPeers(wifiChannel, new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {

            for (WifiP2pDevice device : wifiP2pDeviceList.getDeviceList())
            {
                if (device.deviceName.equals("ABC")) Log.d(tag, "found device!!! ");
                // device.deviceName
            }
        }
    });

      

If you want your device name to get it in the receiver:



if (action.equals(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION))
{
    WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
    // device.deviceName
}

      

If you want to change the device name:

try {
    Method method = wifiP2pManager.getClass().getMethod("setDeviceName",
        WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class);

    method.invoke(wifiP2pManager, wifiChannel, "New Device Name", new WifiP2pManager.ActionListener() {
        public void onSuccess() {}

        public void onFailure(int reason) {}
    });
} catch (Exception e)   {}

      

+2


source


You have an object for WifiP2pDeviceList

(peers)

Call a getDeviceList()

peer method that returns a collection (list) of P2p devicesCollection<WifiP2pDevice>

Then we iterate over the element of the collection that is WifiP2pDevice

, and it will contain the property deviceName

that you exactly need.



Refer this training from Android Developers.

Hope you can get it

+2


source







All Articles