private void addServiceDiscoveryRequest() { serviceRequest = WifiP2pDnsSdServiceRequest.newInstance(); // Tell the framework we want to scan for services. Prerequisite for discovering services wifiP2pManager.addServiceRequest(channel, serviceRequest, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.i(TAG, "Service discovery request added"); } @Override public void onFailure(int reason) { Log.e(TAG, "Failure adding service discovery request: " + FailureReason.fromInteger(reason).toString()); serviceRequest = null; } }); }
private void addServiceRequest() { WifiP2pDnsSdServiceRequest request = WifiP2pDnsSdServiceRequest.newInstance(); final Handler handler = new Handler(); p2p.addServiceRequest(channel, request, new ActionListener() { public void onSuccess() { Log.d(TAG, "Added service request"); // Calling discoverServices() too soon can result in a // NO_SERVICE_REQUESTS failure - looks like a race condition // http://p2feed.com/wifi-direct.html#bugs handler.postDelayed(new Runnable() { public void run() { startServiceDiscovery(); } }, 1000); } public void onFailure(int reason) { print("Adding service request failed, error code " + reason); // No point starting service discovery } }); }
private void setupDnsSdServiceRequest() { if (serviceRequest == null){ // After attaching listeners, create a service request for the type of service desired serviceRequest = WifiP2pDnsSdServiceRequest.newInstance(); manager.addServiceRequest(channel, serviceRequest, new ActionListener() { public void onSuccess() { // appendStatus("Added service discovery request"); } public void onFailure(int reason) { // appendStatus("Failed adding service discovery request"); } }); } }
/** * Setups listeners that are called, when a new "susurrus"-service is found. */ public void setupLocalServiceDiscovery() { Log.d(LOG_TAG, "Setup discovering rooms."); wifiDirectManager.setDnsSdResponseListeners(wifiDirectChannel, servListener, txtListener); // get an instance of the WifiP2P service request object WifiP2pDnsSdServiceRequest roomRequest = WifiP2pDnsSdServiceRequest.newInstance(); // add a service discovery request wifiDirectManager.addServiceRequest(wifiDirectChannel, roomRequest, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { // Success! Log.d(LOG_TAG, "... wifiDirectManager.addServiceRequest success ..."); discoverLocalServices(); } @Override public void onFailure(int code) { // Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY Log.d(LOG_TAG, "addServiceRequest error: " + code); } } ); }
public NFGame(Context context, NFGameNotifyListener nFGameNotifyListener) { mContext = context; mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mWifiP2pManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE); mChannel = mWifiP2pManager.initialize(mContext, mContext.getMainLooper(), null); mNFGameNotifyListener = nFGameNotifyListener; mNFGameBroadcastReceiver = new NFGameBroadcastReceiver(); mNFGameDnsSdServiceResponseListener = new NFGameDnsSdServiceResponseListener(); try { ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), 0); appLabel = mContext.getPackageManager().getApplicationLabel(appInfo).toString(); } catch (NameNotFoundException e) { // e.printStackTrace(); } serviceType = mContext.getPackageName(); mWifiP2pDnsSdServiceInfo = WifiP2pDnsSdServiceInfo.newInstance(appLabel, serviceType, null); mWifiP2pDnsSdServiceRequest = WifiP2pDnsSdServiceRequest.newInstance(); mHandler = new Handler(); mDiscoveryingRunnable = new Runnable() { @Override public void run() { discoverPeers(); } }; peers = new ArrayList<WifiP2pDevice>(); servicePeers = new ArrayList<WifiP2pDevice>(); }
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); } }); }
private void initPeersDiscovery() { WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance("run2gether", "presence"); p2pManager.addServiceRequest(p2pChannel, serviceRequest, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "success"); } @Override public void onFailure(int code) { // Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY } }); WifiP2pManager.DnsSdTxtRecordListener txtListener = new WifiP2pManager.DnsSdTxtRecordListener() { @Override public void onDnsSdTxtRecordAvailable(String fullDomain, Map<String, String> userAttribMap, WifiP2pDevice device) { final WDSwanDevice nearbyUser = new WDSwanDevice(userAttribMap.get("name"), userAttribMap.get("regId"), device); if (!hasPeer(nearbyUser.getUsername())) { addPeer(nearbyUser); slpActivity.getNearbyPeersAdapter().notifyDataSetChanged(); Log.d(TAG, "Found new nearby user " + nearbyUser); } else { if (updatePeer(nearbyUser)) { log("Updated nearby user " + nearbyUser, true); } } } }; WifiP2pManager.DnsSdServiceResponseListener servListener = new WifiP2pManager.DnsSdServiceResponseListener() { @Override public void onDnsSdServiceAvailable(String instanceName, String registrationType, WifiP2pDevice resourceType) { // nothing to do here Log.d(TAG, "onBonjourServiceAvailable " + instanceName); } }; p2pManager.setDnsSdResponseListeners(p2pChannel, servListener, txtListener); nearbyPeersChecker.run(); }
private void discoverServices() { WifiP2pManager.DnsSdTxtRecordListener txtListener = new WifiP2pManager.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) { Log.d(TAG, "DnsSdTxtRecord available -" + record.toString()); //buddies.put(device.deviceAddress, (String)record.get("buddyname")); // Add to the custom adapter defined specifically for showing // wifi devices connectToDevice(device); } }; WifiP2pManager.DnsSdServiceResponseListener servListener = new WifiP2pManager.DnsSdServiceResponseListener() { @Override public void onDnsSdServiceAvailable(String instanceName, String registrationType, WifiP2pDevice resourceType) { // Update the device name with the human-friendly version from // the DnsTxtRecord, assuming one arrived. resourceType.deviceName = buddies .containsKey(resourceType.deviceAddress) ? buddies .get(resourceType.deviceAddress) : resourceType.deviceName; // Add to the custom adapter defined specifically for showing // wifi devices. Log.d(TAG, "onBonjourServiceAvailable " + instanceName); } }; manager.setDnsSdResponseListeners(channel, servListener, txtListener); WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance(); manager.addServiceRequest(channel, serviceRequest, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.v(TAG, "Suggesfully added a servicerequest"); } @Override public void onFailure(int reason) { Log.e(TAG, "Failed to add servicerequest"); } }); }
@Override public void discover() { mP2PManager.setDnsSdResponseListeners(mChannel, mDnsListener, mDnsTxtRecordListener); mServiceRequest = WifiP2pDnsSdServiceRequest.newInstance(); mP2PManager.addServiceRequest(mChannel, mServiceRequest, mServiceAddListener); }