首页 文章

启动发现和开始广告超时不适用于Android Nearby

提问于
浏览
1

我正在开发一个基于Android Nearby的项目 . 我可以将操作的超时添加为请求连接或发送有效负载,但是它无法使其在发现和广告过程中起作用...

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

我假装超时是为了避免等待时间,直到设备找到另一个连接配对 . 有没有人有这个问题?

1 回答

  • 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秒后达到超时 . 希望它对你们所有人都有帮助!等待API更好的实现,但它仍然有效 .

相关问题