android.net.wifi.p2p.WifiP2pManager.ActionListener Java Examples

The following examples show how to use android.net.wifi.p2p.WifiP2pManager.ActionListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void stopPeerDiscovery() {
    mWifiP2pManager.stopPeerDiscovery(mWifiP2pChannel, new ActionListener() {
        @Override
        public void onSuccess() {
            if (DEBUG) {
                Slog.d(TAG, "Stop peer discovery succeeded.");
            }
        }

        @Override
        public void onFailure(int reason) {
            if (DEBUG) {
                Slog.d(TAG, "Stop peer discovery failed with reason " + reason + ".");
            }
        }
    });
}
 
Example #2
Source File: CommCareWiFiDirectActivity.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
@Override
public void connect(WifiP2pConfig config) {
    Logger.log(TAG, "connecting to wi-fi peer");

    mManager.connect(mChannel, config, new ActionListener() {

        @Override
        public void onSuccess() {
            myStatusText.setText(localize("wifi.direct.connect.success"));
        }

        @Override
        public void onFailure(int reason) {
            Logger.log(TAG, "Connection to peer failed");
            Toast.makeText(CommCareWiFiDirectActivity.this,
                    localize("wifi.direct.connect.failed"),
                    Toast.LENGTH_SHORT).show();
        }
    });
}
 
Example #3
Source File: WifiDirectGroupManager.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
public void createGroup(){
		mWifiP2pManager.createGroup(mChannel, new ActionListener() {
			@Override
			public void onSuccess() {
				ALog.i(TAG, "create group success");
//				mHandler.obtainMessage(Event.CREATE_GROUP_SUCCESS).sendToTarget();
				mHandler.obtainMessage(Event.START_GROUP_SUCCESS).sendToTarget();
				mWifiP2pManager.requestGroupInfo(mChannel, WifiDirectGroupManager.this);
			}
			@Override
			public void onFailure(int reason) {
				ALog.i(TAG, "create group failure "+errorCode2String(reason));
//				mHandler.obtainMessage(Event.CREATE_GROUP_FAILURE,reason).sendToTarget();
				mHandler.obtainMessage(Event.START_GROUP_FAILURE).sendToTarget();
				//try to get data,when is start
				mWifiP2pManager.requestGroupInfo(mChannel, WifiDirectGroupManager.this);
			}
		});
	}
 
Example #4
Source File: WifiDirectGroupManager.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
public void removeGroup(){
		mWifiP2pManager.removeGroup(mChannel, new ActionListener() {
			@Override
			public void onSuccess() {
				ALog.i(TAG, "remove group success");
//				mHandler.obtainMessage(Event.REMOVE_GROUOP_SUCCESS).sendToTarget();
				mHandler.obtainMessage(Event.STOP_GROUP_SUCCESS).sendToTarget();
			}
			@Override
			public void onFailure(int reason) {
				ALog.i(TAG, "remove group failure "+errorCode2String(reason));
//				mHandler.obtainMessage(Event.REMOVE_GROUPO_FAILURE,reason).sendToTarget();
				mHandler.obtainMessage(Event.STOP_GROUP_FAILURE).sendToTarget();
			}
		});
	}
 
Example #5
Source File: WifiDirectService.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
protected void stopPeer(){
		mWifiDirectManager.stopDiscover(new ActionListener() {
			@Override
			public void onSuccess() {
				ALog.i(TAG, "WifiDirectService 停止探索");
				mIsWifiP2pSearching=false;
				sendBroadcast(new Intent(WifiDirectManager.WIFI_DIRECT_END_SEARCHING_ACTION));		
			}
			@Override
			public void onFailure(int reason) {
				Message msg=mServiceHandler.obtainMessage(MSG_DISCOVER_STOP);
//				mServiceHandler.sendMessageDelayed(msg, 2000);
			}
		});
		
	}
 
Example #6
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
public void connect(WifiP2pConfig config) {
		ALog.i(TAG, "connect mWifiP2pManager:" + mWifiP2pManager + ", config:" + config);
		if (mWifiP2pManager != null) {
			mWifiP2pManager.connect(mWifiChannel, config, new ActionListener() {

				@Override
				public void onSuccess() {
					ALog.v(TAG, "connect onSuccess!");
					Message msg = mManagerHandler.obtainMessage(Utilities.MSG_WIFI_CONNECT_SUCCESS);
					mManagerHandler.sendMessage(msg);
				}
				
				@Override
				public void onFailure(int reason) {
					ALog.v(TAG, "connect failed reason:" + reason);
//					Message msg = mManagerHandler.obtainMessage(Utilities.MSG_WIFI_CONNECT_FAILED);
//					if(mManagerHandler.hasMessages(Utilities.MSG_WIFI_CONNECT_FAILED))
//						return;
//					mManagerHandler.sendMessageDelayed(msg, 500);
				}
			});
		}
	}
 
Example #7
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
public void cancelConnect() {
	ALog.i(TAG, "cancelDisconnect mWifiP2pManager:" + mWifiP2pManager + ", mWifiChannel:" + mWifiChannel);
	if (mWifiP2pManager != null) {
		mWifiP2pManager.cancelConnect(mWifiChannel, new ActionListener() {

			@Override
			public void onSuccess() {
				ALog.v(TAG, "cancelDisconnect onSuccess!");
			}

			@Override
			public void onFailure(int reason) {
				ALog.v(TAG, "cancelDisconnect failed reason:" + reason);
			}
		});
	}
}
 
Example #8
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
public void disconnect() {
	ALog.i(TAG, "disconnect mWifiP2pManager:" + mWifiP2pManager + ", mWifiChannel:" + mWifiChannel);
	if (mWifiP2pManager != null) {
		mWifiP2pManager.removeGroup(mWifiChannel, new ActionListener() {
			@Override
			public void onSuccess() {
				ALog.v(TAG, "disconnect onSuccess!");
			}
			@Override
			public void onFailure(int reason) {
				ALog.v(TAG, "disconnect failed reason:" + reason);
			}
		});
	}
}
 
Example #9
Source File: WifiController.java    From gilgamesh with GNU General Public License v3.0 5 votes vote down vote up
public void startWifiDiscovery ()
{
	
	if (mWifiEnabled)
	{
	 mWifiManager.discoverPeers(mWifiChannel, new WifiP2pManager.ActionListener() {

         @Override
         public void onSuccess() {
             // Code for when the discovery initiation is successful goes here.
             // No services have actually been discovered yet, so this method
             // can often be left blank.  Code for peer discovery goes in the
             // onReceive method, detailed below.
         	Log.d(TAG,"success on wifi discover");
         	
         }

         @Override
         public void onFailure(int reasonCode) {
             // Code for when the discovery initiation fails goes here.
             // Alert the user that something went wrong.
         	Log.d(TAG,"FAIL on wifi discovery: " + reasonCode);
         }

	    });
	 
	 discoverWifiService();
	}
}
 
Example #10
Source File: WifiController.java    From gilgamesh with GNU General Public License v3.0 5 votes vote down vote up
public void init (GilgaService service)
{
 mService = service;

     mWifiManager = (WifiP2pManager) mService.getSystemService(Context.WIFI_P2P_SERVICE);
     
     mWifiChannel = mWifiManager.initialize(mService, mService.getMainLooper(), new ChannelListener()
     {

@Override
public void onChannelDisconnected() {
	Log.d(GilgaService.TAG,"wifi p2p disconnected");
}
     	
     });
     
     WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
     mWifiManager.addServiceRequest(mWifiChannel,
             serviceRequest,
             new ActionListener() {
                 @Override
                 public void onSuccess() {
                     // Success!
 	            	Log.d(TAG,"SUCCESS: added service request wifi name service");

                 }

                 @Override
                 public void onFailure(int code) {
                     // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
                 	Log.d(TAG,"FAILURED: added service request wifi name service: " + code);
                 }
             });
     
}
 
Example #11
Source File: WifiDirectService.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
public void startDiscoverAfterStopDiscover(){
	mServiceHandler.removeMessages(MSG_DISCOVER_STOP);
	mWifiDirectManager.stopDiscover(new ActionListener() {
		@Override
		public void onSuccess() {
			startDiscover();
		}

		@Override
		public void onFailure(int reason) {
			startDiscover();
		}
	});
}
 
Example #12
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
public void setDeviceName(String name){
//		setDeviceName(Channel c, String devName, ActionListener listener)
		Class<WifiP2pManager> clzz = WifiP2pManager.class;
		
		ActionListener listener=new ActionListener() {
			@Override
			public void onSuccess() {
				ALog.i(TAG, "setDeviceName succees");
			}
			@Override
			public void onFailure(int reason) {
				ALog.i(TAG, "setDeviceName failure");
			}
		};
		
		Method m=null;
		try {
			m=clzz.getDeclaredMethod("setDeviceName", 
					new Class[]{Channel.class,String.class,ActionListener.class});
			m.setAccessible(true);
			m.invoke(mWifiP2pManager, getChannel(),name,listener);	
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(m!=null)
				m.setAccessible(false);
		}
	}
 
Example #13
Source File: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void tryDiscoverPeers() {
    mWifiP2pManager.discoverPeers(mWifiP2pChannel, new ActionListener() {
        @Override
        public void onSuccess() {
            if (DEBUG) {
                Slog.d(TAG, "Discover peers succeeded.  Requesting peers now.");
            }

            if (mDiscoverPeersInProgress) {
                requestPeers();
            }
        }

        @Override
        public void onFailure(int reason) {
            if (DEBUG) {
                Slog.d(TAG, "Discover peers failed with reason " + reason + ".");
            }

            // Ignore the error.
            // We will retry automatically in a little bit.
        }
    });

    // Retry discover peers periodically until stopped.
    mHandler.postDelayed(mDiscoverPeers, DISCOVER_PEERS_INTERVAL_MILLIS);
}
 
Example #14
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message msg) {
  		ALog.i(TAG, "mManagerHandler msg.what:" + msg.what);
  		switch (msg.what) {
  		case Utilities.MSG_WIFI_DISCOVER_PEER_FAILED:
  			ALog.i(TAG, "Wifi discover peer failed.now retry");
  			discover((ActionListener)msg.obj);
  			break;
  		case Utilities.MSG_WIFI_DISCOVER_STOP_FAILED:
  			ALog.i(TAG, "Wifi discover stop failed.now retry");
  			stopDiscover((ActionListener)msg.obj);
  			break;
  		case Utilities.MSG_WIFI_DISCOVER_PEER_SUCCESS:
  			ALog.i(TAG, "Wifi discover peer success.");
  			sendWifiDirectBroadcast(WIFI_DIRECT_DISCOVERED_PEER_ACTION);
  			break;
  		case Utilities.MSG_WIFI_CONNECT_FAILED:
  			ALog.i(TAG, "Wifi connect failed.");
  			break;
  		case Utilities.MSG_WIFI_CONNECT_SUCCESS:
  			ALog.i(TAG, "Wifi connect success.");
  			sendWifiDirectBroadcast(WIFI_DIRECT_CONNECT_FAILED);
  			break;
  		case Utilities.MSG_WIFI_CONNECTED:
  			mIsWifiP2pConnected = true;
  			ALog.i(TAG, "Wifi now p2pConnected.");
  			sendWifiDirectBroadcast(WIFI_DIRECT_CONNECTED_ACTION);
  			break;
  		}
  	}
 
Example #15
Source File: HeartbeatService.java    From letv with Apache License 2.0 5 votes vote down vote up
void discoverP2pDevice() {
    LeBoxNetworkManager.getInstance().discoverP2pPeers(new ActionListener() {
        public void onSuccess() {
            Logger.d(HeartbeatService.TAG, "---discoverP2pDevice--discoverPeers--onSuccess");
        }

        public void onFailure(int reason) {
            Logger.d(HeartbeatService.TAG, "---discoverP2pDevice--discoverPeers--onFailure reason=" + reason);
        }
    });
}
 
Example #16
Source File: CommCareWiFiDirectActivity.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
public void discoverPeers() {
    Logger.log(TAG, "Discovering Wi-fi direct peers");

    WiFiDirectManagementFragment fragment = (WiFiDirectManagementFragment)getSupportFragmentManager()
            .findFragmentById(R.id.wifi_manager_fragment);

    if (!fragment.isWifiP2pEnabled()) {
        Toast.makeText(CommCareWiFiDirectActivity.this, localize("wifi.direct.wifi.direct.off"),
                Toast.LENGTH_SHORT).show();
        return;
    }

    final DeviceListFragment dlFragment = (DeviceListFragment)getSupportFragmentManager()
            .findFragmentById(R.id.frag_list);
    dlFragment.onInitiateDiscovery();

    mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

        @Override
        public void onSuccess() {
            Toast.makeText(CommCareWiFiDirectActivity.this,
                    localize("wifi.direct.discovery.start"),
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(int reasonCode) {

            Logger.log(TAG, "Discovery of Wi-fi peers failed");

            if (reasonCode == 0 || reasonCode == 2) {
                Toast.makeText(CommCareWiFiDirectActivity.this,
                        localize("wifi.direct.discovery.failed.generic"),
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(CommCareWiFiDirectActivity.this,
                        localize("wifi.direct.discovery.failed.specific", "" + reasonCode),
                        Toast.LENGTH_SHORT).show();
            }

        }
    });
}
 
Example #17
Source File: LeBoxNetworkManager.java    From letv with Apache License 2.0 4 votes vote down vote up
public void discoverP2pPeers(ActionListener listener) {
    LeBoxWifiDirectModel.getInstance().discoverPeers(listener);
}
 
Example #18
Source File: LeBoxNetworkManager.java    From letv with Apache License 2.0 4 votes vote down vote up
public void connectP2p(WifiP2pDevice device, ActionListener listener) {
    LeBoxWifiDirectModel.getInstance().connect(device, listener);
}
 
Example #19
Source File: WifiController.java    From gilgamesh with GNU General Public License v3.0 4 votes vote down vote up
private void discoverWifiService() {
    DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
        @Override
        /* Callback includes:
         * fullDomain: full domain name: e.g "printer._ipp._tcp.local."
         * record: TXT record dta as a map of key/value pairs.
         * device: The device running the advertised service.
         */

        public void onDnsSdTxtRecordAvailable(String fullDomain, Map record, WifiP2pDevice device) {
                
                String status = (String)record.get("status");
                
                Log.d(TAG,"got status from wifi DNS: " + device.deviceAddress + " (" + fullDomain + ") " + status);
                mService.processInboundMessage(device.deviceAddress,status,false);
            }
        
        };
    
        DnsSdServiceResponseListener servListener = new DnsSdServiceResponseListener() {
            @Override
            public void onDnsSdServiceAvailable(String instanceName, String registrationType,
                    WifiP2pDevice resourceType) {
            	Log.d(TAG,"SD service available!");
            }
        };
        
        mWifiManager.setDnsSdResponseListeners(mWifiChannel, servListener, txtListener);
        
        mWifiManager.discoverServices(mWifiChannel, new ActionListener() {

            @Override
            public void onSuccess() {
                // Success!
            	Log.d(TAG, "discover services!");
            }

            @Override
            public void onFailure(int code) {
                // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
                if (code == WifiP2pManager.P2P_UNSUPPORTED) {
                    Log.d(TAG, "P2P isn't supported on this device.");
                    mWifiEnabled = false;
                }
            }
        });

}