如何在新的play-services-nearby中实现抽象类EndpointDiscoveryListener和ConnectionRequestListener?

问题描述:

更新我的播放服务,就近版本'10 .2.0' ,它改变从界面到抽象类的EndpointDiscoveryListener和ConnectionRequestListener,我EndpointDiscoveryListener延长NearbyClient并宣布内部类ConnectionRequestListener,现在我看到AppIdentifier已过时过,我搜索了很多在谷歌,但我找不到任何新的例子, 这里是我的代码,我从GitHub playgameservices改变:如何在新的play-services-nearby中实现抽象类EndpointDiscoveryListener和ConnectionRequestListener?

public class NearbyClient extends Connections.EndpointDiscoveryListener implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     Connections.MessageListener { 


    private class OnConnectionRequest extends Connections.ConnectionRequestListener { 

    private NearbyClient mNearbyClient; 

    OnConnectionRequest(NearbyClient nearbyClient) 
    { 
     this.mNearbyClient = nearbyClient; 
    } 

    @Override 
    public void onConnectionRequest(final String remoteEndpointId, final String remoteEndpointName, byte[] payload) { 
     Log.d(TAG, "onConnectionRequest:" + remoteEndpointId + 
       ":" + remoteEndpointName); 

     if (mIsHost) { 
      // The host accepts all connection requests it gets. 
      byte[] myPayload = null; 
      Nearby.Connections.acceptConnectionRequest(mGoogleApiClient, remoteEndpointId, 
        myPayload, mNearbyClient).setResultCallback(new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        Log.d(TAG, "acceptConnectionRequest:" + status + ":" + remoteEndpointId); 
        if (status.isSuccess()) { 
         Toast.makeText(mContext, "Connected to " + remoteEndpointName, 
           Toast.LENGTH_SHORT).show(); 

         // Record connection 
         HeroParticipant participant = new HeroParticipant(remoteEndpointId, remoteEndpointName); 
         mConnectedClients.put(remoteEndpointId, participant); 

         // Notify listener 
         mListener.onConnectedToEndpoint(remoteEndpointId, remoteEndpointName); 
        } else { 
         Toast.makeText(mContext, "Failed to connect to: " + remoteEndpointName, 
           Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
     } else { 
      // Clients should not be advertising and will reject all connection requests. 
      Log.w(TAG, "Connection Request to Non-Host Device - Rejecting"); 
      Nearby.Connections.rejectConnectionRequest(mGoogleApiClient, remoteEndpointId); 
     } 
    } 

} 

的代码的其余部分是相同的例子。 实施新版本的最佳方式是什么?
它显示我“不幸的是,Google Play服务已停止”,当我想作为客户端连接时, 什么是弃用新版本?

在NearbyClient类的上下文中,最简单的方法是向类中添加两个新字段来实现抽象类,并简单地调用现有的onConnectionRequest和onEndpointFound/Lost。

当设备ID参数不再暴露时,引入了10.2中的混淆。在大多数情况下,这是应用程序必须做的无意义的簿记,所以现在在10.2中,您不必跟踪设备ID!

private Connections.ConnectionRequestListener myConnectionRequestListener = 
     new Connections.ConnectionRequestListener() { 
      @Override 
      public void onConnectionRequest(String remoteEndpointId, String 
        remoteEndpointName, byte[] bytes) { 
       NearbyClient.this.onConnectionRequest(remoteEndpointId, 
         remoteEndpointName, bytes); 
      } 
     }; 
private Connections.EndpointDiscoveryListener myEndpointDiscoveryListener = 
     new Connections.EndpointDiscoveryListener() { 
      @Override 
      public void onEndpointFound(String endpointId, 
             String serviceId, 
             String name) { 
       NearbyClient.this.onEndpointFound(endpointId,serviceId, 
         name); 
      } 

      @Override 
      public void onEndpointLost(String remoteEndpointId) { 
       NearbyClient.this.onEndpointLost(remoteEndpointId); 
      } 
     }; 

我会在本周晚些时候尝试8位艺术家来更新它与10.2一起工作。与此同时,请随时提交拉请求,如果你得到它的第一个:)。

+0

你运行8bitartist?因为它不适合我。如果你确实请给我发送新的?或更换在github –

+1

样本已更新:https://github.com/playgameservices/android-basic-samples有一个已知的bug但是,播放服务有时会打电话时stopDiscovery()或stopAdvertising()崩溃。他们正在修复它,并将在下一次SDK更新中。 –

+0

请让我知道,如果错误纠正 –