2017-09-14 4 views
1

Je travaille sur un projet basé sur Android à proximité. Je pourrais ajouter les délais d'attente pour les opérations de demande de connexion ou Envoyer Payload mais il a été impossible de faire cela fonctionne dans la découverte et le processus de la publicité ...Démarrer la découverte et commencer la publicité Timeout ne fonctionne pas avec Android à proximité

Nearby.Connections.startDiscovery(
      googleApiClient, 
      getServiceId(), 
      object : EndpointDiscoveryCallback() { 
       override fun onEndpointFound(endpointId: String, info: DiscoveredEndpointInfo) { 
        Log.d(TAG, 
          String.format(
            "onEndpointFound(endpointId=%s, serviceId=%s, endpointName=%s)", 
            endpointId, info.serviceId, info.endpointName)) 

        if (getServiceId() == info.serviceId) { 
         val endpoint = Endpoint(endpointId, info.endpointName) 
         discoveredEndpoints.put(endpointId, endpoint) 
         onEndpointDiscovered(endpoint) 
        } 
       } 

       override fun onEndpointLost(endpointId: String) { 
        Log.d(TAG, String.format("onEndpointLost(endpointId=%s)", endpointId)) 
       } 
      }, 
      DiscoveryOptions(STRATEGY)) 
      .setResultCallback({ status -> onResult(ConnectionCase.START_DISCOVERY, status) }, TIMEOUT_DISCOVERY_MILLIS, TimeUnit.MILLISECONDS) 


private val TIMEOUT_DISCOVERY_MILLIS: Long = 1000 

Ce que je fais semblant avec ce délai d'attente est d'éviter les âges d'attente jusqu'à ce que le dispositif trouve une autre connexion à associer. Quelqu'un at-il eu ce problème?

Répondre

0

Pendant ce temps la solution de contournement que j'ai mis en œuvre est d'ajouter un CountDownTimer, avec le temps voulu pour le délai d'attente publicitaire:

private val TIMEOUT_DISCOVERY_MILLIS: Long = 15000 
    private val SECOND_MILLIS: Long = 1000 
private fun startConnectionTimer() { 

     countDownTimer = object : CountDownTimer(TIMEOUT_DISCOVERY_MILLIS, SECOND_MILLIS) { 

      override fun onTick(millisUntilFinished: Long) { 
       Log.d("ADVERT", "seconds remaining: " + millisUntilFinished/SECOND_MILLIS) 
      } 

      override fun onFinish() { 
       if (!connectionAccepted) { 
        onTimeOut() 
       } 
      } 
     }.start() 

    } 
    //when advertising starts, this function is called: 
     protected fun onAdvertisingStarted() { 
     connectionAccepted = false 
     startConnectionTimer() 
    } 
//It resets everything and stops the NearbyActions 
    fun onTimeOut() { 
     resetState() 
     stopNearbyActions() 
     onTimeOutReached() 
     setState(State.UNKNOWN) 
    } 

De cette façon, chaque fois qu'un utilisateur perd la connexion, il arrivera un temps mort après 15 secondes. J'espère que cela vous aide tous! En attente d'une meilleure implémentation de l'API, mais ça marche quand même.