Пример #1
0
 @Override
 public void onConnectionRequest(
     final String remoteEndpointId,
     final String remoteDeviceId,
     final String remoteEndpointName,
     byte[] payload) {
   if (mIsHost) {
     byte[] myPayload = null;
     // Automatically accept all requests
     Nearby.Connections.acceptConnectionRequest(
             mGoogleApiClient, remoteEndpointId, myPayload, this)
         .setResultCallback(
             new ResultCallback<Status>() {
               @Override
               public void onResult(Status status) {
                 if (status.isSuccess()) {
                   clients.add(
                       new EndpointInfo(
                           remoteEndpointId, remoteDeviceId, null, remoteEndpointName));
                   SystemUtils.toast(ServerActivity.this, "Connected to " + remoteEndpointName);
                 } else {
                   SystemUtils.toast(
                       ServerActivity.this, "Failed to connect to: " + remoteEndpointName);
                 }
               }
             });
   } else {
     // Clients should not be advertising and will reject all connection requests.
     Nearby.Connections.rejectConnectionRequest(mGoogleApiClient, remoteEndpointId);
   }
 }
Пример #2
0
  public void setReady(Boolean ready) {
    this.ready = ready;

    MainActivity.event.setType(Evenement.EventType.readyChanged);
    MainActivity.event.setAllPlayers(Game.allPlayers);

    Nearby.Connections.sendReliableMessage(
        MainActivity.mGoogleApiClient,
        MainActivity.hosterId,
        MainActivity.serialize(MainActivity.event));
  }
Пример #3
0
  private void startAdvertising() {
    SystemUtils.toast(ServerActivity.this, "startAdvertising()");
    if (!NetworkUtils.isConnectedToNetwork(this)) {
      SystemUtils.toast(ServerActivity.this, "!isConnectedToNetwork()");
      // return;
    }

    // Identify that this device is the host
    mIsHost = true;

    // Advertising with an AppIdentifer lets other devices on the
    // network discover this application and prompt the user to
    // install the application.
    List<AppIdentifier> appIdentifierList = new ArrayList<>();
    appIdentifierList.add(new AppIdentifier(getPackageName()));
    AppMetadata appMetadata = new AppMetadata(appIdentifierList);

    // The advertising timeout is set to run indefinitely
    // Positive values represent timeout in milliseconds
    long NO_TIMEOUT = 0L;

    String name = null;
    Nearby.Connections.startAdvertising(mGoogleApiClient, name, appMetadata, NO_TIMEOUT, this)
        .setResultCallback(
            new ResultCallback<Connections.StartAdvertisingResult>() {
              @Override
              public void onResult(Connections.StartAdvertisingResult result) {
                if (result.getStatus().isSuccess()) {
                  SystemUtils.toast(ServerActivity.this, "Device is advertising");
                } else {
                  int statusCode = result.getStatus().getStatusCode();
                  SystemUtils.toast(
                      ServerActivity.this, "Advertising failed - see statusCode for more details");
                }
              }
            });
  }