3

I want to get the wi-fi direct name when I execute request peers, 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 list of peers so that I can found that one named "ABC". Any idea?

user2301281
  • 757
  • 2
  • 12
  • 27
  • what do you mean by **wifi-direct** name? You mean the [deviceName](http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pDevice.html#deviceName)!!? coz device name is very straight forward to obtain... – Hariharan Gandhi May 15 '15 at 12:22
  • I edited my qs, I want to find the deviceName from list of peers. But I can't figure out what wrong of my code. I can't get the expected result.. – user2301281 May 15 '15 at 12:38
  • Thanks for editing. I guessed so. Could you paste some more of your code to get exactly you are going wrong in getting the name ? I mean the part where you are trying to get the device name... – Hariharan Gandhi May 15 '15 at 12:43

2 Answers2

2

If you want others 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 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 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)   {}
Ali
  • 21,572
  • 15
  • 83
  • 95
  • I think I had confused to get the own deviceName & other devices Name. Thanks for point it out. – user2301281 May 15 '15 at 12:56
  • @Ali very clearly explained. i just didn't want to give away code without him trying it, as it was easy to find :P :P cheers – Hariharan Gandhi May 15 '15 at 12:59
  • @user2301281 you're welcome. Please mark the answer if your satisfied. Thank you – Ali May 15 '15 at 13:00
  • @user2301281 I really suggest looking at WiFi Direct demo in android SDK. – Ali May 15 '15 at 13:02
  • @Ali, yes. I already looking & try the WifiDirectDemo in sample before. But inside the broadcast sample don't have the "onPeersAvailable()" part. So I can't get the idea. Sorry for my poor coding skills. & very thank you for clear the idea. =D – user2301281 May 15 '15 at 13:08
  • @user2301281 There is no need to perform to perform requestPeers or other WiFiP2P methods inside the receiver, you can just do it at the beginning of your activity or fragment. – Ali May 15 '15 at 13:21
  • @Ali, u mean without using requestPeers I can direct execute deviceName.equals("ABC")?? – user2301281 May 15 '15 at 13:30
  • @user2301281 No, it's clear that you don't have the peers if you don't request them first. My point is you can request peers anywhere, not just inside the wifi receiver. – Ali May 15 '15 at 14:11
  • @Ali, noted. Thanks for pointed out so many concept of wifi direct. It's really help me. =D – user2301281 May 15 '15 at 14:21
2

You have the object for WifiP2pDeviceList(peers)

Call the method getDeviceList() on peers which returns a collection(list) of P2p devices Collection<WifiP2pDevice>

Then iterate through the collection element which is a WifiP2pDevice and it would contain the deviceName property which is what you need exactly.

Refer this training from android developers

Hope you are able to get it

Hariharan Gandhi
  • 1,174
  • 9
  • 19
  • I can't really get the idea of "iterate through the collection element". Can you explain more detail? Sorry, i'm newbie. > – user2301281 May 15 '15 at 13:00
  • 2
    @user2301281 Since it contains a 'list' of devices, you need to run through each elements in the list to get it's deviceName. Refer to the `for` loop in @Ali's reply – Hariharan Gandhi May 15 '15 at 13:03
  • thanks. I think I more clear about it now. I will have a try. Thanks for your kindly suggestion. It really save my day. =DD – user2301281 May 15 '15 at 13:10