You will need an API key for the NearBee SDK service to work.
allprojects {
repositories {
…
maven {
url "https://mobstac.jfrog.io/artifactory/beaconstacandroidsdk-gradle-release/"
}
…
}
}
dependencies {
…
implementation 'co.nearbee:nearbeesdk:2.1.30'
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
NearBee requires the location permission to scan for Beacons.
<application>
…
…
<meta-data
android:name="co.nearbee.api_key"
android:value="MY_DEV_TOKEN" />
<meta-data
android:name="co.nearbee.organization_id"
android:value="123" />
…
…
</application>
NearBee.Builder builder = new NearBee.Builder(context)
.setBackgroundNotificationsEnabled(true)
.setCallBackInterval(5);
nearBee = builder.build();
setBackgroundNotificationsEnabled(true);
Enables notifications from Nearby beacons in the Background. If any requirements are not met an error will be logged by the Background service. To change the behaviour (enabled to disabled and vice-versa) you need to re-initialize the SDK via the Builder as mentioned above.
setCallBackInterval(timeInSeconds);
Sets the time between callbacks for displaying beacon notifications in the foreground. This has no effect on the background notification behaviour.
// Start scanning and get updates
nearBee.startScanning(new NearBeaconListener() {
@Override
public void onUpdate(ArrayList<NearBeacon> beaconsInRange) {
// An updated list of beacons currently in range
}
@Override
public void onBeaconLost(ArrayList<NearBeacon> lostBeacons) {
// List of beacons that went out of range
}
@Override
public void onBeaconFound(ArrayList<NearBeacon> foundBeacons) {
// List of beacons that appeared after last update
}
@Override
public void onError(NearBeeException exception) {
// Any error preventing Nearbee from scanning for beacons.
// This error must be resolved and nearBee.startScanning should be called again.
}
});
// Stop scanning
nearBee.stopScanning();
// Should ideally be done when the app goes to the background
// Change background notification state after initialization
nearBee.enableBackgroundNotifications(true);
// This clears the cache and forces the server to re-fetch beacon notifications
nearBee.clearNotificationCache();
There are two types of attachments -
These are extracted from the physical web url of the beacon
beacon.getAttachments().getPhysicalWebAttachment();
PhysicalWeb properties
// Title
physicalWeb.getTitle();
// Description
physicalWeb.getDescription();
// Url for the icon
physicalWeb.getIconURL();
// Physical web url
physicalWeb.getUrl();
// Returns if the url is currexntly active
physicalWeb.isActive();
This comes from Google Nearby attachment for a specific beacon
// returns a list of ProximityAttachment objects
beacon.getAttachments().getProximityApiAttachments();
ProximityAttachment properties
In addition to all the properties from physical web, ProximityAttachment has extra properties
// Url for banner image
proximityAttachment.getBannerImageURL();
// Banner type, portrait = 1 or landscape = 2
proximityAttachment.getBannerType();
// ISO code for the language
proximityAttachment.getLanguage();
Get the ProximityAttachment
for the current device locale language.
Will return null
if ProximityAttachment is not available for that language
beacon.getAttachmentForCurrentLanguage(context);
Returns a BeaconAttachment
object, which will be a ProximityAttachment
if an attachment is found for the current language, or a PhysicalWeb
otherwise
Will return null
if no attachments are present for this beacon
beacon.getBestAvailableAttachment(context);
// BeaconAttachment is the base class for both ProximityAttachment and PhysicalWeb
beacon.launchUrl(context, beaconAttachment);
Beacons may contain a Business object which is the Place
associated with the beacon
Business business = beacon.getBusiness();
They have following properties
// Color associated with the place (int)
business.getColor();
// Color code associated with the place (Hex code)
business.getColorCode();
// Cover image url
business.getCoverURL();
// Google place id
business.getGooglePlaceID();
// Place name
business.getName();
// Icon image url
business.getIconURL();
package com.your_app_package;
import android.content.Context;
import android.content.Intent;
import co.nearbee.NotificationManager;
import co.nearbee.models.BeaconAttachment;
import co.nearbee.models.NearBeacon;
public class MyNotificationManager extends NotificationManager {
public MyNotificationManager(Context context) {
super(context);
}
@Override
public Intent getAppIntent(Context context) {
// This intent is for handling grouped notification click
return new Intent(context, MainActivity.class);
}
@Override
public Intent getBeaconIntent(Context context, NearBeacon nearBeacon) {
// This intent is for handling individual notification click
// Pass the intent of the activity that you want to be opened on click
if (nearBeacon.getBusiness() != null) {
BeaconAttachment attachment = nearBeacon.getBestAvailableAttachment(context);
if (attachment != null) {
final Intent intent = new Intent(context, MainActivity.class);
// pass the url from the beacon, so that it can be opened from your activity
intent.putExtra("url", attachment.getUrl());
return intent;
}
}
return null;
}
}
<meta-data
android:name="co.nearbee.notification_util"
android:value=".MyNotificationManager" />
nearBee.startGeoFenceMonitoring().setSuccessListener(new SuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("NearBee GeoFence", "Setup success");
}
}).setErrorListener(new ErrorListener() {
@Override
public void onError(Exception e) {
Log.d("NearBee GeoFence", "Setup failed");
}
});
nearBee.stopGeoFenceMonitoring().setSuccessListener(new SuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("NearBee GeoFence", "Removal success");
}
}).setErrorListener(new ErrorListener() {
@Override
public void onError(Exception e) {
Log.d("NearBee GeoFence", "Removal failed");
}
});
boolean enabled = nearBee.isGeoFenceMonitoringEnabled();
package com.your_app_package;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import co.nearbee.geofence.GeoAttachment;
import co.nearbee.geofence.GeoNotificationManager;
import co.nearbee.geofence.repository.models.GeoFence;
public class MyGeoNotificationManager extends GeoNotificationManager {
public MyGeoNotificationManager(Context context) {
super(context);
}
@Nullable
@Override
public Intent getGeoFenceIntent(Context context, GeoFence geoFence) {
GeoAttachment attachment = geoFence.getAttachment();
if (attachment != null) {
final Intent intent = new Intent(context, MyActivity.class);
// pass the url from the beacon, so that it can be opened from your activity
intent.putExtra("url", attachment.getUrl());
return intent;
}
return null;
}
@Override
public Intent getAppIntent(Context context) {
return new Intent(context, MainActivity.class);
}
}
<meta-data
android:name="co.nearbee.geo_notification_util"
android:value=".MyGeoNotificationManager" />
By default the NearBee SDK displays the notifications. To disable this and take control of Beacon and GeoFence events follow this guide.