Skip to content

Commit c111d1c

Browse files
committed
Add discovery broadcasts for p2p
This will allow apps to figure out if discovery is active or not and based on that initiate a new discovery for fresh connections Change-Id: I4778f135fdd88773e4f0d50c384f9b6ebf561e6d
1 parent 61472a8 commit c111d1c

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

api/current.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12732,12 +12732,16 @@ package android.net.wifi.p2p {
1273212732
method public void requestPeers(android.net.wifi.p2p.WifiP2pManager.Channel, android.net.wifi.p2p.WifiP2pManager.PeerListListener);
1273312733
field public static final int BUSY = 2; // 0x2
1273412734
field public static final int ERROR = 0; // 0x0
12735+
field public static final java.lang.String EXTRA_DISCOVERY_STATE = "discoveryState";
1273512736
field public static final java.lang.String EXTRA_NETWORK_INFO = "networkInfo";
1273612737
field public static final java.lang.String EXTRA_WIFI_P2P_DEVICE = "wifiP2pDevice";
1273712738
field public static final java.lang.String EXTRA_WIFI_P2P_INFO = "wifiP2pInfo";
1273812739
field public static final java.lang.String EXTRA_WIFI_STATE = "wifi_p2p_state";
1273912740
field public static final int P2P_UNSUPPORTED = 1; // 0x1
1274012741
field public static final java.lang.String WIFI_P2P_CONNECTION_CHANGED_ACTION = "android.net.wifi.p2p.CONNECTION_STATE_CHANGE";
12742+
field public static final java.lang.String WIFI_P2P_DISCOVERY_CHANGED_ACTION = "android.net.wifi.p2p.DISCOVERY_STATE_CHANGE";
12743+
field public static final int WIFI_P2P_DISCOVERY_STARTED = 2; // 0x2
12744+
field public static final int WIFI_P2P_DISCOVERY_STOPPED = 1; // 0x1
1274112745
field public static final java.lang.String WIFI_P2P_PEERS_CHANGED_ACTION = "android.net.wifi.p2p.PEERS_CHANGED";
1274212746
field public static final java.lang.String WIFI_P2P_STATE_CHANGED_ACTION = "android.net.wifi.p2p.STATE_CHANGED";
1274312747
field public static final int WIFI_P2P_STATE_DISABLED = 1; // 0x1

wifi/java/android/net/wifi/WifiMonitor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public class WifiMonitor {
166166
/* P2P-DEVICE-LOST p2p_dev_addr=42:fc:89:e1:e2:27 */
167167
private static final String P2P_DEVICE_LOST_STR = "P2P-DEVICE-LOST";
168168

169+
/* P2P-FIND-STOPPED */
170+
private static final String P2P_FIND_STOPPED_STR = "P2P-FIND-STOPPED";
171+
169172
/* P2P-GO-NEG-REQUEST 42:fc:89:a8:96:09 dev_passwd_id=4 */
170173
private static final String P2P_GO_NEG_REQUEST_STR = "P2P-GO-NEG-REQUEST";
171174

@@ -264,6 +267,7 @@ public class WifiMonitor {
264267
public static final int P2P_PROV_DISC_PBC_RSP_EVENT = BASE + 34;
265268
public static final int P2P_PROV_DISC_ENTER_PIN_EVENT = BASE + 35;
266269
public static final int P2P_PROV_DISC_SHOW_PIN_EVENT = BASE + 36;
270+
public static final int P2P_FIND_STOPPED_EVENT = BASE + 37;
267271

268272
/* hostap events */
269273
public static final int AP_STA_DISCONNECTED_EVENT = BASE + 41;
@@ -516,6 +520,8 @@ private void handleP2pEvents(String dataString) {
516520
mStateMachine.sendMessage(P2P_DEVICE_FOUND_EVENT, new WifiP2pDevice(dataString));
517521
} else if (dataString.startsWith(P2P_DEVICE_LOST_STR)) {
518522
mStateMachine.sendMessage(P2P_DEVICE_LOST_EVENT, new WifiP2pDevice(dataString));
523+
} else if (dataString.startsWith(P2P_FIND_STOPPED_STR)) {
524+
mStateMachine.sendMessage(P2P_FIND_STOPPED_EVENT);
519525
} else if (dataString.startsWith(P2P_GO_NEG_REQUEST_STR)) {
520526
mStateMachine.sendMessage(P2P_GO_NEGOTIATION_REQUEST_EVENT,
521527
new WifiP2pConfig(dataString));

wifi/java/android/net/wifi/p2p/WifiP2pManager.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,41 @@ public class WifiP2pManager {
181181
public static final String WIFI_P2P_PEERS_CHANGED_ACTION =
182182
"android.net.wifi.p2p.PEERS_CHANGED";
183183

184+
/**
185+
* Broadcast intent action indicating that peer discovery has either started or stopped.
186+
* One extra {@link #EXTRA_DISCOVERY_STATE} indicates whether discovery has started
187+
* or stopped.
188+
*
189+
* Note that discovery will be stopped during a connection setup. If the application tries
190+
* to re-initiate discovery during this time, it can fail.
191+
*/
192+
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
193+
public static final String WIFI_P2P_DISCOVERY_CHANGED_ACTION =
194+
"android.net.wifi.p2p.DISCOVERY_STATE_CHANGE";
195+
196+
/**
197+
* The lookup key for an int that indicates whether p2p discovery has started or stopped.
198+
* Retrieve it with {@link android.content.Intent#getIntExtra(String,int)}.
199+
*
200+
* @see #WIFI_P2P_DISCOVERY_STARTED
201+
* @see #WIFI_P2P_DISCOVERY_STOPPED
202+
*/
203+
public static final String EXTRA_DISCOVERY_STATE = "discoveryState";
204+
205+
/**
206+
* p2p discovery has stopped
207+
*
208+
* @see #WIFI_P2P_DISCOVERY_CHANGED_ACTION
209+
*/
210+
public static final int WIFI_P2P_DISCOVERY_STOPPED = 1;
211+
212+
/**
213+
* p2p discovery has started
214+
*
215+
* @see #WIFI_P2P_DISCOVERY_CHANGED_ACTION
216+
*/
217+
public static final int WIFI_P2P_DISCOVERY_STARTED = 2;
218+
184219
/**
185220
* Broadcast intent action indicating that this device details have changed.
186221
*/

wifi/java/android/net/wifi/p2p/WifiP2pService.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
140140
* is invoked */
141141
private boolean mPersistGroup;
142142

143+
/* Track whether we are in p2p discovery. This is used to avoid sending duplicate
144+
* broadcasts
145+
*/
146+
private boolean mDiscoveryStarted;
147+
143148
private NetworkInfo mNetworkInfo;
144149

145150
/* Is chosen as a unique range to avoid conflict with
@@ -489,11 +494,15 @@ public boolean processMessage(Message message) {
489494
case WifiP2pManager.DISCOVER_PEERS:
490495
if (mWifiNative.p2pFind(DISCOVER_TIMEOUT_S)) {
491496
replyToMessage(message, WifiP2pManager.DISCOVER_PEERS_SUCCEEDED);
497+
sendP2pDiscoveryChangedBroadcast(true);
492498
} else {
493499
replyToMessage(message, WifiP2pManager.DISCOVER_PEERS_FAILED,
494500
WifiP2pManager.ERROR);
495501
}
496502
break;
503+
case WifiMonitor.P2P_FIND_STOPPED_EVENT:
504+
sendP2pDiscoveryChangedBroadcast(false);
505+
break;
497506
case WifiP2pManager.STOP_DISCOVERY:
498507
if (mWifiNative.p2pStopFind()) {
499508
replyToMessage(message, WifiP2pManager.STOP_DISCOVERY_SUCCEEDED);
@@ -1030,6 +1039,20 @@ private void sendP2pStateChangedBroadcast(boolean enabled) {
10301039
mContext.sendStickyBroadcast(intent);
10311040
}
10321041

1042+
private void sendP2pDiscoveryChangedBroadcast(boolean started) {
1043+
if (mDiscoveryStarted == started) return;
1044+
mDiscoveryStarted = started;
1045+
1046+
if (DBG) logd("discovery change broadcast " + started);
1047+
1048+
final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION);
1049+
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1050+
intent.putExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE, started ?
1051+
WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED :
1052+
WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED);
1053+
mContext.sendStickyBroadcast(intent);
1054+
}
1055+
10331056
private void sendThisDeviceChangedBroadcast() {
10341057
final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
10351058
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);

0 commit comments

Comments
 (0)