Code example for WifiP2pManager

Methods: discoverPeers, discoverServices, requestGroupInfo, setDnsSdResponseListeners

0
	} 
	 
	private void startDiscovery() { 
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
			registerServiceRequest(); 
			wifiMgr.setDnsSdResponseListeners(channel, discoveryListener, null);
			wifiMgr.discoverServices(channel, new TaggedActionListener("discoverService"));
		} else { 
			wifiMgr.discoverPeers(channel, new TaggedActionListener("discoverPeers"));
		} 
	} 
	 
	private void stopDiscovery() { 
		Log.i(TAG, "stop wifi p2p discovery");
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
			wifiMgr.stopPeerDiscovery(channel, new TaggedActionListener("stopPeerDiscovery"));
		} 
	} 
	 
	public void registerService(String serviceName, String serviceType) {
		if (!isEnabled)
			return; 
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && !isLocalServiceRegistered) {
			Log.d(TAG, "Register service "+ serviceName + " type " + serviceType);
			WifiP2pDnsSdServiceInfo servInfo = WifiP2pDnsSdServiceInfo.newInstance(serviceName, serviceType , null);
			wifiMgr.addLocalService(channel, servInfo, new TaggedActionListener("addLocalService"));
			isLocalServiceRegistered = true;
		} 
	} 
	 
	public void unregisterServices() { 
		if (!isEnabled)
			return; 
		wifiMgr.clearLocalServices(channel, new TaggedActionListener("clearLocalServices"));
		isLocalServiceRegistered = false;
	} 
	 
	public void disconnect() { 
		if (isConnected)
			wifiMgr.removeGroup(channel, new TaggedActionListener("removeGroup"));
	} 
	 
	public boolean isConnected() { 
		return isConnected;
	} 
	 
	public boolean isEnabled() { 
		return isEnabled;
	} 
	 
	public void stopWifiP2P() { 
		if (!isConnected){
			unregisterServiceRequest(); 
			stopDiscovery(); 
		} 
		ctx.unregisterReceiver(wifiReceiver);
	} 
 
	private void refreshPeers() { 
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
			Log.i(TAG, "Request peers");
			wifiMgr.requestPeers(channel, discoveryListener);
		} 
	} 
	 
	@Override 
	public void onChannelDisconnected() { 
		Log.e(TAG, "Error Channel disconnected");
	} 
 
	private class WifiReceiver extends BroadcastReceiver {
		 
		@Override 
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
				onStateChanged(intent);
			} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
				onConnectionChanged(intent);
			} else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)) {
				onDiscoveryChanged(intent);
			} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
				onConnectionChanged(intent);
			} else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
				onPeersChanged(intent);
			} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
				onLocalDeviceChanged(intent);
			} 
		} 
 
		private void onLocalDeviceChanged(Intent intent) {
			Log.i(TAG, "Local Wifi p2p device has changed ");
			localDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
			if (localDevice.status == WifiP2pDevice.CONNECTED) 
				Log.i(TAG, "Local device is connected");
			else  
				Log.i(TAG, "Local device is not connected");
		} 
		 
		private void onPeersChanged(Intent intent) {
			Log.i(TAG, "WifiP2p Peers has changed");
			if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
				Log.i(TAG, "Do not listen to peer changes but only service changes");
			} 
			refreshPeers(); 
		} 
		 
		private void onConnectionChanged(Intent intent) {
			WifiP2pInfo p2pInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO);
			NetworkInfo netInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
			Log.i(TAG, "Wifi p2p connection changed " + netInfo);
			isConnected = netInfo.isConnected();
			Log.i(TAG, "Wifip2p connection is connected ? " + isConnected);
			if (isConnected) {
			wifiMgr.requestConnectionInfo(channel, new ConnectionInfoListener() {
				 
				@Override 
				public void onConnectionInfoAvailable(WifiP2pInfo info) {
						Log.i(TAG, "Wifi p2p connection is formed ? " + info.groupFormed);
						wifiMgr.requestGroupInfo(channel, new GroupInfoListener() {
							 
							@Override 
							public void onGroupInfoAvailable(WifiP2pGroup group) {
								if (group == null)
									return;