public class

WifiP2pManager

extends Object
java.lang.Object
   ↳ android.net.wifi.p2p.WifiP2pManager

Class Overview

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 initialize(Context, Looper, WifiP2pManager.ChannelListener) before doing any p2p operation.

Application actions discoverPeers(WifiP2pManager.Channel, WifiP2pManager.ActionListener), connect(WifiP2pManager.Channel, WifiP2pConfig, WifiP2pManager.ActionListener), cancelConnect(WifiP2pManager.Channel, WifiP2pManager.ActionListener), createGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener) and removeGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener) need a WifiP2pManager.ActionListener instance for receiving callbacks onSuccess() or 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 ERROR, P2P_UNSUPPORTED or BUSY.

An application can initiate discovery of peers with discoverPeers(WifiP2pManager.Channel, WifiP2pManager.ActionListener). An initiated discovery request from an application stays active until the device starts connecting to a peer or forms a p2p group. The WifiP2pManager.ActionListener callbacks provide feedback on whether the discovery initiation was successful or failure. Additionally, applications can listen to WIFI_P2P_PEERS_CHANGED_ACTION intent action to know when the peer list changes.

When the peer list change intent WIFI_P2P_PEERS_CHANGED_ACTION is received or when an application needs to fetch the current list of peers, it can request the list of peers with requestPeers(WifiP2pManager.Channel, WifiP2pManager.PeerListListener). When the peer list is available onPeersAvailable(WifiP2pDeviceList) is called with the device list.

An application can initiate a connection request to a peer through connect(WifiP2pManager.Channel, WifiP2pConfig, WifiP2pManager.ActionListener). See 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 createGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener) which creates an access point whose details can be fetched with requestGroupInfo(WifiP2pManager.Channel, WifiP2pManager.GroupInfoListener).

After a successful group formation through createGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener) or through connect(WifiP2pManager.Channel, WifiP2pConfig, WifiP2pManager.ActionListener), use requestConnectionInfo(WifiP2pManager.Channel, WifiP2pManager.ConnectionInfoListener) to fetch the connection details. The connection info WifiP2pInfo contains the address of the group owner groupOwnerAddress and a flag 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.

Android has no platform support for service discovery yet, so applications could run a service discovery protocol to discover services on the peer-to-peer netework.

Note: Registering an application handler with initialize(Context, Looper, WifiP2pManager.ChannelListener) requires the permissions ACCESS_WIFI_STATE and CHANGE_WIFI_STATE to perform any further peer-to-peer operations. Get an instance of this class by calling Context.getSystemService(Context.WIFI_P2P_SERVICE).

Summary

Nested Classes
interface WifiP2pManager.ActionListener Interface for callback invocation on an application action  
class WifiP2pManager.Channel A channel that connects the application to the Wifi p2p framework. 
interface WifiP2pManager.ChannelListener Interface for callback invocation when framework channel is lost  
interface WifiP2pManager.ConnectionInfoListener Interface for callback invocation when connection info is available  
interface WifiP2pManager.GroupInfoListener Interface for callback invocation when group info is available  
interface WifiP2pManager.PeerListListener Interface for callback invocation when peer list is available  
Constants
int BUSY Passed with onFailure(int).
int ERROR Passed with onFailure(int).
String EXTRA_NETWORK_INFO The lookup key for a NetworkInfo object associated with the Wi-Fi network.
String EXTRA_WIFI_P2P_DEVICE The lookup key for a WifiP2pDevice object Retrieve with getParcelableExtra(String).
String EXTRA_WIFI_P2P_INFO The lookup key for a WifiP2pInfo object Retrieve with getParcelableExtra(String).
String EXTRA_WIFI_STATE The lookup key for an int that indicates whether Wi-Fi p2p is enabled or disabled.
int P2P_UNSUPPORTED Passed with onFailure(int).
String WIFI_P2P_CONNECTION_CHANGED_ACTION Broadcast intent action indicating that the state of Wi-Fi p2p connectivity has changed.
String WIFI_P2P_PEERS_CHANGED_ACTION Broadcast intent action indicating that the available peer list has changed.
String WIFI_P2P_STATE_CHANGED_ACTION Broadcast intent action to indicate whether Wi-Fi p2p is enabled or disabled.
int WIFI_P2P_STATE_DISABLED Wi-Fi p2p is disabled.
int WIFI_P2P_STATE_ENABLED Wi-Fi p2p is enabled.
String WIFI_P2P_THIS_DEVICE_CHANGED_ACTION Broadcast intent action indicating that this device details have changed.
Public Methods
void cancelConnect(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)
Cancel any ongoing p2p group negotiation

The function call immediately returns after sending a connection cancellation request to the framework.

void connect(WifiP2pManager.Channel c, WifiP2pConfig config, WifiP2pManager.ActionListener listener)
Start a p2p connection to a device with the specified configuration.
void createGroup(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)
Create a p2p group with the current device as the group owner.
void discoverPeers(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)
Initiate peer discovery.
WifiP2pManager.Channel initialize(Context srcContext, Looper srcLooper, WifiP2pManager.ChannelListener listener)
Registers the application with the Wi-Fi framework.
void removeGroup(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)
Remove the current p2p group.
void requestConnectionInfo(WifiP2pManager.Channel c, WifiP2pManager.ConnectionInfoListener listener)
Request device connection info.
void requestGroupInfo(WifiP2pManager.Channel c, WifiP2pManager.GroupInfoListener listener)
Request p2p group info.
void requestPeers(WifiP2pManager.Channel c, WifiP2pManager.PeerListListener listener)
Request the current list of peers.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int BUSY

Since: API Level 14

Passed with onFailure(int). Indicates that the operation failed because the framework is busy and unable to service the request

Constant Value: 2 (0x00000002)

public static final int ERROR

Since: API Level 14

Passed with onFailure(int). Indicates that the operation failed due to an internal error.

Constant Value: 0 (0x00000000)

public static final String EXTRA_NETWORK_INFO

Since: API Level 14

The lookup key for a NetworkInfo object associated with the Wi-Fi network. Retrieve with getParcelableExtra(String).

Constant Value: "networkInfo"

public static final String EXTRA_WIFI_P2P_DEVICE

Since: API Level 14

The lookup key for a WifiP2pDevice object Retrieve with getParcelableExtra(String).

Constant Value: "wifiP2pDevice"

public static final String EXTRA_WIFI_P2P_INFO

Since: API Level 14

The lookup key for a WifiP2pInfo object Retrieve with getParcelableExtra(String).

Constant Value: "wifiP2pInfo"

public static final String EXTRA_WIFI_STATE

Since: API Level 14

The lookup key for an int that indicates whether Wi-Fi p2p is enabled or disabled. Retrieve it with getIntExtra(String, int).

Constant Value: "wifi_p2p_state"

public static final int P2P_UNSUPPORTED

Since: API Level 14

Passed with onFailure(int). Indicates that the operation failed because p2p is unsupported on the device.

Constant Value: 1 (0x00000001)

public static final String WIFI_P2P_CONNECTION_CHANGED_ACTION

Since: API Level 14

Broadcast intent action indicating that the state of Wi-Fi p2p connectivity has changed. One extra EXTRA_WIFI_P2P_INFO provides the p2p connection info in the form of a WifiP2pInfo object. Another extra EXTRA_NETWORK_INFO provides the network info in the form of a NetworkInfo.

Constant Value: "android.net.wifi.p2p.CONNECTION_STATE_CHANGE"

public static final String WIFI_P2P_PEERS_CHANGED_ACTION

Since: API Level 14

Broadcast intent action indicating that the available peer list has changed. Fetch the changed list of peers with requestPeers(WifiP2pManager.Channel, WifiP2pManager.PeerListListener)

Constant Value: "android.net.wifi.p2p.PEERS_CHANGED"

public static final String WIFI_P2P_STATE_CHANGED_ACTION

Since: API Level 14

Broadcast intent action to indicate whether Wi-Fi p2p is enabled or disabled. An extra EXTRA_WIFI_STATE provides the state information as int.

See Also
Constant Value: "android.net.wifi.p2p.STATE_CHANGED"

public static final int WIFI_P2P_STATE_DISABLED

Since: API Level 14

Wi-Fi p2p is disabled.

Constant Value: 1 (0x00000001)

public static final int WIFI_P2P_STATE_ENABLED

Since: API Level 14

Wi-Fi p2p is enabled.

Constant Value: 2 (0x00000002)

public static final String WIFI_P2P_THIS_DEVICE_CHANGED_ACTION

Since: API Level 14

Broadcast intent action indicating that this device details have changed.

Constant Value: "android.net.wifi.p2p.THIS_DEVICE_CHANGED"

Public Methods

public void cancelConnect (WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

Since: API Level 14

Cancel any ongoing p2p group negotiation

The function call immediately returns after sending a connection cancellation request to the framework. The application is notified of a success or failure to initiate cancellation through listener callbacks onSuccess() or onFailure(int).

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
listener for callbacks on success or failure. Can be null.

public void connect (WifiP2pManager.Channel c, WifiP2pConfig config, WifiP2pManager.ActionListener listener)

Since: API Level 14

Start a p2p connection to a device with the specified configuration.

The function call immediately returns after sending a connection request to the framework. The application is notified of a success or failure to initiate connect through listener callbacks onSuccess() or onFailure(int).

Register for WIFI_P2P_CONNECTION_CHANGED_ACTION intent to determine when the framework notifies of a change in connectivity.

If the current device is not part of a p2p group, a connect request initiates a group negotiation with the peer.

If the current device is part of an existing p2p group or has created a p2p group with createGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener), an invitation to join the group is sent to the peer device.

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
config options as described in WifiP2pConfig class
listener for callbacks on success or failure. Can be null.

public void createGroup (WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

Since: API Level 14

Create a p2p group with the current device as the group owner. This essentially creates an access point that can accept connections from legacy clients as well as other p2p devices.

Note: This function would normally not be used unless the current device needs to form a p2p connection with a legacy client

The function call immediately returns after sending a group creation request to the framework. The application is notified of a success or failure to initiate group creation through listener callbacks onSuccess() or onFailure(int).

Application can request for the group details with requestGroupInfo(WifiP2pManager.Channel, WifiP2pManager.GroupInfoListener).

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
listener for callbacks on success or failure. Can be null.

public void discoverPeers (WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

Since: API Level 14

Initiate peer discovery. A discovery process involves scanning for available Wi-Fi peers for the purpose of establishing a connection.

The function call immediately returns after sending a discovery request to the framework. The application is notified of a success or failure to initiate discovery through listener callbacks onSuccess() or onFailure(int).

The discovery remains active until a connection is initiated or a p2p group is formed. Register for WIFI_P2P_PEERS_CHANGED_ACTION intent to determine when the framework notifies of a change as peers are discovered.

Upon receiving a WIFI_P2P_PEERS_CHANGED_ACTION intent, an application can request for the list of peers using requestPeers(WifiP2pManager.Channel, WifiP2pManager.PeerListListener).

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
listener for callbacks on success or failure. Can be null.

public WifiP2pManager.Channel initialize (Context srcContext, Looper srcLooper, WifiP2pManager.ChannelListener listener)

Since: API Level 14

Registers the application with the Wi-Fi framework. This function must be the first to be called before any p2p operations are performed.

Parameters
srcContext is the context of the source
srcLooper is the Looper on which the callbacks are receivied
listener for callback at loss of framework communication. Can be null.
Returns
  • Channel instance that is necessary for performing any further p2p operations

public void removeGroup (WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

Since: API Level 14

Remove the current p2p group.

The function call immediately returns after sending a group removal request to the framework. The application is notified of a success or failure to initiate group removal through listener callbacks onSuccess() or onFailure(int).

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
listener for callbacks on success or failure. Can be null.

public void requestConnectionInfo (WifiP2pManager.Channel c, WifiP2pManager.ConnectionInfoListener listener)

Since: API Level 14

Request device connection info.

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
listener for callback when connection info is available. Can be null.

public void requestGroupInfo (WifiP2pManager.Channel c, WifiP2pManager.GroupInfoListener listener)

Since: API Level 14

Request p2p group info.

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
listener for callback when group info is available. Can be null.

public void requestPeers (WifiP2pManager.Channel c, WifiP2pManager.PeerListListener listener)

Since: API Level 14

Request the current list of peers.

Parameters
c is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener)
listener for callback when peer list is available. Can be null.