@@ -56,6 +56,7 @@ public class Salut implements WifiP2pManager.ConnectionInfoListener{
5656 public SalutDevice registeredHost ;
5757 public boolean isRunningAsHost = false ;
5858 public boolean isConnectedToAnotherDevice = false ;
59+ public boolean isDiscovering = false ;
5960 private ServerSocket listenerServiceSocket ;
6061 private ServerSocket salutServerSocket ;
6162
@@ -494,6 +495,8 @@ public void onFailure(int reason) {
494495
495496 private void createService (final SalutCallback onSuccess , final SalutCallback onFailure ) {
496497
498+ manager .clearLocalServices (channel , null );
499+
497500 Log .d (TAG , "Starting " + thisDevice .serviceName + " Transport Protocol " + TTP );
498501
499502 //Inject the listening port along with whatever else data that is going to be sent.
@@ -573,8 +576,7 @@ public void unregisterClient(boolean disableWiFi)
573576 unregisterClient (null , null , disableWiFi );
574577 }
575578
576- public void startNetworkService (SalutDeviceCallback onDeviceRegisteredWithHost )
577- {
579+ public void startNetworkService (SalutDeviceCallback onDeviceRegisteredWithHost ) {
578580 startNetworkService (onDeviceRegisteredWithHost , null , null );
579581 }
580582
@@ -620,11 +622,14 @@ public void onDnsSdServiceAvailable(String instanceName, String serviceNameAndTP
620622 @ Override
621623 public void onDnsSdTxtRecordAvailable (String serviceFullDomainName , Map <String , String > record , WifiP2pDevice device ) {
622624
623- for ( SalutDevice found : foundDevices )
625+ if (! foundDevices . isEmpty () )
624626 {
625- if ( found . deviceName . equals ( device . deviceName ) )
627+ for ( SalutDevice found : foundDevices )
626628 {
627- return ;
629+ if (found .deviceName .equals (device .deviceName ))
630+ {
631+ return ;
632+ }
628633 }
629634 }
630635
@@ -664,11 +669,14 @@ public void onDnsSdServiceAvailable(String instanceName, String serviceNameAndTP
664669 @ Override
665670 public void onDnsSdTxtRecordAvailable (String serviceFullDomainName , Map <String , String > record , WifiP2pDevice device ) {
666671
667- for ( SalutDevice found : foundDevices )
672+ if (! foundDevices . isEmpty () )
668673 {
669- if ( found . deviceName . equals ( device . deviceName ) )
674+ for ( SalutDevice found : foundDevices )
670675 {
671- return ;
676+ if (found .deviceName .equals (device .deviceName ))
677+ {
678+ return ;
679+ }
672680 }
673681 }
674682
@@ -682,7 +690,7 @@ public void onDnsSdTxtRecordAvailable(String serviceFullDomainName, Map<String,
682690 onDeviceFound .call ();
683691 firstDeviceAlreadyFound = true ;
684692 }
685- else if (firstDeviceAlreadyFound && callContinously )
693+ else if (callContinously )
686694 {
687695 onDeviceFound .call ();
688696 }
@@ -716,9 +724,15 @@ public void onDnsSdServiceAvailable(String instanceName, String serviceNameAndTP
716724 @ Override
717725 public void onDnsSdTxtRecordAvailable (String serviceFullDomainName , Map <String , String > record , WifiP2pDevice device ) {
718726
719- for (SalutDevice found : foundDevices ) {
720- if (found .deviceName .equals (device .deviceName )) {
721- return ;
727+
728+ if (!foundDevices .isEmpty ())
729+ {
730+ for (SalutDevice found : foundDevices )
731+ {
732+ if (found .deviceName .equals (device .deviceName ))
733+ {
734+ return ;
735+ }
722736 }
723737 }
724738
@@ -732,7 +746,7 @@ public void onDnsSdTxtRecordAvailable(String serviceFullDomainName, Map<String,
732746 onDeviceFound .call (foundDevice );
733747 firstDeviceAlreadyFound = true ;
734748 }
735- else if (firstDeviceAlreadyFound && callContinously )
749+ else if (callContinously )
736750 {
737751 onDeviceFound .call (foundDevice );
738752 }
@@ -768,6 +782,8 @@ public void run() {
768782
769783 private void discoverNetworkServices (final SalutCallback deviceNotSupported )
770784 {
785+ isDiscovering = true ;
786+
771787 foundDevices .clear ();
772788
773789 if (!receiverRegistered )
@@ -883,6 +899,8 @@ public void onSuccess() {
883899
884900 public void stopServiceDiscovery (boolean shouldUnregister )
885901 {
902+ isDiscovering = false ;
903+
886904 if (isConnectedToAnotherDevice )
887905 disconnectFromDevice ();
888906
0 commit comments