See Also: WifiP2pManager Members
This class provides the API for managing Wi-Fi peer-to-peer connectivity. This lets an application discover available peers, setup connection to peers and query for the list of peers. When a p2p connection is formed over wifi, the device continues to maintain the uplink connection over mobile or any other available network for internet connectivity on the device.
The API is asynchronous and responses to requests from an application are on listener callbacks provided by the application. The application needs to do an initialization with WifiP2pManager.Initialize(Android.Content.Context, Android.OS.Looper, Android.OS.Looper) before doing any p2p operation.
Most application calls need a NoType:android/net/wifi/p2p/WifiP2pManager$ActionListener;Href=../../../../../reference/android/net/wifi/p2p/WifiP2pManager.ActionListener.html instance for receiving callbacks NoType:android/net/wifi/p2p/WifiP2pManager$ActionListener;Href=../../../../../reference/android/net/wifi/p2p/WifiP2pManager.ActionListener.html#onSuccess() or NoType:android/net/wifi/p2p/WifiP2pManager$ActionListener;Href=../../../../../reference/android/net/wifi/p2p/WifiP2pManager.ActionListener.html#onFailure(int). Action callbacks indicate whether the initiation of the action was a success or a failure. Upon failure, the reason of failure can be one of WifiP2pManager.Error, WifiP2pManager.P2pUnsupported or WifiP2pManager.Busy.
An application can initiate discovery of peers with WifiP2pManager.DiscoverPeers(.Channel, .IActionListener). An initiated discovery request from an application stays active until the device starts connecting to a peer ,forms a p2p group or there is an explicit WifiP2pManager.StopPeerDiscovery(.Channel, .IActionListener). Applications can listen to WifiP2pManager.WifiP2pDiscoveryChangedAction to know if a peer-to-peer discovery is running or stopped. Additionally, WifiP2pManager.WifiP2pPeersChangedAction indicates if the peer list has changed.
When an application needs to fetch the current list of peers, it can request the list of peers with WifiP2pManager.RequestPeers(.Channel, .IPeerListListener). When the peer list is available NoType:android/net/wifi/p2p/WifiP2pManager$PeerListListener;Href=../../../../../reference/android/net/wifi/p2p/WifiP2pManager.PeerListListener.html#onPeersAvailable(android.net.wifi.p2p.WifiP2pDeviceList) is called with the device list.
An application can initiate a connection request to a peer through WifiP2pManager.Connect(.Channel, Android.Net.Wifi.P2p.WifiP2pConfig, Android.Net.Wifi.P2p.WifiP2pConfig). See Android.Net.Wifi.P2p.WifiP2pConfig for details on setting up the configuration. For communication with legacy Wi-Fi devices that do not support p2p, an app can create a group using WifiP2pManager.CreateGroup(.Channel, .IActionListener) which creates an access point whose details can be fetched with WifiP2pManager.RequestGroupInfo(.Channel, .IGroupInfoListener).
After a successful group formation through WifiP2pManager.CreateGroup(.Channel, .IActionListener) or through WifiP2pManager.Connect(.Channel, Android.Net.Wifi.P2p.WifiP2pConfig, Android.Net.Wifi.P2p.WifiP2pConfig), use WifiP2pManager.RequestConnectionInfo(.Channel, .IConnectionInfoListener) to fetch the connection details. The connection info Android.Net.Wifi.P2p.WifiP2pInfo contains the address of the group owner WifiP2pInfo.GroupOwnerAddress and a flag WifiP2pInfo.IsGroupOwner to indicate if the current device is a p2p group owner. A p2p client can thus communicate with the p2p group owner through a socket connection.
With peer discovery using WifiP2pManager.DiscoverPeers(.Channel, .IActionListener), an application discovers the neighboring peers, but has no good way to figure out which peer to establish a connection with. For example, if a game application is interested in finding all the neighboring peers that are also running the same game, it has no way to find out until after the connection is setup. Pre-association service discovery is meant to address this issue of filtering the peers based on the running services.
With pre-association service discovery, an application can advertise a service for a application on a peer device prior to a connection setup between the devices. Currently, DNS based service discovery (Bonjour) and Upnp are the higher layer protocols supported. Get Bonjour resources at dns-sd.org and Upnp resources at upnp.org As an example, a video application can discover a Upnp capable media renderer prior to setting up a Wi-fi p2p connection with the device.
An application can advertise a Upnp or a Bonjour service with a call to WifiP2pManager.AddLocalService(.Channel, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceInfo, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceInfo). After a local service is added, the framework automatically responds to a peer application discovering the service prior to establishing a p2p connection. A call to WifiP2pManager.RemoveLocalService(.Channel, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceInfo, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceInfo) removes a local service and WifiP2pManager.ClearLocalServices(.Channel, .IActionListener) can be used to clear all local services.
An application that is looking for peer devices that support certain services can do so with a call to WifiP2pManager.DiscoverServices(.Channel, .IActionListener). Prior to initiating the discovery, application can add service discovery request with a call to WifiP2pManager.AddServiceRequest(.Channel, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceRequest, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceRequest), remove a service discovery request with a call to WifiP2pManager.RemoveServiceRequest(.Channel, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceRequest, Android.Net.Wifi.P2p.Nsd.WifiP2pServiceRequest) or clear all requests with a call to WifiP2pManager.ClearServiceRequests(.Channel, .IActionListener). When no service requests remain, a previously running service discovery will stop. The application is notified of a result of service discovery request through listener callbacks set through WifiP2pManager.SetDnsSdResponseListeners(.Channel, .IDnsSdServiceResponseListener, .IDnsSdServiceResponseListener) for Bonjour or WifiP2pManager.SetUpnpServiceResponseListener(.Channel, .IUpnpServiceResponseListener) for Upnp.
Note: Registering an application handler with WifiP2pManager.Initialize(Android.Content.Context, Android.OS.Looper, Android.OS.Looper) requires the permissions NoType:android/Manifest$permission;Href=../../../../../reference/android/Manifest.permission.html#ACCESS_WIFI_STATE and NoType:android/Manifest$permission;Href=../../../../../reference/android/Manifest.permission.html#CHANGE_WIFI_STATE to perform any further peer-to-peer operations. Get an instance of this class by calling Android.Content.Context.GetSystemService(string).