com.google.android.gms.nearby.connection.AdvertisingOptions Java Examples

The following examples show how to use com.google.android.gms.nearby.connection.AdvertisingOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/** Broadcasts our presence using Nearby Connections so other players can find us. */
private void startAdvertising() {
  // Note: Advertising may fail. To keep this demo simple, we don't handle failures.
  connectionsClient.startAdvertising(
          codeName, getPackageName(), connectionLifecycleCallback,
          new AdvertisingOptions.Builder().setStrategy(STRATEGY).build());
}
 
Example #2
Source File: ConnectionsActivity.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the device to advertising mode. It will broadcast to other devices in discovery mode.
 * Either {@link #onAdvertisingStarted()} or {@link #onAdvertisingFailed()} will be called once
 * we've found out if we successfully entered this mode.
 */
protected void startAdvertising() {
  mIsAdvertising = true;
  final String localEndpointName = getName();

  AdvertisingOptions.Builder advertisingOptions = new AdvertisingOptions.Builder();
  advertisingOptions.setStrategy(getStrategy());

  mConnectionsClient
      .startAdvertising(
          localEndpointName,
          getServiceId(),
          mConnectionLifecycleCallback,
          advertisingOptions.build())
      .addOnSuccessListener(
          new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void unusedResult) {
              logV("Now advertising endpoint " + localEndpointName);
              onAdvertisingStarted();
            }
          })
      .addOnFailureListener(
          new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
              mIsAdvertising = false;
              logW("startAdvertising() failed.", e);
              onAdvertisingFailed();
            }
          });
}
 
Example #3
Source File: RobocarAdvertiser.java    From robocar with Apache License 2.0 5 votes vote down vote up
public final void startAdvertising() {
    if (!mGoogleApiClient.isConnected()) {
        Log.d(TAG, "Google Api Client not connected");
        return;
    }
    if (mAdvertisingInfo == null) {
        Log.d(TAG, "Can't start advertising, no advertising info.");
        return;
    }
    if (mAdvertisingLiveData.getValue()) {
        Log.d(TAG, "Already advertising");
        return;
    }

    // Pre-emptively set this so the check above catches calls while we wait for a result.
    mAdvertisingLiveData.setValue(true);
    Nearby.Connections.startAdvertising(mGoogleApiClient, mAdvertisingInfo.getAdvertisingName(),
            SERVICE_ID, mLifecycleCallback, new AdvertisingOptions(STRATEGY))
            .setResultCallback(new ResultCallback<Connections.StartAdvertisingResult>() {
                @Override
                public void onResult(@NonNull StartAdvertisingResult startAdvertisingResult) {
                    Status status = startAdvertisingResult.getStatus();
                    if (status.isSuccess()) {
                        Log.d(TAG, "Advertising started.");
                        mAdvertisingLiveData.setValue(true);
                    } else {
                        Log.d(TAG, String.format("Failed to start advertising. %d, %s",
                                status.getStatusCode(), status.getStatusMessage()));
                        // revert state
                        mAdvertisingLiveData.setValue(false);
                    }
                }
            });
}
 
Example #4
Source File: ConnectionsActivity.java    From Zom-Android-XMPP with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets the device to advertising mode. It will broadcast to other devices in discovery mode.
 * Either {@link #onAdvertisingStarted()} or {@link #onAdvertisingFailed()} will be called once
 * we've found out if we successfully entered this mode.
 */
protected void startAdvertising() {
  mIsAdvertising = true;
  final String localEndpointName = getName();

  mConnectionsClient
      .startAdvertising(
          localEndpointName,
          getServiceId(),
          mConnectionLifecycleCallback,
          new AdvertisingOptions(getStrategy()))
      .addOnSuccessListener(
          new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void unusedResult) {
              logV("Now advertising endpoint " + localEndpointName);
              onAdvertisingStarted();
            }
          })
      .addOnFailureListener(
          new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
              mIsAdvertising = false;
              logW("startAdvertising() failed.", e);
              onAdvertisingFailed();
            }
          });
}
 
Example #5
Source File: NearbyConnectionModule.java    From react-native-google-nearby-connection with MIT License 4 votes vote down vote up
@ReactMethod
protected void startAdvertising(final String endpointName, final String serviceId, final int strategy) {
	mIsAdvertising = true;
	onAdvertisingStarting(endpointName, serviceId);

	Strategy finalStrategy = Strategy.P2P_CLUSTER;
	switch(strategy) {
		case 0: finalStrategy = Strategy.P2P_CLUSTER;
			break;
		case 1: finalStrategy = Strategy.P2P_STAR;
			break;
	}

	final Activity activity = getCurrentActivity();
	final ConnectionsClient clientSingleton = getConnectionsClientSingleton(serviceId);
	final AdvertisingOptions advertisingOptions =  new AdvertisingOptions(finalStrategy);

       permissionsCheck(activity, Arrays.asList(getRequiredPermissions()), new Callable<Void>() {
           @Override
           public Void call() throws Exception {
			clientSingleton
				.startAdvertising(
					endpointName,
					serviceId,
					getConnectionLifecycleCallback(serviceId, "advertised"),
					advertisingOptions
				)
				.addOnSuccessListener(
					new OnSuccessListener<Void>() {
						@Override
						public void onSuccess(Void unusedResult) {
							logV("Now advertising endpoint " + endpointName + " with serviceId " + serviceId);
							onAdvertisingStarted(endpointName, serviceId);
						}
					}
				)
				.addOnFailureListener(
					new OnFailureListener() {
						@Override
						public void onFailure(@NonNull Exception e) {
							ApiException apiException = (ApiException) e;

							mIsAdvertising = false;
							logW("startAdvertising for endpointName "+ endpointName +" serviceId "+ serviceId +" failed.", e);
							onAdvertisingStartFailed(endpointName, serviceId, apiException.getStatusCode());
						}
					}
				);

               return null;
           }
       });
}
 
Example #6
Source File: MainActivity.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
/** Broadcasts our presence using Nearby Connections so other players can find us. */
private void startAdvertising() {
    // Note: Advertising may fail. To keep this demo simple, we don't handle failures.
    connectionsClient.startAdvertising(
            "mykey", getPackageName(), connectionLifecycleCallback, new AdvertisingOptions(Strategy.P2P_STAR));
}