如何在新的play-services-nearby中实现abstract class Endpoint Discovery Listener和Connection RequestListener?
how to implement abstract class EndpointDiscoveryListener and ConnectionRequestListener in new play-services-nearby?
我将附近的播放服务更新到版本“10.2.0”,它将 EndpointDiscoveryListener 和 ConnectionRequestListener 从接口更改为抽象 class,我使用 EndpointDiscoveryListener 扩展 NearbyClient 并声明内部 class ConnectionRequestListener ,现在我看到 AppIdentifier 也被弃用了,我在 google 中搜索了很多但我找不到任何新的例子,
这是我从 github playgameservices 更改的代码:
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 class 的上下文中,最简单的方法是向 class 添加两个新字段来实现抽象 classes 并简单地调用现有的 onConnectionRequest 和 onEndpointFound/Lost.
10.2 中的混淆是在不再公开设备 ID 参数时引入的。在大多数情况下,这是应用程序必须做的毫无意义的簿记,所以现在在 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);
}
};
我将在本周晚些时候试用 8bit artist 以更新它以使用 10.2。同时,如果您先让它工作,请随时提交拉取请求 :) .
我将附近的播放服务更新到版本“10.2.0”,它将 EndpointDiscoveryListener 和 ConnectionRequestListener 从接口更改为抽象 class,我使用 EndpointDiscoveryListener 扩展 NearbyClient 并声明内部 class ConnectionRequestListener ,现在我看到 AppIdentifier 也被弃用了,我在 google 中搜索了很多但我找不到任何新的例子, 这是我从 github playgameservices 更改的代码:
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 class 的上下文中,最简单的方法是向 class 添加两个新字段来实现抽象 classes 并简单地调用现有的 onConnectionRequest 和 onEndpointFound/Lost.
10.2 中的混淆是在不再公开设备 ID 参数时引入的。在大多数情况下,这是应用程序必须做的毫无意义的簿记,所以现在在 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);
}
};
我将在本周晚些时候试用 8bit artist 以更新它以使用 10.2。同时,如果您先让它工作,请随时提交拉取请求 :) .