WifiP2pManager.java revision 9cc2718abc0152d79e3e8bf23be94ddd3cc9db87
1/*
2 * Copyright (C) 2011 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
17package android.net.wifi.p2p;
18
19import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.content.Context;
22import android.net.ConnectivityManager;
23import android.net.IConnectivityManager;
24import android.os.Binder;
25import android.os.IBinder;
26import android.os.Handler;
27import android.os.Message;
28import android.os.RemoteException;
29import android.os.ServiceManager;
30import android.os.WorkSource;
31import android.os.Messenger;
32import android.util.Log;
33
34import com.android.internal.util.AsyncChannel;
35import com.android.internal.util.Protocol;
36
37/**
38 * This class provides the API for managing Wi-Fi peer-to-peer connectivity. This lets an
39 * application discover available peers, setup connection to peers and query for the list of peers.
40 * When a p2p connection is formed over wifi, the device continues to maintain the uplink
41 * connection over mobile or any other available network for internet connectivity on the device.
42 *
43 * <p> The API is asynchronous and response to a request from an application is sent in the form
44 * of a {@link android.os.Message} on a {@link android.os.Handler} that needs to be initialized
45 * by the application right at the beginning before any p2p operations are performed via
46 * {@link #initialize}.
47 *
48 * <p> An application can request for the current list of peers using {@link #requestPeers}. The
49 * {@link #RESPONSE_PEERS} message on the handler indicates that the peer list is available.
50 * Use {@link #peersInResponse} to extract the peer device list upon the receiving the
51 * {@link #RESPONSE_PEERS} message.
52 *
53 * <p> If an application needs to initiate a discovery, use {@link #discoverPeers} and listen
54 * to {@link #WIFI_P2P_PEERS_CHANGED_ACTION} intent action to initiate a request to fetch
55 * list of peers with {@link #requestPeers}. An initiated discovery request from an application
56 * stays active until the device starts connecting to a peer or forms a p2p group.
57 *
58 * <p> An application can initiate a connection request to a peer through {@link #connect}. See
59 * {@link WifiP2pConfig} for details on setting up the configuration. For communication with legacy
60 * Wi-Fi devices that do not support p2p, an app can create a group using {@link #createGroup}
61 * which creates an access point whose details can be fetched with {@link #requestGroupInfo}.
62 *
63 * <p> After a successful group formation through {@link #createGroup} or through {@link #connect},
64 * use {@link #requestConnectionInfo} to fetch the connection details. Connection information
65 * can be obtained with {@link #connectionInfoInResponse} on a {@link #RESPONSE_CONNECTION_INFO}
66 * message. The connection info {@link WifiP2pInfo} contains the address of the group owner
67 * {@link WifiP2pInfo#groupOwnerAddress} and a flag {@link #WifiP2pInfo#isGroupOwner} to indicate
68 * if the current device is a p2p group owner. A p2p client can thus communicate with
69 * the p2p group owner through a socket connection.
70 *
71 * <p> Android has no platform support for service discovery yet, so applications could
72 * run a service discovery protocol to discover services on the peer-to-peer netework.
73 *
74 * <p class="note"><strong>Note:</strong>
75 * Registering an application handler with {@link #initialize} requires the permissions
76 * {@link android.Manifest.permission#ACCESS_WIFI_STATE} and
77 * {@link android.Manifest.permission#CHANGE_WIFI_STATE} to perform any further peer-to-peer
78 * operations.
79 *
80 * Get an instance of this class by calling {@link android.content.Context#getSystemService(String)
81 * Context.getSystemService(Context.WIFI_P2P_SERVICE)}.
82 *
83 * {@see WifiP2pConfig}
84 * {@see WifiP2pInfo}
85 * {@see WifiP2pGroup}
86 * {@see WifiP2pDevice}
87 * {@see WifiP2pDeviceList}
88 * @hide
89 */
90public class WifiP2pManager {
91    /**
92     * Broadcast intent action to indicate whether Wi-Fi p2p is enabled or disabled. An
93     * extra {@link #EXTRA_WIFI_STATE} provides the state information as int.
94     *
95     * @see #EXTRA_WIFI_STATE
96     */
97    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
98    public static final String WIFI_P2P_STATE_CHANGED_ACTION =
99        "android.net.wifi.P2P_STATE_CHANGED";
100
101    /**
102     * The lookup key for an int that indicates whether Wi-Fi p2p is enabled or disabled.
103     * Retrieve it with {@link android.content.Intent#getIntExtra(String,int)}.
104     *
105     * @see #WIFI_P2P_STATE_DISABLED
106     * @see #WIFI_P2P_STATE_ENABLED
107     */
108    public static final String EXTRA_WIFI_STATE = "wifi_p2p_state";
109
110    /**
111     * Wi-Fi p2p is disabled.
112     *
113     * @see #WIFI_P2P_STATE_CHANGED_ACTION
114     */
115    public static final int WIFI_P2P_STATE_DISABLED = 1;
116
117    /**
118     * Wi-Fi p2p is enabled.
119     *
120     * @see #WIFI_P2P_STATE_CHANGED_ACTION
121     */
122    public static final int WIFI_P2P_STATE_ENABLED = 2;
123
124    /**
125     * Broadcast intent action indicating that the state of Wi-Fi p2p connectivity
126     * has changed. One extra {@link #EXTRA_WIFI_P2P_INFO} provides the p2p connection info in
127     * the form of a {@link WifiP2pInfo} object. Another extra {@link #EXTRA_NETWORK_INFO} provides
128     * the network info in the form of a {@link android.net.NetworkInfo}.
129     *
130     * @see #EXTRA_WIFI_P2P_INFO
131     * @see #EXTRA_NETWORK_INFO
132     */
133    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
134    public static final String WIFI_P2P_CONNECTION_CHANGED_ACTION =
135        "android.net.wifi.CONNECTION_STATE_CHANGE";
136
137    /**
138     * The lookup key for a {@link android.net.wifi.p2p.WifiP2pInfo} object
139     * Retrieve with {@link android.content.Intent#getParcelableExtra(String)}.
140     */
141    public static final String EXTRA_WIFI_P2P_INFO = "wifiP2pInfo";
142
143    /**
144     * The lookup key for a {@link android.net.NetworkInfo} object associated with the
145     * Wi-Fi network. Retrieve with
146     * {@link android.content.Intent#getParcelableExtra(String)}.
147     */
148    public static final String EXTRA_NETWORK_INFO = "networkInfo";
149
150    /**
151     * The lookup key for a {@link android.net.LinkProperties} object associated with the
152     * network. Retrieve with
153     * {@link android.content.Intent#getParcelableExtra(String)}.
154     * @hide
155     */
156    public static final String EXTRA_LINK_PROPERTIES = "linkProperties";
157
158    /**
159     * The lookup key for a {@link android.net.LinkCapabilities} object associated with the
160     * network. Retrieve with
161     * {@link android.content.Intent#getParcelableExtra(String)}.
162     * @hide
163     */
164    public static final String EXTRA_LINK_CAPABILITIES = "linkCapabilities";
165
166    /**
167     * Broadcast intent action indicating that the available peer list has changed. Fetch
168     * the changed list of peers with {@link #requestPeers}
169     */
170    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
171    public static final String WIFI_P2P_PEERS_CHANGED_ACTION =
172        "android.net.wifi.PEERS_CHANGED";
173
174    /**
175     * Activity Action: Pick a Wi-Fi p2p network to connect to.
176     * <p>Input: Nothing.
177     * <p>Output: Nothing.
178     * @hide
179     */
180    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
181    public static final String ACTION_PICK_WIFI_P2P_NETWORK =
182        "android.net.wifi.PICK_WIFI_P2P_NETWORK";
183
184    IWifiP2pManager mService;
185
186    /**
187     * Message {@link android.os.Message#what} sent on the application handler specified
188     * at {@link #initialize} indicating the asynchronous channel has disconnected. An
189     * application could choose to reconnect with {@link #initialize}
190     */
191    public static final int HANDLER_DISCONNECTION = AsyncChannel.CMD_CHANNEL_DISCONNECTED;
192
193    private static final int BASE = Protocol.BASE_WIFI_P2P_MANAGER;
194
195    /** @hide */
196    public static final int ENABLE_P2P                              = BASE + 1;
197    /** @hide */
198    public static final int ENABLE_P2P_FAILED                       = BASE + 2;
199    /** @hide */
200    public static final int ENABLE_P2P_SUCCEEDED                    = BASE + 3;
201
202    /** @hide */
203    public static final int DISABLE_P2P                             = BASE + 4;
204    /** @hide */
205    public static final int DISABLE_P2P_FAILED                      = BASE + 5;
206    /** @hide */
207    public static final int DISABLE_P2P_SUCCEEDED                   = BASE + 6;
208
209    /** @hide */
210    public static final int DISCOVER_PEERS                          = BASE + 7;
211
212    /**
213     * Message {@link android.os.Message#what} value indicating that the {@link #discoverPeers}
214     * operation failed.
215     * <p> The reason for failure could be one of {@link #P2P_UNSUPPORTED}, {@link #P2P_DISABLED}
216     * or {@link #ALREADY_IN_EFFECT}
217     */
218    public static final int DISCOVER_PEERS_FAILED                   = BASE + 8;
219    /**
220     * Message {@link android.os.Message#what} value indicating that the {@link #discoverPeers}
221     * operation succeeded.
222     * <p> The application can register for {@link #WIFI_P2P_PEERS_CHANGED_ACTION} intent
223     * to listen for changes in the peer list as a result of the discovery process.
224     */
225    public static final int DISCOVER_PEERS_SUCCEEDED                = BASE + 9;
226
227    /** @hide */
228    public static final int CONNECT                                 = BASE + 10;
229
230    /**
231     * Message {@link android.os.Message#what} value indicating that the {@link #connect}
232     * operation failed.
233     * <p> The reason for failure could be one of {@link #P2P_UNSUPPORTED}, {@link #P2P_DISABLED}
234     * or {@link #ALREADY_IN_EFFECT}
235     */
236    public static final int CONNECT_FAILED                          = BASE + 11;
237    /**
238     * Message {@link android.os.Message#what} value indicating that the {@link #connect}
239     * operation succeeded.
240     * <p> The application can register for {@link #WIFI_P2P_CONNECTION_CHANGED_ACTION} intent
241     * to listen for connectivity change as a result of the connect operation
242     */
243    public static final int CONNECT_SUCCEEDED                       = BASE + 12;
244
245    /** @hide */
246    public static final int CREATE_GROUP                            = BASE + 13;
247
248    /**
249     * Message {@link android.os.Message#what} value indicating that the {@link #createGroup}
250     * operation failed.
251     * <p> The reason for failure could be one of {@link #P2P_UNSUPPORTED}, {@link #P2P_DISABLED}
252     * or {@link #ALREADY_IN_EFFECT}
253     */
254    public static final int CREATE_GROUP_FAILED                     = BASE + 14;
255    /**
256     * Message {@link android.os.Message#what} value indicating that the {@link #createGroup}
257     * operation succeeded.
258     * <p> The application can request the group details with {@link #requestGroupInfo}
259     */
260    public static final int CREATE_GROUP_SUCCEEDED                  = BASE + 15;
261
262    /** @hide */
263    public static final int REMOVE_GROUP                            = BASE + 16;
264    /**
265     * Message {@link android.os.Message#what} value indicating that the {@link #removeGroup}
266     * operation failed.
267     * <p> The reason for failure could be one of {@link #P2P_UNSUPPORTED}, {@link #P2P_DISABLED}
268     * or {@link #ALREADY_IN_EFFECT}
269     */
270    public static final int REMOVE_GROUP_FAILED                     = BASE + 17;
271    /**
272     * Message {@link android.os.Message#what} value indicating that the {@link #removeGroup}
273     * operation succeeded.
274     */
275    public static final int REMOVE_GROUP_SUCCEEDED                  = BASE + 18;
276
277    /**
278     * Supported {@link android.os.Message#arg1} value on the following response messages:
279     * {@link #DISCOVER_PEERS_FAILED}, {@link #CONNECT_FAILED}, {@link #CREATE_GROUP_FAILED}
280     * and {@link #REMOVE_GROUP_FAILED}
281     *
282     * <p> This indicates that the reason for failure is because p2p is unsupported on the
283     * device
284     */
285    public static final int P2P_UNSUPPORTED     = 1;
286
287    /**
288     * Supported {@link android.os.Message#arg1} value on the following response messages:
289     * {@link #DISCOVER_PEERS_FAILED}, {@link #CONNECT_FAILED}, {@link #CREATE_GROUP_FAILED}
290     * and {@link #REMOVE_GROUP_FAILED}
291     *
292     * <p> This indicates that the reason for failure is because p2p is currently disabled
293     * by the user
294     */
295    public static final int P2P_DISABLED        = 2;
296
297    /**
298     * Supported {@link android.os.Message#arg1} value on the following response messages:
299     * {@link #DISCOVER_PEERS_FAILED}, {@link #CONNECT_FAILED}, {@link #CREATE_GROUP_FAILED}
300     * and {@link #REMOVE_GROUP_FAILED}
301     *
302     * <p> This indicates that the reason for failure is because the operation is already in
303     * effect
304     */
305    public static final int ALREADY_IN_EFFECT   = 3;
306
307
308    /** @hide */
309    public static final int REQUEST_PEERS                           = BASE + 19;
310    /**
311     * Message {@link android.os.Message#what} delivered on the application hander
312     * in response to a {@link #requestPeers} call from the application.
313     *
314     * <p> Extract a {@link WifiP2pDeviceList} object by calling {@link #peersInResponse}
315     * on the message object
316     */
317    public static final int RESPONSE_PEERS                          = BASE + 20;
318
319    /** @hide */
320    public static final int REQUEST_CONNECTION_INFO                 = BASE + 21;
321
322    /**
323     * Message {@link android.os.Message#what} delivered on the application hander
324     * in response to a {@link #requestConnectionInfo} call from the application.
325     *
326     * <p> Extract a {@link WifiP2pInfo} object by calling {@link #connectionInfoInResponse}
327     * on the message object
328     */
329    public static final int RESPONSE_CONNECTION_INFO                = BASE + 22;
330
331    /** @hide */
332    public static final int REQUEST_GROUP_INFO                      = BASE + 23;
333
334    /**
335     * Message {@link android.os.Message#what} delivered on the application hander
336     * in response to a {@link #requestGroupInfo} call from the application.
337     *
338     * <p> Extract a {@link WifiP2pGroup} object by calling {@link #groupInfoInResponse}
339     * on the message object
340     */
341
342    public static final int RESPONSE_GROUP_INFO                     = BASE + 24;
343
344    /** @hide */
345    public static final int WPS_PBC                                 = BASE + 23;
346    /** @hide */
347    public static final int WPS_PIN                                 = BASE + 24;
348    /** @hide */
349    public static final int WPS_PIN_AVAILABLE                       = BASE + 25;
350
351    /**
352     * Create a new WifiP2pManager instance. Applications use
353     * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
354     * the standard {@link android.content.Context#WIFI_P2P_SERVICE Context.WIFI_P2P_SERVICE}.
355     * @param service the Binder interface
356     * @param handler target for messages
357     * @hide - hide this because it takes in a parameter of type IWifiP2pManager, which
358     * is a system private class.
359     */
360    public WifiP2pManager(IWifiP2pManager service) {
361        mService = service;
362    }
363
364    /**
365     * A channel that connects the application handler to the Wifi framework.
366     * Most p2p operations require a Channel as an argument. An instance of Channel is obtained
367     * by doing a call on {@link #initialize}
368     */
369    public class Channel {
370        Channel(AsyncChannel c) {
371            mAsyncChannel = c;
372        }
373        AsyncChannel mAsyncChannel;
374    }
375
376    /**
377     * Registers the application handler with the Wi-Fi framework. This function
378     * must be the first to be called before any p2p operations are performed.
379     *
380     * <p class="note"><strong>Note:</strong>
381     * The handler registered with the framework should only handle messages
382     * with {@link android.os.Message#what} values defined in this file. Adding application
383     * specific private {@link android.os.Message#what} types should be done on a seperate handler
384     *
385     * @param srcContext is the context of the source
386     * @param srcHandler is the handler on which the source will receive message responses
387     * asynchronously
388     * @return Channel instance that is necessary for performing any further p2p operations
389     */
390    public Channel initialize(Context srcContext, Handler srcHandler) {
391        Messenger messenger = getMessenger();
392        if (messenger == null) return null;
393
394        AsyncChannel asyncChannel = new AsyncChannel();
395        Channel c = new Channel(asyncChannel);
396        if (asyncChannel.connectSync(srcContext, srcHandler, messenger)
397                == AsyncChannel.STATUS_SUCCESSFUL) {
398            return c;
399        } else {
400            return null;
401        }
402    }
403
404    /** @hide */
405    public boolean isP2pSupported() {
406        try {
407            return mService.isP2pSupported();
408        } catch (RemoteException e) {
409            return false;
410        }
411    }
412
413    /**
414     * Sends in a request to the system to enable p2p. This will pop up a dialog
415     * to the user and upon authorization will enable p2p.
416     * @hide
417     */
418    public void enableP2p(Channel c) {
419        if (c == null) return;
420        c.mAsyncChannel.sendMessage(ENABLE_P2P);
421    }
422
423    /**
424     * Sends in a request to the system to disable p2p. This will pop up a dialog
425     * to the user and upon authorization will enable p2p.
426     * @hide
427     */
428    public void disableP2p(Channel c) {
429        if (c == null) return;
430        c.mAsyncChannel.sendMessage(DISABLE_P2P);
431    }
432
433    /**
434     * Initiate peer discovery. A discovery process involves scanning for available Wi-Fi peers
435     * for the purpose of establishing a connection.
436     *
437     * <p> The function call immediately returns after sending a discovery request
438     * to the framework. The application handler is notified of a success or failure to initiate
439     * discovery with {@link #DISCOVER_PEERS_SUCCEEDED} or {@link #DISCOVER_PEERS_FAILED}.
440     *
441     * <p> The discovery remains active until a connection is initiated or
442     * a p2p group is formed. Register for {@link #WIFI_P2P_PEERS_CHANGED_ACTION} intent to
443     * determine when the framework notifies of a change as peers are discovered.
444     *
445     * <p> Upon receiving a {@link #WIFI_P2P_PEERS_CHANGED_ACTION} intent, an application
446     * can request for the list of peers using {@link #requestPeers} which will deliver a
447     * {@link #RESPONSE_PEERS} message on the application handler. The application can then
448     * extract a {@link WifiP2pDeviceList} object by calling {@link #peersInResponse}
449     * on the message.
450     */
451    public void discoverPeers(Channel c) {
452        if (c == null) return;
453        c.mAsyncChannel.sendMessage(DISCOVER_PEERS);
454    }
455
456    /**
457     * Start a p2p connection to a device with the specified configuration.
458     *
459     * <p> The function call immediately returns after sending a connection request
460     * to the framework. The application handler is notified of a success or failure to initiate
461     * connectivity with {@link #CONNECT_SUCCEEDED} or {@link #CONNECT_FAILED}.
462     *
463     * <p> Register for {@link #WIFI_P2P_CONNECTION_CHANGED_ACTION} intent to
464     * determine when the framework notifies of a change in connectivity.
465     *
466     * <p> If the current device is not part of a p2p group, a connect request initiates
467     * a group negotiation with the peer.
468     *
469     * <p> If the current device is part of an existing p2p group or has created
470     * a p2p group with {@link #createGroup}, an invitation to join the group is sent to
471     * the peer device.
472     *
473     * @param config options as described in {@link WifiP2pConfig} class.
474     */
475    public void connect(Channel c, WifiP2pConfig config) {
476        if (c == null) return;
477        c.mAsyncChannel.sendMessage(CONNECT, config);
478    }
479
480    /**
481     * Create a p2p group with the current device as the group owner. This essentially creates
482     * an access point that can accept connections from legacy clients as well as other p2p
483     * devices.
484     * <p> For p2p operation, this would normally not be used unless the current device needs
485     * to form a p2p connection with a legacy client
486     *
487     * <p> The function call immediately returns after sending a group creation request
488     * to the framework. The application handler is notified of a success or failure to create
489     * group with {@link #CREATE_GROUP_SUCCEEDED} or {@link #CREATE_GROUP_FAILED}.
490     *
491     * <p> Application can request for the group details with {@link #requestGroupInfo} which will
492     * deliver a {@link #RESPONSE_GROUP_INFO} message on the application handler. The application
493     * can then extract a {@link WifiP2pGroup} object by calling {@link #groupInfoInResponse}
494     * on the message.
495     */
496    public void createGroup(Channel c) {
497        if (c == null) return;
498        c.mAsyncChannel.sendMessage(CREATE_GROUP);
499    }
500
501    /**
502     * Remove the current p2p group.
503     *
504     * <p> The function call immediately returns after sending a group removal request
505     * to the framework. The application handler is notified of a success or failure to remove
506     * a group with {@link #REMOVE_GROUP_SUCCEEDED} or {@link #REMOVE_GROUP_FAILED}.
507     */
508    public void removeGroup(Channel c) {
509        if (c == null) return;
510        c.mAsyncChannel.sendMessage(REMOVE_GROUP);
511    }
512
513    /**
514     * Request the current list of peers. This returns a {@link #RESPONSE_PEERS} on the application
515     * handler. The {@link #RESPONSE_PEERS} message on the handler indicates that the peer list is
516     * available. Use {@link #peersInResponse} to extract {@link WifiP2pDeviceList} from the message
517     */
518    public void requestPeers(Channel c) {
519        if (c == null) return;
520        c.mAsyncChannel.sendMessage(REQUEST_PEERS);
521    }
522
523    /**
524     * Upon receiving a {@link #RESPONSE_PEERS} on the application handler, an application
525     * can extract the peer device list using this function.
526     */
527    public WifiP2pDeviceList peersInResponse(Message msg) {
528        return (WifiP2pDeviceList) msg.obj;
529    }
530
531    /**
532     * Request device connection info. This returns a {@link #RESPONSE_CONNECTION_INFO} on
533     * the application handler. The {@link #RESPONSE_CONNECTION_INFO} message on the handler
534     * indicates that connection info is available. Use {@link #connectionInfoInResponse} to
535     * extract {@link WifiP2pInfo} from the message.
536     */
537    public void requestConnectionInfo(Channel c) {
538        if (c == null) return;
539        c.mAsyncChannel.sendMessage(REQUEST_CONNECTION_INFO);
540    }
541
542    /**
543     * Upon receiving a {@link #RESPONSE_CONNECTION_INFO} on the application handler, an application
544     * can extract the connection info using this function.
545     */
546    public WifiP2pInfo connectionInfoInResponse(Message msg) {
547        return (WifiP2pInfo) msg.obj;
548    }
549
550    /**
551     * Request p2p group info. This returns a {@link #RESPONSE_GROUP_INFO} on
552     * the application handler. The {@link #RESPONSE_GROUP_INFO} message on the handler
553     * indicates that group info is available. Use {@link #groupInfoInResponse} to
554     * extract {@link WifiP2pGroup} from the message.
555     */
556    public void requestGroupInfo(Channel c) {
557        if (c == null) return;
558        c.mAsyncChannel.sendMessage(REQUEST_GROUP_INFO);
559    }
560
561    /**
562     * Upon receiving a {@link #RESPONSE_GROUP_INFO} on the application handler, an application
563     * can extract the group info using this function.
564     */
565    public WifiP2pGroup groupInfoInResponse(Message msg) {
566        return (WifiP2pGroup) msg.obj;
567    }
568
569    /**
570     * Get a reference to WifiP2pService handler. This is used to establish
571     * an AsyncChannel communication with WifiService
572     *
573     * @return Messenger pointing to the WifiP2pService handler
574     * @hide
575     */
576    public Messenger getMessenger() {
577        try {
578            return mService.getMessenger();
579        } catch (RemoteException e) {
580            return null;
581        }
582    }
583
584    /**
585     * Setup DNS connectivity on the current process to the connected Wi-Fi p2p peers
586     *
587     * @return -1 on failure
588     * @hide
589     */
590    public int startPeerCommunication() {
591        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
592        IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
593        try {
594            return cm.startUsingNetworkFeature(ConnectivityManager.TYPE_WIFI, "p2p", new Binder());
595        } catch (RemoteException e) {
596            return -1;
597        }
598    }
599
600    /**
601     * Tear down connectivity to the connected Wi-Fi p2p peers
602     *
603     * @return -1 on failure
604     * @hide
605     */
606    public int stopPeerCommunication() {
607        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
608        IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
609        try {
610            return cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_WIFI, "p2p");
611        } catch (RemoteException e) {
612            return -1;
613        }
614    }
615
616}
617