• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.wifi.aware;
18 
19 import static com.android.ranging.flags.Flags.FLAG_RANGING_RTT_ENABLED;
20 
21 import android.annotation.FlaggedApi;
22 import android.annotation.NonNull;
23 import android.annotation.SystemApi;
24 import android.net.wifi.rtt.RangingResult;
25 
26 import java.util.List;
27 
28 /**
29  * Base class for Aware session events callbacks. Should be extended by
30  * applications wanting notifications. The callbacks are set when a
31  * publish or subscribe session is created using
32  * {@link WifiAwareSession#publish(PublishConfig, DiscoverySessionCallback,
33  * android.os.Handler)} or
34  * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback,
35  * android.os.Handler)}.
36  * <p>
37  * A single callback is set at session creation - it cannot be replaced.
38  */
39 public class DiscoverySessionCallback {
40     /**
41      * Called when a publish operation is started successfully in response to a
42      * {@link WifiAwareSession#publish(PublishConfig, DiscoverySessionCallback,
43      * android.os.Handler)} operation.
44      *
45      * @param session The {@link PublishDiscoverySession} used to control the
46      *            discovery session.
47      */
onPublishStarted(@onNull PublishDiscoverySession session)48     public void onPublishStarted(@NonNull PublishDiscoverySession session) {
49         /* empty */
50     }
51 
52     /**
53      * Called when a subscribe operation is started successfully in response to a
54      * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback,
55      * android.os.Handler)} operation.
56      *
57      * @param session The {@link SubscribeDiscoverySession} used to control the
58      *            discovery session.
59      */
onSubscribeStarted(@onNull SubscribeDiscoverySession session)60     public void onSubscribeStarted(@NonNull SubscribeDiscoverySession session) {
61         /* empty */
62     }
63 
64     /**
65      * Called when a publish or subscribe discovery session configuration update request
66      * succeeds. Called in response to
67      * {@link PublishDiscoverySession#updatePublish(PublishConfig)} or
68      * {@link SubscribeDiscoverySession#updateSubscribe(SubscribeConfig)}.
69      */
onSessionConfigUpdated()70     public void onSessionConfigUpdated() {
71         /* empty */
72     }
73 
74     /**
75      * Called when a publish or subscribe discovery session cannot be created:
76      * {@link WifiAwareSession#publish(PublishConfig, DiscoverySessionCallback,
77      * android.os.Handler)} or
78      * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback,
79      * android.os.Handler)}, or when a configuration update fails:
80      * {@link PublishDiscoverySession#updatePublish(PublishConfig)} or
81      * {@link SubscribeDiscoverySession#updateSubscribe(SubscribeConfig)}.
82      * <p>
83      *     For discovery session updates failure leaves the session running with its previous
84      *     configuration - the discovery session is not terminated.
85      */
onSessionConfigFailed()86     public void onSessionConfigFailed() {
87         /* empty */
88     }
89 
90     /**
91      * Called when a discovery session (publish or subscribe) terminates. Termination may be due
92      * to user-request (either directly through {@link DiscoverySession#close()} or
93      * application-specified expiration, e.g. {@link PublishConfig.Builder#setTtlSec(int)}
94      * or {@link SubscribeConfig.Builder#setTtlSec(int)}).
95      */
onSessionTerminated()96     public void onSessionTerminated() {
97         /* empty */
98     }
99 
100     /**
101      * Called when a discovery session (publish or subscribe) has been suspended successfully.
102      * Suspension is triggered by {@link DiscoverySession#suspend()}.
103      * @hide
104      */
105     @SystemApi
onSessionSuspendSucceeded()106     public void onSessionSuspendSucceeded() {
107         /* empty */
108     }
109 
110     /**
111      * Called when a discovery session (publish or subscribe) has failed to be suspended
112      * successfully. Suspension is triggered by {@link DiscoverySession#suspend()}.
113      *
114      * @param reason One of:
115      *      {@link WifiAwareManager#WIFI_AWARE_SUSPEND_REDUNDANT_REQUEST},
116      *      {@link WifiAwareManager#WIFI_AWARE_SUSPEND_INVALID_SESSION},
117      *      {@link WifiAwareManager#WIFI_AWARE_SUSPEND_CANNOT_SUSPEND},
118      *      {@link WifiAwareManager#WIFI_AWARE_SUSPEND_INTERNAL_ERROR}
119      * @hide
120      */
121     @SystemApi
onSessionSuspendFailed( @ifiAwareManager.SessionSuspensionFailedReasonCode int reason)122     public void onSessionSuspendFailed(
123             @WifiAwareManager.SessionSuspensionFailedReasonCode int reason) {
124         /* empty */
125     }
126 
127     /**
128      * Called when a discovery session (publish or subscribe) has been resumed from suspension
129      * successfully. Resumption is triggered by {@link DiscoverySession#resume()}.
130      * @hide
131      */
132     @SystemApi
onSessionResumeSucceeded()133     public void onSessionResumeSucceeded() {
134         /* empty */
135     }
136 
137     /**
138      * Called when a discovery session (publish or subscribe) has failed to be resumed from
139      * suspension successfully. Resumption is triggered by {@link DiscoverySession#resume()}.
140      *
141      * @param reason One of:
142      *      {@link WifiAwareManager#WIFI_AWARE_RESUME_REDUNDANT_REQUEST},
143      *      {@link WifiAwareManager#WIFI_AWARE_RESUME_INVALID_SESSION},
144      *      {@link WifiAwareManager#WIFI_AWARE_RESUME_INTERNAL_ERROR}
145      * @hide
146      */
147     @SystemApi
onSessionResumeFailed( @ifiAwareManager.SessionResumptionFailedReasonCode int reason)148     public void onSessionResumeFailed(
149             @WifiAwareManager.SessionResumptionFailedReasonCode int reason) {
150         /* empty */
151     }
152 
153     /**
154      * Called when a subscribe operation results in a service discovery.
155      * <p>
156      * Note that this method and
157      * {@link #onServiceDiscoveredWithinRange(PeerHandle, byte[], List, int)} may be called
158      * multiple times per service discovery.
159      * <p>
160      * Note: This method is superseded by {@link #onServiceDiscovered(ServiceDiscoveryInfo)} which
161      * returns more information. Note that both legacy and new callback will be triggered on
162      * discovery.
163      *
164      * @param peerHandle An opaque handle to the peer matching our discovery operation.
165      * @param serviceSpecificInfo The service specific information (arbitrary
166      *            byte array) provided by the peer as part of its discovery
167      *            configuration.
168      * @param matchFilter The filter which resulted in this service discovery. For
169      * {@link PublishConfig#PUBLISH_TYPE_UNSOLICITED},
170      * {@link SubscribeConfig#SUBSCRIBE_TYPE_PASSIVE} discovery sessions this is the publisher's
171      *                    match filter. For {@link PublishConfig#PUBLISH_TYPE_SOLICITED},
172      *                    {@link SubscribeConfig#SUBSCRIBE_TYPE_ACTIVE} discovery sessions this
173      *                    is the subscriber's match filter.
174      */
onServiceDiscovered(PeerHandle peerHandle, byte[] serviceSpecificInfo, List<byte[]> matchFilter)175     public void onServiceDiscovered(PeerHandle peerHandle,
176             byte[] serviceSpecificInfo, List<byte[]> matchFilter) {
177         /* empty */
178     }
179 
180     /**
181      * Called when a subscribe operation results in a service discovery.
182      * <p>
183      * Note: This method supersedes {@link #onServiceDiscovered(PeerHandle, byte[], List)} and
184      * provides additional information - including cipher suite type and security context of the
185      * peer. Both the legacy and the new callback will be triggered on discovery.
186      *
187      * @param info A {@link ServiceDiscoveryInfo} structure containing information on the discovery
188      *             session and the discovered peer.
189      */
onServiceDiscovered(@onNull ServiceDiscoveryInfo info)190     public void onServiceDiscovered(@NonNull ServiceDiscoveryInfo info) {
191         /* empty */
192     }
193 
194     /**
195      * Called when a subscribe operation results in a
196      * service discovery. Called when a Subscribe service was configured with a range requirement
197      * {@link SubscribeConfig.Builder#setMinDistanceMm(int)} and/or
198      * {@link SubscribeConfig.Builder#setMaxDistanceMm(int)} and the Publish service was configured
199      * with {@link PublishConfig.Builder#setRangingEnabled(boolean)}.
200      * <p>
201      * If either Publisher or Subscriber does not enable Ranging, or if Ranging is temporarily
202      * disabled by the underlying device, service discovery proceeds without ranging and the
203      * {@link #onServiceDiscovered(PeerHandle, byte[], List)} is called.
204      * <p>
205      * Note that this method and {@link #onServiceDiscovered(PeerHandle, byte[], List)} may be
206      * called multiple times per service discovery.
207      * <p>
208      * Note: This method is superseded by
209      * {@link #onServiceDiscoveredWithinRange(ServiceDiscoveryInfo, int)} which returns more
210      * information. Note that both legacy and new callback will be triggered on discovery.
211      *
212      * @param peerHandle An opaque handle to the peer matching our discovery operation.
213      * @param serviceSpecificInfo The service specific information (arbitrary
214      *            byte array) provided by the peer as part of its discovery
215      *            configuration.
216      * @param matchFilter The filter which resulted in this service discovery. For
217      * {@link PublishConfig#PUBLISH_TYPE_UNSOLICITED},
218      * {@link SubscribeConfig#SUBSCRIBE_TYPE_PASSIVE} discovery sessions this is the publisher's
219      *                    match filter. For {@link PublishConfig#PUBLISH_TYPE_SOLICITED},
220      *                    {@link SubscribeConfig#SUBSCRIBE_TYPE_ACTIVE} discovery sessions this
221      *                    is the subscriber's match filter.
222      * @param distanceMm The measured distance to the Publisher in mm. Note: the measured distance
223      *                   may be negative for very close devices.
224      */
onServiceDiscoveredWithinRange(PeerHandle peerHandle, byte[] serviceSpecificInfo, List<byte[]> matchFilter, int distanceMm)225     public void onServiceDiscoveredWithinRange(PeerHandle peerHandle,
226         byte[] serviceSpecificInfo, List<byte[]> matchFilter, int distanceMm) {
227         /* empty */
228     }
229 
230     /**
231      * Called when a subscribe operation results in a
232      * service discovery. Called when a Subscribe service was configured with a range requirement
233      * {@link SubscribeConfig.Builder#setMinDistanceMm(int)} and/or
234      * {@link SubscribeConfig.Builder#setMaxDistanceMm(int)} and the Publish service was configured
235      * with {@link PublishConfig.Builder#setRangingEnabled(boolean)}.
236      * <p>
237      * If either Publisher or Subscriber does not enable Ranging, or if Ranging is temporarily
238      * disabled by the underlying device, service discovery proceeds without ranging and the
239      * {@link #onServiceDiscovered(PeerHandle, byte[], List)} is called.
240      * <p>
241      * Note: This method supersedes
242      * {@link #onServiceDiscoveredWithinRange(PeerHandle, byte[], List, int)} and provides
243      * additional information - including cipher suite type and security context of the peer. Both
244      * the legacy and the new callback will be triggered on discovery.
245      *
246      * @param info A {@link ServiceDiscoveryInfo} which indicate service config of the descovery
247      *             sessions.
248      * @param distanceMm The measured distance to the Publisher in mm. Note: the measured distance
249  *                   may be negative for very close devices.
250      */
onServiceDiscoveredWithinRange(@onNull ServiceDiscoveryInfo info, int distanceMm)251     public void onServiceDiscoveredWithinRange(@NonNull ServiceDiscoveryInfo info, int distanceMm) {
252         /* empty */
253     }
254 
255     /**
256      * Called in response to
257      * {@link DiscoverySession#sendMessage(PeerHandle, int, byte[])}
258      * when a message is transmitted successfully - i.e. when it was received successfully by the
259      * peer (corresponds to an ACK being received).
260      * <p>
261      * Note that either this callback or
262      * {@link DiscoverySessionCallback#onMessageSendFailed(int)} will be
263      * received - never both.
264      *
265      * @param messageId The arbitrary message ID specified when sending the message.
266      */
onMessageSendSucceeded(@uppressWarnings"unused") int messageId)267     public void onMessageSendSucceeded(@SuppressWarnings("unused") int messageId) {
268         /* empty */
269     }
270 
271     /**
272      * Called when message transmission initiated with
273      * {@link DiscoverySession#sendMessage(PeerHandle, int, byte[])} fails. E.g. when no ACK is
274      * received from the peer.
275      * <p>
276      * Note that either this callback or
277      * {@link DiscoverySessionCallback#onMessageSendSucceeded(int)} will be received
278      * - never both.
279      *
280      * @param messageId The arbitrary message ID specified when sending the message.
281      */
onMessageSendFailed(@uppressWarnings"unused") int messageId)282     public void onMessageSendFailed(@SuppressWarnings("unused") int messageId) {
283         /* empty */
284     }
285 
286     /**
287      * Called when a message is received from a discovery session peer - in response to the
288      * peer's {@link DiscoverySession#sendMessage(PeerHandle, int, byte[])}.
289      *
290      * @param peerHandle An opaque handle to the peer matching our discovery operation.
291      * @param message A byte array containing the message.
292      */
onMessageReceived(PeerHandle peerHandle, byte[] message)293     public void onMessageReceived(PeerHandle peerHandle, byte[] message) {
294         /* empty */
295     }
296 
297     /**
298      * Called when the discovered service is not available. All further operations on this
299      * discovery session will fail. If the service is available again,
300      * {@link #onServiceDiscovered(PeerHandle, byte[], List)} or
301      * {@link #onServiceDiscoveredWithinRange(PeerHandle, byte[], List, int)} will be called.
302      *
303      * @param peerHandle An opaque handle to the peer matching our discovery operation.
304      * @param reason Discovered service lost reason code. One of
305      *               {@link WifiAwareManager#WIFI_AWARE_DISCOVERY_LOST_REASON_PEER_NOT_VISIBLE},
306      *               {@link WifiAwareManager#WIFI_AWARE_DISCOVERY_LOST_REASON_UNKNOWN
307      */
onServiceLost(@onNull PeerHandle peerHandle, @WifiAwareManager.DiscoveryLostReasonCode int reason)308     public void onServiceLost(@NonNull PeerHandle peerHandle,
309             @WifiAwareManager.DiscoveryLostReasonCode int reason) {
310         /* empty */
311     }
312 
313     /**
314      * Callback indicating that a pairing request is received from peer.
315      *
316      * @param peerHandle The peer's handle where the request is from
317      * @param requestId The ID of the Aware pairing session
318      */
onPairingSetupRequestReceived(@onNull PeerHandle peerHandle, int requestId)319     public void onPairingSetupRequestReceived(@NonNull PeerHandle peerHandle, int requestId) {
320 
321     }
322 
323     /**
324      * Callback indicating that a pairing setup process succeeded.
325      *
326      * @param peerHandle The pairing peer handle.
327      * @param alias      This is the paired device alias set by the caller.
328      *                   {@link DiscoverySession#initiatePairingRequest(PeerHandle, String, int, String)}
329      *                   or
330      *                   {@link DiscoverySession#acceptPairingRequest(int, PeerHandle, String, int, String)}
331      */
onPairingSetupSucceeded(@onNull PeerHandle peerHandle, @NonNull String alias)332     public void onPairingSetupSucceeded(@NonNull PeerHandle peerHandle,
333             @NonNull String alias) {
334 
335     }
336 
337     /**
338      * Callback indicating that a pairing setup process failed.
339      *
340      * @param peerHandle The pairing peer handle.
341      */
onPairingSetupFailed(@onNull PeerHandle peerHandle)342     public void onPairingSetupFailed(@NonNull PeerHandle peerHandle) {
343 
344     }
345 
346     /**
347      * Callback indicating that a pairing verification process succeeded.
348      *
349      * @param peerHandle The pairing peer handle
350      * @param alias      This is the paired device alias set by the caller.
351      *                   {@link DiscoverySession#initiatePairingRequest(PeerHandle, String, int, String)}
352      *                   or
353      *                   {@link DiscoverySession#acceptPairingRequest(int, PeerHandle, String, int, String)}
354      */
onPairingVerificationSucceed(@onNull PeerHandle peerHandle, @NonNull String alias)355     public void onPairingVerificationSucceed(@NonNull PeerHandle peerHandle,
356             @NonNull String alias){
357 
358     }
359 
360     /**
361      * Callback indicating that a pairing verification process failed.
362      *
363      * @param peerHandle The pairing peer handle
364      */
onPairingVerificationFailed(@onNull PeerHandle peerHandle)365     public void onPairingVerificationFailed(@NonNull PeerHandle peerHandle) {
366 
367     }
368 
369     /**
370      * Callback indicating that a Bootstrapping method negotiation succeeded.
371      * The follow-up out-of-band bootstrapping can start
372      *
373      * @param peerHandle The bootstrapping peer handle
374      * @param method     The bootstrapping method accept by the peer
375      */
onBootstrappingSucceeded(@onNull PeerHandle peerHandle, @AwarePairingConfig.BootstrappingMethod int method)376     public void onBootstrappingSucceeded(@NonNull PeerHandle peerHandle,
377             @AwarePairingConfig.BootstrappingMethod int method){
378 
379     }
380 
381     /**
382      * Callback indicating that a Bootstrapping method negotiation failed.
383      *
384      * @param peerHandle The bootstrapping peer handle
385      */
onBootstrappingFailed(@onNull PeerHandle peerHandle)386     public void onBootstrappingFailed(@NonNull PeerHandle peerHandle) {
387 
388     }
389 
390     /**
391      * Callback indicating that ranging results have been received.
392      *
393      * @param rangingResults List of range measurements.
394      * @hide
395      */
396     @FlaggedApi(FLAG_RANGING_RTT_ENABLED)
397     @SystemApi
onRangingResultsReceived(@onNull List<RangingResult> rangingResults)398     public void onRangingResultsReceived(@NonNull List<RangingResult> rangingResults) {
399 
400     }
401 }
402