lulibrary / Background-Beacon-Monitoring

Cordova Plugin to allow background monitoring of beacons and sending interactions to Library Journeys server
Apache License 2.0
8 stars 10 forks source link

Can't find BackgroundBeaconMonitoring #5

Open eprin opened 7 years ago

eprin commented 7 years ago

@stephenrob

I have added your plugin, i can see it in my package.json but when I try to find it in my typescript code like : "console.log(cordova.plugins);" i don't see BackgroundBeaconMonitoring class and its method. How do you call BackgroundBeaconMonitoring in your app ??

stephenrob commented 7 years ago

I've currently not looked into typescript and angular2. The application we are developing with Ionic is still Ionic v1 and Angular 1.5.3 (I'm just cleaning up some of the code before open sourcing this).

Below is an extract from one of our AngularJS services that uses the BackgroundBeaconMonitoring Plugin.

Once you have added the plugin the javascript files should be available to use, they are auto included like all other plugin files, make sure you have android added to your project as this plugin currently only works for android. At this stage you can just call the plugin methods documented in the README.

In most cases you will need to customise the code in this plugin, generally the BeaconLoggingMonitorNotifier and BeaconLoggingRangeNotifier to suit your individual application needs, not just adding the plugin.

angular.module('myJourneys.services').service('BeaconMonitoringService', function($rootScope,
                                                                                  $ionicPlatform,
                                                                                  $cordovaBeacon,
                                                                                  $cordovaLocalNotification,
                                                                                  $cordovaDevice,
                                                                                  BEACON_UUID,
                                                                                  BEACON_GLOBAL_MAJOR,
                                                                                  BEACON_GLOBAL_MINOR,
                                                                                  BEACON_REGIONS,
                                                                                  API_SERVER_URL,
                                                                                  API_VERSION,
                                                                                  PreferencesService,
                                                                                  RegionEventsService,
                                                                                  BeaconEventsService,
                                                                                  AuthService,
                                                                                  $timeout) {

  var BeaconMonitoring = function() {};

  ...

  var requestPermissions = function() {
    $cordovaBeacon.requestAlwaysAuthorization();
    $cordovaLocalNotification.promptForPermission();
    BackgroundBeaconMonitoring.requestPermissions();
  };

  var initAndroidService = function() {

    $ionicPlatform.ready(function() {

      var device_id = $cordovaDevice.getUUID();

      BackgroundBeaconMonitoring.startService(AuthService.token(), AuthService.email(), device_id, API_SERVER_URL, API_VERSION, PreferencesService.readSendMovement(), function() {

        BEACON_REGIONS.forEach(function(region) {

          var identifier = region.identifier;
          var uuid, major, minor = "";

          if (region.uuid === undefined) {
            uuid = BEACON_UUID;
          } else {
            uuid = region.uuid;
          }
          if (region.major === undefined) {
            major = BEACON_GLOBAL_MAJOR;
          } else {
            major = region.major;
          }
          if (region.minor === undefined) {
            minor = BEACON_GLOBAL_MINOR;
          } else {
            minor = region.minor;
          }

          BackgroundBeaconMonitoring.startMonitoringRegion(identifier, uuid, major, minor, function() {}, function() {});
          BackgroundBeaconMonitoring.startRangingRegion(identifier, uuid, major, minor, function() {}, function() {});

        });

      }, function() {});

    });

  };

  BeaconMonitoring.prototype.init = function() {

    $ionicPlatform.ready(function() {

      requestPermissions();

      ...

      initAndroidService();

    });

  };

  return BeaconMonitoring;

});