com.google.android.gms.nearby.messages.SubscribeOptions Java Examples

The following examples show how to use com.google.android.gms.nearby.messages.SubscribeOptions. 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 nearby-beacons with MIT License 6 votes vote down vote up
private void subscribe() {
    Log.d(TAG, "Subscribing…");
    SubscribeOptions options = new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .build();
    //Active subscription for foreground messages
    Nearby.Messages.subscribe(mGoogleApiClient,
            mMessageListener, options);

    //Passive subscription for background messages
    Intent serviceIntent = new Intent(this, BeaconService.class);
    PendingIntent trigger = PendingIntent.getService(this, 0,
            serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    ResultCallback<Status> callback = new BackgroundRegisterCallback();
    Nearby.Messages.subscribe(mGoogleApiClient, trigger, options)
            .setResultCallback(callback);

}
 
Example #2
Source File: MainActivity.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribes to messages from nearby devices and updates the UI if the subscription either
 * fails or TTLs.
 */
private void subscribe() {
    Log.i(TAG, "Subscribing");
    mNearbyDevicesArrayAdapter.clear();
    SubscribeOptions options = new SubscribeOptions.Builder()
            .setStrategy(PUB_SUB_STRATEGY)
            .setCallback(new SubscribeCallback() {
                @Override
                public void onExpired() {
                    super.onExpired();
                    Log.i(TAG, "No longer subscribing");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mSubscribeSwitch.setChecked(false);
                        }
                    });
                }
            }).build();

    Nearby.Messages.subscribe(mGoogleApiClient, mMessageListener, options)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Subscribed successfully.");
                    } else {
                        logAndShowSnackbar("Could not subscribe, status = " + status);
                        mSubscribeSwitch.setChecked(false);
                    }
                }
            });
}
 
Example #3
Source File: MainActivity.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Calls {@link Messages#subscribe(GoogleApiClient, MessageListener, SubscribeOptions)},
 * using a {@link Strategy} for BLE scanning. Attaches a {@link ResultCallback} to monitor
 * whether the call to {@code subscribe()} succeeded or failed.
 */
private void subscribe() {
    // In this sample, we subscribe when the activity is launched, but not on device orientation
    // change.
    if (mSubscribed) {
        Log.i(TAG, "Already subscribed.");
        return;
    }

    SubscribeOptions options = new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .build();

    Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Subscribed successfully.");
                        startService(getBackgroundSubscribeServiceIntent());
                    } else {
                        Log.e(TAG, "Operation failed. Error: " +
                                NearbyMessagesStatusCodes.getStatusCodeString(
                                        status.getStatusCode()));
                    }
                }
            });
}
 
Example #4
Source File: MainActivity.java    From hello-beacons with Apache License 2.0 5 votes vote down vote up
/**
 * Calls {@link Messages#subscribe(GoogleApiClient, MessageListener, SubscribeOptions)},
 * using a {@link Strategy} for BLE scanning. Attaches a {@link ResultCallback} to monitor
 * whether the call to {@code subscribe()} succeeded or failed.
 */
private void subscribe() {
    // In this sample, we subscribe when the activity is launched, but not on device orientation
    // change.
    if (mSubscribed) {
        Log.i(TAG, "Already subscribed.");
        return;
    }

    SubscribeOptions options = new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .build();

    Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Subscribed successfully.");
                        startService(getBackgroundSubscribeServiceIntent());
                    } else {
                        Log.e(TAG, "Operation failed. Error: " +
                                NearbyMessagesStatusCodes.getStatusCodeString(
                                        status.getStatusCode()));
                    }
                }
            });
}
 
Example #5
Source File: MainActivity.java    From hello-beacons with Apache License 2.0 5 votes vote down vote up
/**
 * Calls {@link Messages#subscribe(GoogleApiClient, MessageListener, SubscribeOptions)},
 * using a {@link Strategy} for BLE scanning. Attaches a {@link ResultCallback} to monitor
 * whether the call to {@code subscribe()} succeeded or failed.
 */
private void subscribe() {
    // In this sample, we subscribe when the activity is launched, but not on device orientation
    // change.
    if (mSubscribed) {
        Log.i(TAG, "Already subscribed.");
        return;
    }

    SubscribeOptions options = new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .build();

    Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Subscribed successfully.");
                        startService(getBackgroundSubscribeServiceIntent());
                    } else {
                        Log.e(TAG, "Operation failed. Error: " +
                                NearbyMessagesStatusCodes.getStatusCodeString(
                                        status.getStatusCode()));
                    }
                }
            });
}
 
Example #6
Source File: NearbySubscription.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
protected static SubscribeOptions createSubscribeOptions() {
    MessageFilter messageFilter = PhysicalWebBleClient.getInstance().modifyMessageFilterBuilder(
            new MessageFilter.Builder())
            .build();
    return new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .setFilter(messageFilter)
            .build();
}
 
Example #7
Source File: MessageFragment.java    From io16experiment-master with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribes to messages from nearby devices. If not successful, attempts to resolve any error
 * related to Nearby permissions by displaying an opt-in dialog. Registers a callback which
 * updates state when the subscription expires.
 */
private void subscribe() {
    LogUtils.LOGE(TAG, "trying to subscribe");
    // Cannot proceed without a connected GoogleApiClient. Reconnect and execute the pending
    // task in onConnected().
    if (!mGoogleApiClient.isConnected()) {
        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    } else {
        SubscribeOptions options = new SubscribeOptions.Builder()
                .setStrategy(PUB_SUB_STRATEGY)
                .setCallback(new SubscribeCallback() {
                    @Override
                    public void onExpired() {
                        super.onExpired();
                        LogUtils.LOGE(TAG, "no longer subscribing");
                        updateSharedPreference(Constants.KEY_SUBSCRIPTION_TASK, Constants.TASK_NONE);
                    }
                }).build();

        Nearby.Messages.subscribe(mGoogleApiClient, mMessageListener, options)
                .setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(@NonNull Status status) {
                        if (mProgressDialog != null && mProgressDialog.isShowing()) {
                            mProgressDialog.dismiss();
                        }

                        if (status.isSuccess()) {
                            LogUtils.LOGE(TAG, "subscribed successfully");
                        } else {
                            LogUtils.LOGE(TAG, "could not subscribe");
                            handleUnsuccessfulNearbyResult(status);
                        }
                    }
                });
    }
}
 
Example #8
Source File: NearbySubscription.java    From 365browser with Apache License 2.0 5 votes vote down vote up
protected static SubscribeOptions createSubscribeOptions() {
    MessageFilter messageFilter = PhysicalWebBleClient.getInstance().modifyMessageFilterBuilder(
            new MessageFilter.Builder())
            .build();
    return new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .setFilter(messageFilter)
            .build();
}