Code example for WifiP2pManager

Methods: discoverPeersrequestPeersstopPeerDiscoverygetClass

-2
		} 
	} 
 
	@SuppressLint({ "NewApi", "NewApi" }) 
	public void discover() { 
		mWifiP2pManager.discoverPeers(channel, null);
	} 
 
	private void setDeviceName(WifiP2pManager manager, Channel channel,
			String name, ActionListener listener) {
		try { 
			Method method = manager.getClass().getMethod("setDeviceName",
					Channel.class, String.class, ActionListener.class);
			method.invoke(manager, channel, name, listener);
		} catch (Exception e) {
			Log.e(TAG, "setDeviceName fail: " + e);
		} 
	} 
 
	/** 
	 * @param serverFlag 
	 *            ,if true as the group owner,else as the client 
	 * */ 
	public void searchDirect(Context context, boolean server) {
 
		serverFlag = server;
		discover(); 
	} 
 
	@SuppressLint({ "NewApi", "NewApi" }) 
	public void stopSearch() { 
		mWifiP2pManager.stopPeerDiscovery(channel, null);
	} 
 
	public void stopConnect() { 
		try { 
			mWifiP2pManager.cancelConnect(channel, null);
			mWifiP2pManager.removeGroup(channel, null);
		} catch (Exception e) {
			Log.e(TAG, "stopConnect()" + e);
		} 
	} 
 
	public void registerObserver(ManagerP2pDeivce notify) {
		observerList.add(notify);
	} 
 
	public void unRegisterObserver(ManagerP2pDeivce notify) {
		observerList.remove(notify);
	} 
 
	public void connect(WifiP2pConfig config) {
		mWifiP2pManager.connect(channel, config, null);
	} 
 
	@Override 
	public void wifiP2pConnected() { 
		notifyConnect(); 
	} 
 
	private void getPeerDevice(final boolean flag) {
		final ArrayList<WifiP2pDevice> serverList = new ArrayList<WifiP2pDevice>();
		mWifiP2pManager.requestPeers(channel, new PeerListListener() {
			@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", 
					"NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", 
					"NewApi", "NewApi", "NewApi" }) 
			public void onPeersAvailable(WifiP2pDeviceList peers) {
				if (!flag) {
Experience pair programming with AI  Get Codota for Java