iBeacon 监控不适用于 Cordova-plugin-ibeacon

Posted

技术标签:

【中文标题】iBeacon 监控不适用于 Cordova-plugin-ibeacon【英文标题】:iBeacon Monitoring not working with Cordova-plugin-ibeacon 【发布时间】:2015-06-07 10:43:19 【问题描述】:

当我遇到一些监控问题时,我正在使用 Cordova-plugin-iBeacon 来测距和监控我的 iBeacon。

“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】:

我也遇到过类似的问题。我发现如果我在cordova.plugins.locationManager.BeaconRegion 方法中包含minor,它会起作用。

然而,这很烦人,因为我宁愿只监视具有特定专业的任何信标。我不确定为什么会这样,因为我拥有的另一个应用程序没有指定未成年人并且它工作得很好。

【讨论】:

你能举个例子吗?我也有同样的问题,但你怎么只通过了次要的?

以上是关于iBeacon 监控不适用于 Cordova-plugin-ibeacon的主要内容,如果未能解决你的问题,请参考以下文章

后台 iBeacon 监控

iOS屏幕关闭时如何继续监控iBeacon?

iOS 多久监控一次 iBeacons?

iOS 7.1 iBeacon 监控更新对云台有何影响?

区域监控时检测 iBeacon 需要多长时间?

设备重启后 iBeacon 监控/测距