1

I update my play-services-nearby to version '10.2.0', it changes the EndpointDiscoveryListener and ConnectionRequestListener from interface to abstract class, I extend NearbyClient with EndpointDiscoveryListener and declare inner class ConnectionRequestListener , now I see that AppIdentifier is deprecated too, I searched a lot in google but I can't find any new example, here is my code I changed from 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);
        }
    }

}

the rest of the code is same as example. What is the best way to implement new version?
it shows me "Unfortunately, Google Play Services has stopped" when I want to connect as a client, What is the deprecation new version?

Community
  • 1
  • 1
Amir Mehrnam
  • 35
  • 2
  • 8

1 Answers1

1

The easiest approach in the context of the NearbyClient class is to add two new fields to the class that implement the abstract classes and simply call the existing onConnectionRequest and onEndpointFound/Lost.

The confusion in 10.2 is introduced when the device id parameter is no longer being exposed. In most cases, this was meaningless bookkeeping that the app had to do, so now in 10.2 you don't have to keep track of the device 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);
            }
        };

I'll try out 8bit artist later this week to update it to work with 10.2. In the mean time, please feel free to submit a pull request if you get it working first :) .

Clayton Wilkinson
  • 4,524
  • 1
  • 16
  • 25
  • 1
    Did you run 8bitartist? because it didn't work for me. if you did please send me the new one? or replace it on github – Amir Mehrnam Feb 24 '17 at 12:00
  • 1
    The sample has been updated: https://github.com/playgameservices/android-basic-samples There is a known bug however, that Play Services will crash sometimes when calling stopDiscovery() or stopAdvertising(). They are working on fixing it and will be in the next SDK update. – Clayton Wilkinson Feb 24 '17 at 15:33
  • 1
    @ClaytonWilkinson Can you tell us approximately when we can expect the the SDK update containing the fix? – trinity420 Apr 15 '17 at 11:43
  • 1
    The fix is in the play-services app, not the client libraries. I don't know the dates, but I do know they try to update play-services frequently since it is so central to a lot of services. You can join the beta testing program, which helps test play-services and will also give you a better indication of when changes are coming to the app. You can join here: https://play.google.com/apps/testing/com.google.android.gms – Clayton Wilkinson Apr 16 '17 at 19:06
  • @ClaytonWilkinson Thank you very much! I joined. Unfortunately I still get the same random NullPointerException on calling stopDiscovery()/stopAdvertising() and can't send send messages (endpoint lost after connection). Looking forward to the next beta.. – trinity420 Apr 17 '17 at 11:18