我正在开发一个基于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 回答
同时我实现的解决方法是添加一个CountDownTimer,其中包含广告超时所需的时间:
这样,每当某个用户失去连接时,他将在15秒后达到超时 . 希望它对你们所有人都有帮助!等待API更好的实现,但它仍然有效 .