2017-09-14 5 views
1

私はAndroidをベースにしたプロジェクトに取り組んでいます。私はそのタイムアウトをふり何...発見を開始し、広告のタイムアウトがAndroidで動作しない開始付近

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 

を要求接続と操作のためのタイムアウトを追加したり、ペイロードを送信しますが、それは発見と広告プロセスで動作させることは不可能であった可能性があり、デバイスまで待機年齢を避けるためです対になる別の接続を検出します。誰かその問題がありましたか?

答えて

0

一方、私が実装されている回避策は、広告のタイムアウトのために必要な時間と、CountDownTimerを追加することです:

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) 
    } 

この方法では、一部のユーザーが接続を失うたびに、彼は15の後にタイムアウトに達するだろうあなたのすべてに役立つことを祈っています!とにかく、より良い実装を待っているが、それは動作します。

関連する問題