iBeacon Monitoring无法使用Cordova-plugin-ibeacon

时间:2015-06-07 10:43:19

标签: cordova monitoring ionic ibeacon cordova-plugins

我使用Cordova-plugin-iBeacon来测量和监控我的iBeacons,同时我遇到了一些监控问题。

' startRangingBeaconsInRegion'效果很好,可以在同一区域内检测到信标,并在接近时弹出新视图。但监控功能不起作用。我用我的iPhone 5s设备进行测试。我走得很远,但什么都没发生。我希望用户在进入/退出该区域时可以收到通知消息。 ' didDetermineStateForRegion'永远不会被调用,也不会被EnterRegion / ExitRegion。

任何帮助都将非常感谢!提前谢谢!

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 个答案:

答案 0 :(得分:0)

我有类似的问题。我发现如果我在cordova.plugins.locationManager.BeaconRegion方法中包含了minor,那就可以了。

然而,这很烦人,因为我宁愿只监控任何具有特定主要信标的信标。我不确定为什么会这样,因为我的另一个应用程序没有指定未成年人,它的工作正常。