0
votes

I'm using Cordova-plugin-iBeacon for ranging and monitoring my iBeacons while I got some problems of monitoring.

The 'startRangingBeaconsInRegion' works well, beacon can be detected within the same region and new view will pop up when approaching it. But the monitoring function does not work. I used my iPhone 5s device for testing. I walked very far away but nothing happened. I expect user can receive a notification message when they enter/exit the region. 'didDetermineStateForRegion' is never called, neither didEnterRegion/ExitRegion.

Any help will highly appreciate! Thanks in advance!

var nearestBeacon = null;
  var lastPopElement = null;
  // BeaconLocationManager is a class I wrote to communicate with server and retrieve regions array. It has been tested and I can retrieve a valid array.  
  var regionsArray = BeaconLocationManager.regions();
  var exhibitionRegion = regionsArray["Exhibits"];
  var receptionRegion = regionsArray["Reception"];
  // Background notification id counter.
  var mNotificationId = 0;

  function isNearThan(beacon1, beacon2) {
    return (beacon1.accuracy > 0) && (beacon2.accuracy >0) && (beacon1.accuracy < beacon2.accuracy);
  }

  function updateNearestBeacon(beacons) {
    for ( var n = 0; n < beacons.length; ++n ) {
      var beacon =  beacons[n];
      if (!nearestBeacon) {
        nearestBeacon = beacon;
      } else {
        if (isNearThan(beacon, nearestBeacon)) {
            nearestBeacon = beacon;
        } 
      };
    };
  };

  var detectBeacons = function () {
    // Request permission from user to send notification.
    cordova.plugins.notification.local.promptForPermission();
    var delegate = new cordova.plugins.locationManager.Delegate();

    delegate.didDetermineStateForRegion = function (pluginResult) {
        cordova.plugins.locationManager.appendToDeviceLog('[DOM] didDetermineStateForRegion: '
            + JSON.stringify(pluginResult));
        BeaconLocationManager.closeTo(nearestBeacon.minor);
    };

    delegate.didExitRegion = function(pluginResult) {
        console.log('didExitRegion: ' + JSON.stringify(pluginResult));
    };

    delegate.didEnterRegion = function(pluginResult) {
        console.log('didEnterRegion: ' + JSON.stringify(pluginResult));

       // Set notification title.
       var title = "You are awesome!!!";
       // Create notification.
       cordova.plugins.notification.local.schedule({
           id: ++mNotificationId,
           title: title });
    };

    delegate.didStartMonitoringForRegion = function (pluginResult) {
        console.log('didStartMonitoringForRegion:', pluginResult);
    };

    delegate.didRangeBeaconsInRegion = function (pluginResult) {
        updateNearestBeacon(pluginResult.beacons);

        var nearestBeaconMinor = nearestBeacon.minor;

        $scope.selectedItem = BeaconLocationManager.info(nearestBeaconMinor);

        if ((lastPopElement != nearestBeaconMinor) && (nearestBeacon.accuracy <= 2.5)) {

            detailExhibit.show();

          lastPopElement = nearestBeaconMinor;
        };

        nearestBeacon = null;
    };

    cordova.plugins.locationManager.setDelegate(delegate);

    // required in iOS 8+
    cordova.plugins.locationManager.requestAlwaysAuthorization();
    // or cordova.plugins.locationManager.requestWhenInUseAuthorization(); 

    var exhibitsBeaconRegion = new cordova.plugins.locationManager.BeaconRegion(exhibitionRegion.identifier, exhibitionRegion.uuid, exhibitionRegion.major);
    var receptionBeaconRegion = new cordova.plugins.locationManager.BeaconRegion(receptionRegion.identifier, receptionRegion.uuid, receptionRegion.major);

    cordova.plugins.locationManager.startMonitoringForRegion(exhibitsBeaconRegion)
        .fail(console.error)
        .done();

    cordova.plugins.locationManager.startRangingBeaconsInRegion(exhibitsBeaconRegion)
        .fail(console.error)
        .done();

    cordova.plugins.locationManager.startMonitoringForRegion(receptionBeaconRegion)
        .fail(console.error)
        .done();

    cordova.plugins.locationManager.startRangingBeaconsInRegion(receptionBeaconRegion)
        .fail(console.error)
        .done();

  };
1

1 Answers

-1
votes

I've had a similar problem. I found that if I included at minor in the cordova.plugins.locationManager.BeaconRegion method, it would work.

This is annoying, however, because I would rather just monitor for any beacons with a specific major. I'm not sure why this is the case, since another app I have does not specify the minor and it works just fine.