Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#inhmethods)

# WifiAwareSession

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/net/wifi/aware/WifiAwareSession "View this page in Kotlin") \|Java


`
public


class
WifiAwareSession
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/java/lang/AutoCloseable


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.net.wifi.aware.WifiAwareSession |

<br />

*** ** * ** ***

This class represents a Wi-Fi Aware session - an attachment to the Wi-Fi Aware service through
which the app can execute discovery operations.

## Summary

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#close()() ` Destroy the Wi-Fi Aware service session and, if no other applications are attached to Aware, also disable Aware. |
| ` https://developer.android.com/reference/android/net/NetworkSpecifier` | ` https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#createNetworkSpecifierOpen(int,%20byte[])(int role, byte[] peer) ` *This method was deprecated in API level 31. Please use in-band data-path setup, refer to `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareNetworkSpecifier.Builder`, `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#publish(android.net.wifi.aware.PublishConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)` and `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#subscribe(android.net.wifi.aware.SubscribeConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)`* |
| ` https://developer.android.com/reference/android/net/NetworkSpecifier` | ` https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#createNetworkSpecifierPassphrase(int,%20byte[],%20java.lang.String)(int role, byte[] peer, https://developer.android.com/reference/java/lang/String passphrase) ` *This method was deprecated in API level 31. Please use in-band data-path setup, refer to `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareNetworkSpecifier.Builder`, `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#publish(android.net.wifi.aware.PublishConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)` and `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#subscribe(android.net.wifi.aware.SubscribeConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)`* |
| ` void` | ` https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#publish(android.net.wifi.aware.PublishConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)(https://developer.android.com/reference/android/net/wifi/aware/PublishConfig publishConfig, https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback callback, https://developer.android.com/reference/android/os/Handler handler) ` Issue a request to the Aware service to create a new Aware publish discovery session, using the specified `publishConfig` configuration. |
| ` void` | ` https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#subscribe(android.net.wifi.aware.SubscribeConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)(https://developer.android.com/reference/android/net/wifi/aware/SubscribeConfig subscribeConfig, https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback callback, https://developer.android.com/reference/android/os/Handler handler) ` Issue a request to the Aware service to create a new Aware subscribe discovery session, using the specified `subscribeConfig` configuration. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||
| From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() ` Closes this resource, relinquishing any underlying resources. | ||

## Public methods

### close

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void close ()
```

Destroy the Wi-Fi Aware service session and, if no other applications are attached to Aware,
also disable Aware. This method destroys all outstanding operations - i.e. all publish and
subscribes are terminated, and any outstanding data-links are shut-down. However, it is
good practice to destroy these discovery sessions and connections explicitly before a
session-wide destroy.


An application may re-attach after a destroy using
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#attach(android.net.wifi.aware.AttachCallback,%20android.os.Handler)` .

<br />

### createNetworkSpecifierOpen

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level
31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public NetworkSpecifier createNetworkSpecifierOpen (int role, 
                byte[] peer)
```


**This method was deprecated
in API level 31.**   

Please use in-band data-path setup, refer to
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareNetworkSpecifier.Builder`,
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#publish(android.net.wifi.aware.PublishConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)` and
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#subscribe(android.net.wifi.aware.SubscribeConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)`

Create a `https://developer.android.com/reference/android/net/NetworkRequest.Builder#setNetworkSpecifier(android.net.NetworkSpecifier)` for
an unencrypted WiFi Aware connection (link) to the specified peer. The
`https://developer.android.com/reference/android/net/NetworkRequest.Builder#addTransportType(int)` should be set to
`https://developer.android.com/reference/android/net/NetworkCapabilities#TRANSPORT_WIFI_AWARE`.


This API is targeted for applications which can obtain the peer MAC address using OOB
(out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
when using Aware discovery use the alternative network specifier method -
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareNetworkSpecifier.Builder`.


To set up an encrypted link use the
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#createNetworkSpecifierPassphrase(int,%20byte[],%20java.lang.String)` API.

<br />

| Parameters ||
|---|---|
| `role` | `int`: The role of this device: `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR` or `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER` Value is one of the following: - `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR` - `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER` |
| `peer` | `byte`: The MAC address of the peer's Aware discovery interface. On a RESPONDER this value is used to gate the acceptance of a connection request from only that peer. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/net/NetworkSpecifier` | A `https://developer.android.com/reference/android/net/NetworkSpecifier` to be used to construct `https://developer.android.com/reference/android/net/NetworkRequest.Builder#setNetworkSpecifier(android.net.NetworkSpecifier)` to pass to `https://developer.android.com/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback)` \[or other varieties of that API\]. <br /> |

### createNetworkSpecifierPassphrase

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level
31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public NetworkSpecifier createNetworkSpecifierPassphrase (int role, 
                byte[] peer, 
                String passphrase)
```


**This method was deprecated
in API level 31.**   

Please use in-band data-path setup, refer to
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareNetworkSpecifier.Builder`,
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#publish(android.net.wifi.aware.PublishConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)` and
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#subscribe(android.net.wifi.aware.SubscribeConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler)`

Create a `https://developer.android.com/reference/android/net/NetworkRequest.Builder#setNetworkSpecifier(android.net.NetworkSpecifier)` for
an encrypted WiFi Aware connection (link) to the specified peer. The
`https://developer.android.com/reference/android/net/NetworkRequest.Builder#addTransportType(int)` should be set to
`https://developer.android.com/reference/android/net/NetworkCapabilities#TRANSPORT_WIFI_AWARE`.


This API is targeted for applications which can obtain the peer MAC address using OOB
(out-of-band) discovery. Aware discovery does not provide the MAC address of the peer -
when using Aware discovery use the alternative network specifier method -
`https://developer.android.com/reference/android/net/wifi/aware/WifiAwareNetworkSpecifier.Builder`.

<br />

| Parameters ||
|---|---|
| `role` | `int`: The role of this device: `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR` or `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER` Value is one of the following: - `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR` - `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER` |
| `peer` | `byte`: The MAC address of the peer's Aware discovery interface. On a RESPONDER this value is used to gate the acceptance of a connection request from only that peer. This value cannot be `null`. <br /> |
| `passphrase` | `String`: The passphrase to be used to encrypt the link. The PMK is generated from the passphrase. Use `https://developer.android.com/reference/android/net/wifi/aware/WifiAwareSession#createNetworkSpecifierOpen(int,%20byte[])` to specify an open (unencrypted) link. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/net/NetworkSpecifier` | A `https://developer.android.com/reference/android/net/NetworkSpecifier` to be used to construct `https://developer.android.com/reference/android/net/NetworkRequest.Builder#setNetworkSpecifier(android.net.NetworkSpecifier)` to pass to `https://developer.android.com/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback)` \[or other varieties of that API\]. <br /> |

### publish

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void publish (PublishConfig publishConfig, 
                DiscoverySessionCallback callback, 
                Handler handler)
```

Issue a request to the Aware service to create a new Aware publish discovery session, using
the specified `publishConfig` configuration. The results of the publish operation
are routed to the callbacks of `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback`:

- `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback#onPublishStarted(android.net.wifi.aware.PublishDiscoverySession)` is called when the publish session is created and provides a handle to the session. Further operations on the publish session can be executed on that object.
- `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback#onSessionConfigFailed()` is called if the publish operation failed.


Other results of the publish session operations will also be routed to callbacks
on the `callback` object. The resulting publish session can be modified using
`https://developer.android.com/reference/android/net/wifi/aware/PublishDiscoverySession#updatePublish(android.net.wifi.aware.PublishConfig)`.

The total count of currently available Wi-Fi Aware publish sessions is limited and is
available via the `https://developer.android.com/reference/android/net/wifi/aware/AwareResources#getAvailablePublishSessionsCount()` method.


An application must use the `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySession#close()` to
terminate the publish discovery session once it isn't needed. This will free
resources as well terminate any on-air transmissions.


If targeting `https://developer.android.com/reference/android/os/Build.VERSION_CODES#TIRAMISU` or later, the application must
have `https://developer.android.com/reference/android/Manifest.permission#NEARBY_WIFI_DEVICES` with
android:usesPermissionFlags="neverForLocation". If the application does not declare
android:usesPermissionFlags="neverForLocation", then it must also have
`https://developer.android.com/reference/android/Manifest.permission#ACCESS_FINE_LOCATION`.

If targeting an earlier release than `https://developer.android.com/reference/android/os/Build.VERSION_CODES#TIRAMISU`, the
application must have `https://developer.android.com/reference/android/Manifest.permission#ACCESS_FINE_LOCATION`.

<br />

| Parameters ||
|---|---|
| `publishConfig` | `PublishConfig`: The `https://developer.android.com/reference/android/net/wifi/aware/PublishConfig` specifying the configuration of the requested publish session. This value cannot be `null`. <br /> |
| `callback` | `DiscoverySessionCallback`: A `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback` derived object to be used for session event callbacks. This value cannot be `null`. <br /> |
| `handler` | `Handler`: The Handler on whose thread to execute the callbacks of the `callback` object. If a null is provided then the application's main thread will be used. <br /> |

### subscribe

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void subscribe (SubscribeConfig subscribeConfig, 
                DiscoverySessionCallback callback, 
                Handler handler)
```

Issue a request to the Aware service to create a new Aware subscribe discovery session, using
the specified `subscribeConfig` configuration. The results of the subscribe
operation are routed to the callbacks of `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback`:

- `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback#onSubscribeStarted(android.net.wifi.aware.SubscribeDiscoverySession)` is called when the subscribe session is created and provides a handle to the session. Further operations on the subscribe session can be executed on that object.
- `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback#onSessionConfigFailed()` is called if the subscribe operation failed.


Other results of the subscribe session operations will also be routed to callbacks
on the `callback` object. The resulting subscribe session can be modified using
`https://developer.android.com/reference/android/net/wifi/aware/SubscribeDiscoverySession#updateSubscribe(android.net.wifi.aware.SubscribeConfig)`.

The total count of currently available Wi-Fi Aware subscribe sessions is limited and is
available via the `https://developer.android.com/reference/android/net/wifi/aware/AwareResources#getAvailableSubscribeSessionsCount()` method.


An application must use the `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySession#close()` to
terminate the subscribe discovery session once it isn't needed. This will free
resources as well terminate any on-air transmissions.


If targeting `https://developer.android.com/reference/android/os/Build.VERSION_CODES#TIRAMISU` or later, the application must
have `https://developer.android.com/reference/android/Manifest.permission#NEARBY_WIFI_DEVICES` with
android:usesPermissionFlags="neverForLocation". If the application does not declare
android:usesPermissionFlags="neverForLocation", then it must also have
`https://developer.android.com/reference/android/Manifest.permission#ACCESS_FINE_LOCATION`.

If targeting an earlier release than `https://developer.android.com/reference/android/os/Build.VERSION_CODES#TIRAMISU`, the
application must have `https://developer.android.com/reference/android/Manifest.permission#ACCESS_FINE_LOCATION`.

<br />

| Parameters ||
|---|---|
| `subscribeConfig` | `SubscribeConfig`: The `https://developer.android.com/reference/android/net/wifi/aware/SubscribeConfig` specifying the configuration of the requested subscribe session. This value cannot be `null`. <br /> |
| `callback` | `DiscoverySessionCallback`: A `https://developer.android.com/reference/android/net/wifi/aware/DiscoverySessionCallback` derived object to be used for session event callbacks. This value cannot be `null`. <br /> |
| `handler` | `Handler`: The Handler on whose thread to execute the callbacks of the `callback` object. If a null is provided then the application's main thread will be used. <br /> |

## Protected methods

### finalize

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void finalize ()
```

Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
A subclass overrides the `finalize` method to dispose of
system resources or to perform other cleanup.


The general contract of `finalize` is that it is invoked
if and when the Java virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The `finalize` method may take any action, including
making this object available again to other threads; the usual purpose
of `finalize`, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.


The `finalize` method of class `Object` performs no
special action; it simply returns normally. Subclasses of
`Object` may override this definition.


The Java programming language does not guarantee which thread will
invoke the `finalize` method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.


After the `finalize` method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.


The `finalize` method is never invoked more than once by a Java
virtual machine for any given object.


Any exception thrown by the `finalize` method causes
the finalization of this object to be halted, but is otherwise
ignored.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Throwable` |   |