Passer directement au contenu

Les plus consultés

Consultés récemment


Wi-Fi Aware

On some devices running Android 8.0 (API level 26) and higher, Wi-Fi Aware provides the ability to discover and connect directly to each other without any other type of connectivity between them, such as Wi-Fi Access Point or Cellular. Wi-Fi Aware is also known as Neighbor Awareness Networking or NAN.

Wi-Fi Aware networking works by forming clusters with neighboring devices, or by creating a new cluster if the device is the first one in an area. This clustering behavior is device-wide—apps have no control over clustering behavior—and the behavior is managed by the Wi-Fi Aware system service. Apps use the Wi-Fi Aware APIs to talk to the Wi-Fi Aware system service which, in turn, manages the Wi-Fi Aware hardware on the device.

The Wi-Fi Aware APIs are available to apps that target Android 8.0 (API level 26), and let apps perform the following operations:

Wi-Fi Aware network connections are more reliable than Wi-Fi P2P connections and support higher throughput rates across longer distances than Bluetooth connections. These types of connections are useful for apps that share data between users, such as photo-sharing apps.

Initial setup

To set up your app to use Wi-Fi Aware, perform the following steps:

  1. Request the following permissions in your app's manifest:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  2. Check if the device supports Wi-Fi Aware by calling the PackageManager as shown below:


  3. Check if Wi-Fi Aware is currently available. Wi-Fi Aware may exist on the device, but may not be currently available because the user has disabled Wi-Fi. Depending on their hardware and firmware capabilities, some devices may not support Wi-Fi Aware if Wi-Fi Direct, SoftAP, or tethering is in use. To check whether Wi-Fi Aware is currently available, call isAvailable().

    The availability of Wi-Fi Aware can change at any time. Your app should register a BroadcastReceiver to receive ACTION_WIFI_AWARE_STATE_CHANGED, which is sent whenever availability changes. When your app receives the broadcast intent, the app should check the current state of availability and adjust its behavior accordingly. For example:

    IntentFilter filter =
        new IntentFilter(WifiAwareManager.ACTION_WIFI_AWARE_STATE_CHANGED);
    BroadcastReceiver myReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (wifiAwareManager.isAvailable()) {
            } else {
    context.registerReceiver(myReceiver, filter);

    For more information, see Broadcasts.

Obtain a session

To start using Wi-Fi Aware, your app must obtain a WifiAwareSession by calling attach(). This method does the following:

If the app attaches successfully, the system executes the onAttached() callback. This callback provides a WifiAwareSession object that your app should use for all further session operations. An app can now use the session to publish a service or subscribe to a service.

Your app should call attach() only once. If you call attach() multiple times, your app receives different sessions, each with its own namespace. This could be useful in complex scenarios, but should generally be avoided.

Publish a service

To make a service discoverable, call the publish() method, which takes the following parameters:

Here's an example:

PublishConfig config = new PublishConfig.Builder()

mAwareSession.publish(config, new DiscoverySessionCallback() {
    public void onPublishStarted(PublishDiscoverySession session) {
    public void onMessageReceived(PeerHandle peerHandle, byte[] message) {
    }, null);

If publication succeeds, then the onPublishStarted() callback method is executed.

At this point, when devices running matching subscriber apps move into the Wi-Fi range of the publishing device, the subscribers discover the service. When a subscriber discovers a publisher, the publisher doesn't receive a notification. If the subscriber sends a message to the publisher, however, then the publisher receives a notification. When that happens, the onMessageReceived() callback method is executed. You can use the PeerHandle argument from this method to send a message back to the subscriber or create a connection to it.

To stop publishing the service, call DiscoverySession.close(). Discovery sessions are associated with their parent WifiAwareSession. If the parent session is closed, its associated discovery sessions are also closed. However, the system doesn't guarantee when out-of-scope sessions are closed, so we recommend that you manually call DiscoverySession.close().

Subscribe to a service

To subscribe to a service, call the subscribe() method, which takes the following parameters:

Here's an example:

SubscribeConfig config = new SubscribeConfig.Builder()

mAwareSession.subscribe(config, new DiscoverySessionCallback() {
    public void onSubscribeStarted(SubscribeDiscoverySession session) {

    public void onServiceDiscovered(PeerHandle peerHandle,
            byte[] serviceSpecificInfo, List<byte[]> matchFilter) {
}, null);

If the subscribe operation succeeds, the system executes the onSubscribeStarted() callback in your app. Because you can use the SubscribeDiscoverySession argument in the callback to communicate with a publisher after your app has discovered one, you should save this reference. You can update the description at any time by calling updateSubscribe() on the discovery session.

At this point, your subscription waits for matching publishers to come into Wi-Fi range. When this happens, the system executes the onServiceDiscovered() callback method. You can use the PeerHandle argument from this callback to send a message or create a connection to that publisher.

To stop subscribing to a service, call DiscoverySession.close(). Discovery sessions are associated with their parent WifiAwareSession. If the parent session is closed, its associated discovery sessions are also closed. However, the system doesn't guarantee when out-of-scope sessions are closed, so we recommend that you manually call DiscoverySession.close().

Send a message

To send a message to another device, you need the following objects:

To send a message, call sendMessage(). The following callbacks might then occur:

Create a connection

There are two ways to create a Wi-Fi Aware connection. The first way assumes that you have used Wi-Fi Aware to discover the other device and you have the other device's PeerHandle. The second way assumes that you have discovered the other device's MAC address through some other mechanism, such as Bluetooth or BLE. This is known as out-of-band discovery, or OOB.

Regardless of which method you choose, there are always two devices in a Wi-Fi Aware connection: an initiator and a responder. If you're using Wi-Fi Aware discovery, then the roles are fixed and don't need to be explicitly specified: the subscriber is the initiator and the publisher is the responder. If you are using out-of-band discovery, then the devices need to negotiate these roles on their own.

To create a connection, complete the following sequence of steps:

  1. Create a network specifier:

    The responder isn't required to provide a MAC address or a PeerHandle. If no MAC address or PeerHandle is specified, the device accepts all connection requests.

  2. Build a network request, setting the transport mechanism to TRANSPORT_WIFI_AWARE:

    NetworkRequest myNetworkRequest = new NetworkRequest.Builder()

  3. Call requestNetwork() and provide the following callback methods:

    mCallback = new ConnectivityManager.NetworkCallback() {
        public void onAvailable(Network network) {
        public void onLinkPropertiesChanged(Network network,
                LinkProperties linkProperties) {
        public void onLost(Network network) {
    mConnMgr.requestNetwork(networkRequest, mCallback);

    The appropriate callback methods are executed when the network connection is available, changed, or lost.

  4. When you're finished with the network connection, call unregisterNetworkCallback().

Ce site Web utilise des cookies pour enregistrer vos préférences relatives à la langue du site et aux options d'affichage.

Recevez les dernières actualités destinées aux développeurs Android, ainsi que des conseils qui vous mèneront vers le succès sur Google Play.

* Champs obligatoires

Super !

Suivez Google Developers sur WeChat

Consulter ce site en  ?

Vous avez demandé une page en , mais la langue indiquée dans vos préférences linguistiques pour ce site est la suivante : .

Souhaitez-vous modifier vos préférences linguistiques et parcourir ce site en  ? Pour modifier ultérieurement vos préférences linguistiques, utilisez le menu "Langue" au bas de chaque page.

Cette classe nécessite le niveau d'API  ou supérieur

Ce document est masqué, car vous avez sélectionné le niveau d'API  pour la documentation. Vous pouvez modifier le niveau d'API pour la documentation avec le sélecteur situé au-dessus du menu de navigation de gauche.

Pour découvrir comment déterminer le niveau d'API nécessaire pour votre application, consultez le guide sur la prise en charge des différentes versions de plate-forme (en anglais).

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)