Esempio n. 1
0
        /**
         * Send a connection request to a given endpoint.
         * @param endpointId the endpointId to which you want to connect.
         * @param endpointName the name of the endpoint to which you want to connect. Not required to
         *                     make the connection, but used to display after success or failure.
         */
        async void ConnectTo(String endpointId, string endpointName)
        {
            Log("connectTo:" + endpointId + ":" + endpointName);

            // Send a connection request to a remote endpoint. By passing 'null' for the name,
            // the Nearby Connections API will construct a default name based on device model
            // such as 'LGE Nexus 5'.
            string myName = null;

            byte[] myPayload = null;

            var connectionResponseCallback = new ConnectionResponseCallback {
                OnConnectionResponseHandler = (epId, s2, bytes) => {
                    Log("onConnectionResponse:" + epId + ":" + s2);
                    if (s2.IsSuccess)
                    {
                        Log("onConnectionResponse: " + endpointName + " SUCCESS");
                        Toast.MakeText(this, "Connected to " + endpointName, ToastLength.Short).Show();

                        mOtherEndpointId = endpointId;
                        UpdateViewVisibility(NearbyConnectionState.Connected);
                    }
                    else
                    {
                        Log("onConnectionResponse: " + endpointName + " FAILURE");
                    }
                }
            };

            var status = await NearbyClass.Connections.SendConnectionRequestAsync(mGoogleApiClient, myName, endpointId, myPayload, connectionResponseCallback, this);

            Log("connectTo: " + status.IsSuccess);
        }
        async Task ConnectTo (string endpointId, string endpointName)
		{
			DebugLog ("connectTo:" + endpointId + ":" + endpointName);

			string myName = null;
			byte[] myPayload = null;
			var connectionResponseCallback = new ConnectionResponseCallback ();

			connectionResponseCallback.OnConnectionResponseImpl = (remoteEndpointId, status, payload) => {
				Log.Debug (TAG, "onConnectionResponse:" + remoteEndpointId + ":" + status);
				if (status.IsSuccess) {
					DebugLog ("onConnectionResponse: " + endpointName + " SUCCESS");
					Toast.MakeText (this, "Connected to " + endpointName,
						ToastLength.Short).Show ();

					mOtherEndpointId = remoteEndpointId;
					UpdateViewVisibility (NearbyConnectionState.Connected);
				} else {
					DebugLog ("onConnectionResponse: " + endpointName + " FAILURE");
				}
			};

			await NearbyClass.Connections.SendConnectionRequestAsync (mGoogleApiClient, myName, endpointId, 
                myPayload, connectionResponseCallback, this);
		}