WifiAwareSession

open class WifiAwareSession : AutoCloseable
kotlin.Any
   ↳ android.net.wifi.aware.WifiAwareSession

This class represents a Wi-Fi Aware session - an attachment to the Wi-Fi Aware service through which the app can execute discovery operations.
Requires API level 26 (Android 8.0, Oreo)

Summary

Public methods
open NetworkSpecifier!

Create a android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier) for an unencrypted WiFi Aware connection (link) to the specified peer.

open NetworkSpecifier!

Create a android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier) for an encrypted WiFi Aware connection (link) to the specified peer.

open Unit
publish(publishConfig: PublishConfig, callback: DiscoverySessionCallback, handler: Handler?)

Issue a request to the Aware service to create a new Aware publish discovery session, using the specified publishConfig configuration.

open Unit

Destroy the Wi-Fi Aware service session and, if no other applications are attached to Aware, also disable Aware.

open Unit
subscribe(subscribeConfig: SubscribeConfig, callback: DiscoverySessionCallback, handler: Handler?)

Issue a request to the Aware service to create a new Aware subscribe discovery session, using the specified subscribeConfig configuration.

Public methods

createNetworkSpecifierOpen

added in API level 26
open fun createNetworkSpecifierOpen(role: Int, peer: ByteArray): NetworkSpecifier!

Create a android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier) for an unencrypted WiFi Aware connection (link) to the specified peer. The android.net.NetworkRequest.Builder#addTransportType(int) should be set to 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 - DiscoverySession#createNetworkSpecifierOpen(PeerHandle).

To set up an encrypted link use the #createNetworkSpecifierPassphrase(int, byte[], String) API.
Requires API level 26 (Android 8.0, Oreo)

Parameters
role Int: The role of this device: WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR or WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER Value is android.net.wifi.aware.WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR, or android.net.wifi.aware.WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER
peer Int: 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 must never be null.
Return
NetworkSpecifier!: A NetworkSpecifier to be used to construct android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier) to pass to android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, * android.net.ConnectivityManager.NetworkCallback) [or other varieties of that API].

createNetworkSpecifierPassphrase

added in API level 26
open fun createNetworkSpecifierPassphrase(role: Int, peer: ByteArray, passphrase: String): NetworkSpecifier!

Create a android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier) for an encrypted WiFi Aware connection (link) to the specified peer. The android.net.NetworkRequest.Builder#addTransportType(int) should be set to 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 - DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String).
Requires API level 26 (Android 8.0, Oreo)

Parameters
role Int: The role of this device: WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR or WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER Value is android.net.wifi.aware.WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR, or android.net.wifi.aware.WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER
peer Int: 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 must never be null.
passphrase Int: The passphrase to be used to encrypt the link. The PMK is generated from the passphrase. Use #createNetworkSpecifierOpen(int, byte[]) to specify an open (unencrypted) link. This value must never be null.
Return
NetworkSpecifier!: A NetworkSpecifier to be used to construct android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier) to pass to android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, * android.net.ConnectivityManager.NetworkCallback) [or other varieties of that API].

publish

added in API level 26
open fun publish(publishConfig: PublishConfig, callback: DiscoverySessionCallback, handler: Handler?): Unit

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 DiscoverySessionCallback:

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 PublishDiscoverySession#updatePublish(PublishConfig).

An application must use the DiscoverySession#close() to terminate the publish discovery session once it isn't needed. This will free resources as well terminate any on-air transmissions.

The application must have the android.Manifest.permission#ACCESS_COARSE_LOCATION permission to start a publish discovery session.
Requires API level 26 (Android 8.0, Oreo)

Parameters
publishConfig PublishConfig: The PublishConfig specifying the configuration of the requested publish session. This value must never be null.
callback PublishConfig: A DiscoverySessionCallback derived object to be used for session event callbacks. This value must never be null.
handler PublishConfig: 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. This value may be null.

close

added in API level 26
open fun close(): Unit

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 WifiAwareManager#attach(AttachCallback, Handler) .
Requires API level 26 (Android 8.0, Oreo)

Exceptions
Exception if this resource cannot be closed

subscribe

added in API level 26
open fun subscribe(subscribeConfig: SubscribeConfig, callback: DiscoverySessionCallback, handler: Handler?): Unit

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 DiscoverySessionCallback:

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 SubscribeDiscoverySession#updateSubscribe(SubscribeConfig).

An application must use the DiscoverySession#close() to terminate the subscribe discovery session once it isn't needed. This will free resources as well terminate any on-air transmissions.

The application must have the android.Manifest.permission#ACCESS_COARSE_LOCATION permission to start a subscribe discovery session.
Requires API level 26 (Android 8.0, Oreo)

Parameters
subscribeConfig SubscribeConfig: The SubscribeConfig specifying the configuration of the requested subscribe session. This value must never be null.
callback SubscribeConfig: A DiscoverySessionCallback derived object to be used for session event callbacks. This value must never be null.
handler SubscribeConfig: 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. This value may be null.