WifiAwareManager
public
class
WifiAwareManager
extends Object
java.lang.Object | |
↳ | android.net.wifi.aware.WifiAwareManager |
This class provides the primary API for managing Wi-Fi Aware operations: discovery and peer-to-peer data connections.
The class provides access to:
- Initialize a Aware cluster (peer-to-peer synchronization). Refer to
attach(AttachCallback, Handler)
. - Create discovery sessions (publish or subscribe sessions). Refer to
publish(PublishConfig, DiscoverySessionCallback, Handler)
andsubscribe(SubscribeConfig, DiscoverySessionCallback, Handler)
. - Create a Aware network specifier to be used with
requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)
to set-up a Aware connection with a peer. Refer tocreateNetworkSpecifierOpen(PeerHandle)
,createNetworkSpecifierPassphrase(PeerHandle, String)
,createNetworkSpecifierOpen(int, byte[])
, andcreateNetworkSpecifierPassphrase(int, byte[], String)
.
Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that
the functionality is available use the isAvailable()
function. To track
changes in Aware usability register for the ACTION_WIFI_AWARE_STATE_CHANGED
broadcast. Note that this broadcast is not sticky - you should register for it and then
check the above API to avoid a race condition.
An application must use attach(AttachCallback, Handler)
to initialize a
Aware cluster - before making any other Aware operation. Aware cluster membership is a
device-wide operation - the API guarantees that the device is in a cluster or joins a
Aware cluster (or starts one if none can be found). Information about attach success (or
failure) are returned in callbacks of AttachCallback
. Proceed with Aware
discovery or connection setup only after receiving confirmation that Aware attach
succeeded - onAttached(WifiAwareSession)
. When an
application is finished using Aware it must use the
close()
API to indicate to the Aware service that the device
may detach from the Aware cluster. The device will actually disable Aware once the last
application detaches.
Once a Aware attach is confirmed use the
publish(PublishConfig, DiscoverySessionCallback, Handler)
or
subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)
to create publish or subscribe Aware discovery sessions. Events are called on the
provided callback object DiscoverySessionCallback
. Specifically, the
onPublishStarted(PublishDiscoverySession)
and
onSubscribeStarted(SubscribeDiscoverySession)
return PublishDiscoverySession
and
SubscribeDiscoverySession
objects respectively on which additional session operations can be performed, e.g. updating
the session updatePublish(PublishConfig)
and
updateSubscribe(SubscribeConfig)
. Sessions can
also be used to send messages using the
sendMessage(PeerHandle, int, byte[])
APIs. When an
application is finished with a discovery session it must terminate it using the
close()
API.
Creating connections between Aware devices is managed by the standard
requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)
.
The NetworkRequest
object should be constructed with:
addTransportType(int)
ofTRANSPORT_WIFI_AWARE
.setNetworkSpecifier(String)
usingcreateNetworkSpecifierOpen(int, byte[])
,createNetworkSpecifierPassphrase(int, byte[], String)
,createNetworkSpecifierOpen(PeerHandle)
, orcreateNetworkSpecifierPassphrase(PeerHandle, String)
.
Instances of this class must be obtained using Context.getSystemService(Class)
with the argument WifiAwareManager.class
or Context.getSystemService(String)
with the argument Context.WIFI_AWARE_SERVICE
.
Summary
Constants | |
---|---|
String |
ACTION_WIFI_AWARE_STATE_CHANGED
Broadcast intent action to indicate that the state of Wi-Fi Aware availability has changed. |
int |
WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
Connection creation role is that of INITIATOR. |
int |
WIFI_AWARE_DATA_PATH_ROLE_RESPONDER
Connection creation role is that of RESPONDER. |
Public methods | |
---|---|
void
|
attach(AttachCallback attachCallback, Handler handler)
Attach to the Wi-Fi Aware service - enabling the application to create discovery sessions or create connections to peers. |
void
|
attach(AttachCallback attachCallback, IdentityChangedListener identityChangedListener, Handler handler)
Attach to the Wi-Fi Aware service - enabling the application to create discovery sessions or create connections to peers. |
Characteristics
|
getCharacteristics()
Returns the characteristics of the Wi-Fi Aware interface: a set of parameters which specify limitations on configurations, e.g. |
boolean
|
isAvailable()
Returns the current status of Aware API: whether or not Aware is available. |
Inherited methods | |
---|---|
Constants
ACTION_WIFI_AWARE_STATE_CHANGED
String ACTION_WIFI_AWARE_STATE_CHANGED
Broadcast intent action to indicate that the state of Wi-Fi Aware availability has changed.
Use the isAvailable()
to query the current status.
This broadcast is not sticky, use the isAvailable()
API after registering
the broadcast to check the current state of Wi-Fi Aware.
Note: The broadcast is only delivered to registered receivers - no manifest registered components will be launched.
Constant Value: "android.net.wifi.aware.action.WIFI_AWARE_STATE_CHANGED"
WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
int WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
Connection creation role is that of INITIATOR. Used to create a network specifier string when requesting a Aware network.
See also:
Constant Value: 0 (0x00000000)
WIFI_AWARE_DATA_PATH_ROLE_RESPONDER
int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER
Connection creation role is that of RESPONDER. Used to create a network specifier string when requesting a Aware network.
See also:
Constant Value: 1 (0x00000001)
Public methods
attach
void attach (AttachCallback attachCallback, Handler handler)
Attach to the Wi-Fi Aware service - enabling the application to create discovery sessions or
create connections to peers. The device will attach to an existing cluster if it can find
one or create a new cluster (if it is the first to enable Aware in its vicinity). Results
(e.g. successful attach to a cluster) are provided to the attachCallback
object.
An application must call close()
when done with the
Wi-Fi Aware object.
Note: a Aware cluster is a shared resource - if the device is already attached to a cluster
then this function will simply indicate success immediately using the same attachCallback
.
Parameters | |
---|---|
attachCallback |
AttachCallback : A callback for attach events, extended from
AttachCallback .This value must never be |
handler |
Handler : The Handler on whose thread to execute the callbacks of the attachCallback object. If a null is provided then the application's main thread will be
used.
|
attach
void attach (AttachCallback attachCallback, IdentityChangedListener identityChangedListener, Handler handler)
Attach to the Wi-Fi Aware service - enabling the application to create discovery sessions or
create connections to peers. The device will attach to an existing cluster if it can find
one or create a new cluster (if it is the first to enable Aware in its vicinity). Results
(e.g. successful attach to a cluster) are provided to the attachCallback
object.
An application must call close()
when done with the
Wi-Fi Aware object.
Note: a Aware cluster is a shared resource - if the device is already attached to a cluster
then this function will simply indicate success immediately using the same attachCallback
.
This version of the API attaches a listener to receive the MAC address of the Aware interface
on startup and whenever it is updated (it is randomized at regular intervals for privacy).
The application must have the ACCESS_COARSE_LOCATION
permission to execute this attach request. Otherwise, use the
attach(AttachCallback, Handler)
version. Note that aside from permission
requirements this listener will wake up the host at regular intervals causing higher power
consumption, do not use it unless the information is necessary (e.g. for OOB discovery).
Parameters | |
---|---|
attachCallback |
AttachCallback : A callback for attach events, extended from
AttachCallback .This value must never be |
identityChangedListener |
IdentityChangedListener : A listener for changed identity, extended from
IdentityChangedListener .This value must never be |
handler |
Handler : The Handler on whose thread to execute the callbacks of the attachCallback and identityChangedListener objects. If a null is provided then the
application's main thread will be used.
|
getCharacteristics
Characteristics getCharacteristics ()
Returns the characteristics of the Wi-Fi Aware interface: a set of parameters which specify limitations on configurations, e.g. the maximum service name length.
Returns | |
---|---|
Characteristics |
An object specifying configuration limitations of Aware. |
isAvailable
boolean isAvailable ()
Returns the current status of Aware API: whether or not Aware is available. To track
changes in the state of Aware API register for the
ACTION_WIFI_AWARE_STATE_CHANGED
broadcast.
Returns | |
---|---|
boolean |
A boolean indicating whether the app can use the Aware API at this time (true) or not (false). |