public void StartDiscovery(string serviceId, TimeSpan?advertisingDuration,
                                   IDiscoveryListener listener)
        {
            Misc.CheckNotNull(serviceId, "serviceId");
            Misc.CheckNotNull(listener, "listener");

            var listenerOnGameThread = new OnGameThreadDiscoveryListener(listener);

            if (advertisingDuration.HasValue && advertisingDuration.Value.Ticks < 0)
            {
                throw new InvalidOperationException("advertisingDuration must be positive");
            }

            using (var endpointDiscoveryCallback = new AndroidJavaObject(
                       "com.google.games.bridge.EndpointDiscoveryCallbackProxy",
                       new EndpointDiscoveryCallback(listenerOnGameThread)))
                using (var discoveryOptions = CreateDiscoveryOptions())
                    using (var task = mClient.Call <AndroidJavaObject>("startDiscovery", serviceId, endpointDiscoveryCallback,
                                                                       discoveryOptions))
                    {
                        AndroidTaskUtils.AddOnSuccessListener <AndroidJavaObject>(
                            task,
                            v => NearbyHelperObject.StartDiscoveryTimer(advertisingDuration)
                            );
                    }
        }
        public void StartAdvertising(string name, List <string> appIdentifiers,
                                     TimeSpan?advertisingDuration, Action <AdvertisingResult> resultCallback,
                                     Action <ConnectionRequest> connectionRequestCallback)
        {
            Misc.CheckNotNull(resultCallback, "resultCallback");
            Misc.CheckNotNull(connectionRequestCallback, "connectionRequestCallback");

            if (advertisingDuration.HasValue && advertisingDuration.Value.Ticks < 0)
            {
                throw new InvalidOperationException("advertisingDuration must be positive");
            }

            connectionRequestCallback = ToOnGameThread(connectionRequestCallback);
            resultCallback            = ToOnGameThread(resultCallback);

            AdvertisingConnectionLifecycleCallbackProxy callbackProxy =
                new AdvertisingConnectionLifecycleCallbackProxy(resultCallback, connectionRequestCallback, this);

            using (var connectionLifecycleCallback =
                       new AndroidJavaObject("com.google.games.bridge.ConnectionLifecycleCallbackProxy", callbackProxy))
                using (var advertisingOptions = CreateAdvertisingOptions())
                    using (var task = mClient.Call <AndroidJavaObject>("startAdvertising", name, GetServiceId(),
                                                                       connectionLifecycleCallback, advertisingOptions))
                    {
                        AndroidTaskUtils.AddOnSuccessListener <AndroidJavaObject>(
                            task,
                            v => NearbyHelperObject.StartAdvertisingTimer(advertisingDuration)
                            );
                    }
        }
 public AndroidNearbyConnectionClient()
 {
     PlayGamesHelperObject.CreateObject();
     NearbyHelperObject.CreateObject(this);
     using (var nearbyClass = new AndroidJavaClass("com.google.android.gms.nearby.Nearby"))
     {
         mClient = nearbyClass.CallStatic <AndroidJavaObject>("getConnectionsClient",
                                                              AndroidHelperFragment.GetActivity());
     }
 }