java.lang.Object | |
↳ | android.net.wifi.p2p.WifiP2pManager |
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)
.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
WifiP2pManager.ActionListener | Interface for callback invocation on an application action | ||||||||||
WifiP2pManager.Channel | A channel that connects the application to the Wifi p2p framework. | ||||||||||
WifiP2pManager.ChannelListener | Interface for callback invocation when framework channel is lost | ||||||||||
WifiP2pManager.ConnectionInfoListener | Interface for callback invocation when connection info is available | ||||||||||
WifiP2pManager.GroupInfoListener | Interface for callback invocation when group info is available | ||||||||||
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Cancel any ongoing p2p group negotiation
The function call immediately returns after sending a connection cancellation request to the framework. | |||||||||||
Start a p2p connection to a device with the specified configuration.
| |||||||||||
Create a p2p group with the current device as the group owner.
| |||||||||||
Initiate peer discovery.
| |||||||||||
Registers the application with the Wi-Fi framework.
| |||||||||||
Remove the current p2p group.
| |||||||||||
Request device connection info.
| |||||||||||
Request p2p group info.
| |||||||||||
Request the current list of peers.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Passed with onFailure(int)
.
Indicates that the operation failed because the framework is busy and
unable to service the request
Passed with onFailure(int)
.
Indicates that the operation failed due to an internal error.
The lookup key for a NetworkInfo
object associated with the
Wi-Fi network. Retrieve with
getParcelableExtra(String)
.
The lookup key for a WifiP2pDevice
object
Retrieve with getParcelableExtra(String)
.
The lookup key for a WifiP2pInfo
object
Retrieve with getParcelableExtra(String)
.
The lookup key for an int that indicates whether Wi-Fi p2p is enabled or disabled.
Retrieve it with getIntExtra(String, int)
.
Passed with onFailure(int)
.
Indicates that the operation failed because p2p is unsupported on the device.
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
.
Broadcast intent action indicating that the available peer list has changed. Fetch
the changed list of peers with requestPeers(WifiP2pManager.Channel, WifiP2pManager.PeerListListener)
Broadcast intent action to indicate whether Wi-Fi p2p is enabled or disabled. An
extra EXTRA_WIFI_STATE
provides the state information as int.
Broadcast intent action indicating that this device details have changed.
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)
.
c | is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener) |
---|---|
listener | for callbacks on success or failure. Can be null. |
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.
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. |
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)
.
c | is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener) |
---|---|
listener | for callbacks on success or failure. Can be null. |
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)
.
c | is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener) |
---|---|
listener | for callbacks on success or failure. Can be null. |
Registers the application with the Wi-Fi framework. This function must be the first to be called before any p2p operations are performed.
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. |
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)
.
c | is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener) |
---|---|
listener | for callbacks on success or failure. Can be null. |
Request device connection info.
c | is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener) |
---|---|
listener | for callback when connection info is available. Can be null. |
Request p2p group info.
c | is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener) |
---|---|
listener | for callback when group info is available. Can be null. |
Request the current list of peers.
c | is the channel created at initialize(Context, Looper, WifiP2pManager.ChannelListener) |
---|---|
listener | for callback when peer list is available. Can be null. |