Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
启动发现和启动广告超时不与附近的Android一起工作_Android_Bluetooth Lowenergy_Android Bluetooth - Fatal编程技术网

启动发现和启动广告超时不与附近的Android一起工作

启动发现和启动广告超时不与附近的Android一起工作,android,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth Lowenergy,Android Bluetooth,我正在附近做一个基于Android的项目。我可以为请求连接或发送有效负载等操作添加超时,但不可能使其在发现和广告过程中工作 Nearby.Connections.startDiscovery( googleApiClient, getServiceId(), object : EndpointDiscoveryCallback() { override fun onEndpointFou

我正在附近做一个基于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

我假装超时是为了避免设备找到另一个要配对的连接之前的等待时间。有人遇到过这个问题吗?

同时,我实施的解决方案是添加一个倒计时程序,其中包含广告超时所需的时间:

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实现,但它仍然可以工作。

同时,我已经实现的解决方法是添加一个倒计时程序,其中包含广告超时所需的时间:

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实现,但它仍然可以工作