Nearby Connections 2.0 无法连接到对等点

Nearby Connections 2.0 is unable to connect to peers

我以为我的策略是正确的。P2P_CLUSTER 连接,但我得到的是:

D/Meshy: onEndpointFound (we want to connect to someone!) (endpointId=CL36, serviceId=meshy.SERVICE_ID, endpointName=365589)
W/Meshy: acceptConnection failed with 8011 STATUS_ENDPOINT_UNKNOWN

这很奇怪,从来没有人对连接请求说 "no"。也许我的生命周期有误?

val mEndpointDiscoveryCallback = object : EndpointDiscoveryCallback() {
        // We found someone to connect to!
        override fun onEndpointFound(endpointId: String, info: DiscoveredEndpointInfo) {
            Log.d(TAG, "onEndpointFound (we want to connect to someone!) (endpointId=$endpointId, serviceId=${info.serviceId}, endpointName=${info.endpointName})")
            if (SERVICE_ID == info.serviceId) {
                Nearby.Connections.acceptConnection(mGoogleApiClient, endpointId, mPayloadCallback)
                        .setResultCallback { status ->
                            if (!status.isSuccess) {
                                Log.w(TAG, "acceptConnection failed with ${status.toReadable()}")
                            } else {
                                Log.d(TAG, "acceptConnection success")
                                endpoints.add(endpointId)
                            }
                        }
            } else {
                Log.w(TAG, "onEndpointFound ignoring unknown endpointId=$endpointId serviceId=${info.serviceId}")
            }
        }

您缺少对 requestConnection() 的调用——请参阅 https://developers.google.com/nearby/connections/android/manage-connections 处的代码片段以了解预期的 lifecycle/flow。