【发布时间】: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();
};
【问题讨论】:
标签: cordova monitoring ionic ibeacon cordova-plugins