-1

I am connecting two devices using wifi p2p from Android. I would like to know if there is any way for both devices to know the name of each other when the connection is established. When the device is starting the connection request, it is easy because you choose the peer from the list so you see the name. The question is focus to the device that receives the connection request!!!

I guess it has to be possible since the first time you try to connect you see a pop up with the name of the peer to accept the connection. But I don't know where this information can be found when programming an app.

I guess it has to be stored either in NetworkInfo (when you received a change of state) or in WifiP2pInfo (when the connection info is available).

BlueMountain
  • 197
  • 2
  • 17

3 Answers3

3

When you receive the WIFI_P2P_CONNECTION_CHANGED_ACTION broadcast, if you are on API level 18 or higher, there is an extra in the intent, called EXTRA_WIFI_P2P_GROUP.

EXTRA_WIFI_P2P_GROUP

This returns a WifiP2pGroup on which you can call getClientList(). This gives you a collection of WifiP2pDevices. Once you have a WifiP2pDevice you can just get the field "deviceName".

joe_deniable
  • 2,562
  • 3
  • 28
  • 38
1

Getting peer name is straight forward. Once you get the device list

 public String getName(WifiP2pDevice device){         
    return device.deviceName;
 }

Not sure if this is what you are asking for, pardon me if this is not the answer.

SafalFrom2050
  • 687
  • 6
  • 12
  • Thank you but this is not the answer. I'm asking for the name of the peer device, the name of the devices you are connected to (not the own name). – BlueMountain Jun 03 '18 at 10:56
  • Yes, actually you get the list of available peer as List of WiFiP2pDevice. And you can get their individual name from above method. If you want just connected devices name and not discovered ones, you can store connected WiFiP2pDevices list seperately. :) – SafalFrom2050 Jun 04 '18 at 10:36
  • and how do you get connected (to you) peers? – BlueMountain Jun 04 '18 at 11:48
  • For every WiFiP2pDevice, you discovered, you can use get their status from "WiFiP2pDevice.status" which returns int value corresponding to WiFiP2pDevice.Connected, **.Available, **.Disconnected and so on :) – SafalFrom2050 Jun 04 '18 at 12:12
  • Find more info here https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pDevice.html#status – SafalFrom2050 Jun 04 '18 at 12:13
  • As far as I know, this status can be retrieved from all devices you are able to discover. By doing so, even if you get the status of the devices, -- lets say for example you get the status of a device which is connected -- still you are not able to differenciate/know whether this peer device is connected to you or to other device, you just know that this devices is "connected" but not to who. – BlueMountain Jun 04 '18 at 12:16
  • Maybe you are mistaken by Wifi Direct concept, every devices is connected to a single group owner. If you get the status of selected device as Connected, that means the device is connected in the same group that you are in. If it is connected to another group, the status will definitely not be Connected. – SafalFrom2050 Jun 04 '18 at 12:21
  • lets put an example so I better understand: I have 4 devices which are: 2 group owners and 2 clients connected in pairs to each GO (so there are 2 grups of 2 devices). If a 5th device scan for peers and discover the 4 of them,first, will this device get the status of the other 4? in case it does, which status will it see? – BlueMountain Jun 04 '18 at 12:25
  • Maybe you know this already but, two groups in Wifi direct connection is impossible. In this case, if your 5th device connects to one group among two say Group A, then obviouly devices in your next Group B will not get Connected status because it is not connected to their group. – SafalFrom2050 Jun 04 '18 at 12:33
  • I didn't know that. You mean I cannot have 4 phones connected in pairs? lets say, (dev 1 connected to dev2) and (dev3 connected to dev4)? On the other hand, I want to know what is device 5 going to see during the scan (before getting connected to any group). – BlueMountain Jun 04 '18 at 12:39
  • Yes that is possible, but that would be two different connections(Groups). Your dev5 will discover 4 devices and it will be connected to a group, that the device you choose to connect is a part of. – SafalFrom2050 Jun 04 '18 at 12:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172410/discussion-between-safal-sharma-and-bluemountain). – SafalFrom2050 Jun 04 '18 at 12:45
0

There is requestConnectionInfo method by this you can get all information about connected deivce simply implement this after connection is made like below

  wifiManager.requestConnectionInfo(wifichannel, new WifiP2pManager.ConnectionInfoListener() {
        @Override
        public void onConnectionInfoAvailable(WifiP2pInfo info) {
        //by info you can get host address and isGroupowner  or else information you to 
implement your code after this
 }
Nikhil Sharma
  • 593
  • 7
  • 23