petermetz / cordova-plugin-ibeacon

An iBeacon plugin for Phonegap / Cordova 3.x and upwards. Supports both iOS and Android (contributions are welcome)
Apache License 2.0
726 stars 373 forks source link

Monitoring and Ranging Multiple Beacons #190

Open LiamChapman opened 8 years ago

LiamChapman commented 8 years ago

Hi All,

The library works perfectly with one beacon, but i need to monitor several. I've looked through the issues on this project on how to do this, but it seems out of date and not to be working. It seems to start monitoring the beacons. However, in the console it only returns the determined state of one beacon.

Below is the code I currently have in place. If anyone has any suggestions or tips I'd greatly appreciate it!

Thanks,

Liam

// All Beacons to track
       var Beacons = [
    { 
        uuid        : 'xxx',
        identifier  : Pink',
        minor       : 1,
        major       : 1
    },
    {
        uuid        : 'xxx',
        identifier  : 'Blue',
        minor       : 2,
        major       : 1
    },
    {
        uuid        : 'xxx',
        identifier  : 'Green',
        minor       : 3,
        major       : 1
    }
];                 

// Beacon Plugin Delegation
var delegate = new cordova.plugins.locationManager.Delegate();

delegate.didStartMonitoringForRegion = function (result) {

    // Log to Xcode
    cordova.plugins.locationManager.appendToDeviceLog(">>> START " + JSON.stringify(result));

};

delegate.didEnterRegion = function (result) {

    // Log to Xcode
    cordova.plugins.locationManager.appendToDeviceLog(">>> ENTER " + JSON.stringify(result));

    // Start Ranging Beacon When it enters Inside Region
    cordova.plugins.locationManager.startRangingBeaconsInRegion({
        uuid        : result.region.uuid,
        identifier  : result.region.identifier,
        minor       : result.region.minor,
        major       : result.region.major
    })
    .fail(console.error)
    .done();

};

delegate.didExitRegion = function (result) {

    // Log to Xcode
    cordova.plugins.locationManager.appendToDeviceLog(">>> EXIT " + JSON.stringify(result));         

    // Stop Ranging Beacon if Outside Region
    cordova.plugins.locationManager.stopRangingBeaconsInRegion({
        identifier  : result.region.identifier,
        uuid        : result.region.uuid,               
        major       : result.region.major,
        minor       : result.region.minor
    })
    .fail(console.error)
    .done();

};

delegate.didDetermineStateForRegion = function (result) {

    // Log to Xcode
    cordova.plugins.locationManager.appendToDeviceLog(">>> DETERMINE " + JSON.stringify(result));

};

delegate.didRangeBeaconsInRegion = function (result) {

    // Log to Xcode
    cordova.plugins.locationManager.appendToDeviceLog(">>> RANGE " + JSON.stringify(result));

};

// Set Methods for Location Manager
cordova.plugins.locationManager.setDelegate(delegate);

// Ask/Check For Permission
cordova.plugins.locationManager.requestAlwaysAuthorization();

// Loop Beacons to track and setup region and monitoring.
for (var i in Beacons) {
    var b = Beacons[i];            
    if (b == undefined) continue;
    var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(b.identifier, b.uuid, b.major, b.minor);
    cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion);
}

};
LiamChapman commented 8 years ago

I think I've fixed it. I wasn't defineing the BeaconRegion correctly and passing it my ranging methods correctly. Here is what my code looks like now more or less:

// All Beacons to track
var Beacons = [
    { 
        uuid        : 'xxxxx',
        identifier  : 'Pink',
        minor       : 1,
        major       : 1
    },
    {
        uuid        : 'xxxxx',
        identifier  : 'Blue',
        minor       : 2,
        major       : 1
    },
    {
        uuid        : 'xxxxx',
        identifier  : 'Green',
        minor       : 3,
        major       : 1
    }
];                 

var BeaconRegions = [];

// loop Beacons to be used.
for (var i in Beacons) {
    var b                       = Beacons[i];
    BeaconRegions[b.identifier] = new cordova.plugins.locationManager.BeaconRegion(b.identifier, b.uuid, b.major, b.minor);
}//end for

// Beacon Plugin Delegation
var delegate = new cordova.plugins.locationManager.Delegate();

delegate.didStartMonitoringForRegion = function (result) {

    // Log to Xcode
    cordova.plugins.locationManager.appendToDeviceLog(">>> START " + JSON.stringify(result));

};

delegate.didEnterRegion = function (result) {

    // Log to Xcode
    // cordova.plugins.locationManager.appendToDeviceLog(">>> ENTER " + JSON.stringify(result));

   console.log(">>>|| ENTER DEBUG ||<<<", result.region.identifier);

    // Start Ranging Beacon When it enters Inside Region
    cordova.plugins.locationManager.startRangingBeaconsInRegion(BeaconRegions[result.region.identifier])
    .fail(console.error)
    .done();

};

delegate.didExitRegion = function (result) {

    // Log to Xcode
    // cordova.plugins.locationManager.appendToDeviceLog(">>> EXIT " + JSON.stringify(result));
    console.log(">>>|| EXIT DEBUG ||<<<", result.region.identifier);

    // Stop Ranging Beacon if Outside Region
    cordova.plugins.locationManager.stopRangingBeaconsInRegion(BeaconRegions[result.region.identifier])
    .fail(console.error)
    .done();

};

delegate.didDetermineStateForRegion = function (result) {

    // Log to Xcode
    // cordova.plugins.locationManager.appendToDeviceLog(">>> DETERMINE " + JSON.stringify(result));

    console.log(">>>|| DETERMINE DEBUG ||<<<", result.region.identifier);

};

delegate.didRangeBeaconsInRegion = function (result) {

    // Log to Xcode
    // cordova.plugins.locationManager.appendToDeviceLog(">>> RANGE " + JSON.stringify(result));

    console.log(">>>|| RANGE DEBUG ||<<<", result.region.identifier, result.beacons[0].proximity);

};

// Set Methods for Location Manager
cordova.plugins.locationManager.setDelegate(delegate);

// Ask/Check For Permission
cordova.plugins.locationManager.requestAlwaysAuthorization();

// Loop BeaconRegions to setup region monitoring.
for (var i in BeaconRegions) {
    var b = BeaconRegions[i];
    if (b == undefined) continue;            
    cordova.plugins.locationManager.startMonitoringForRegion(b);
}
andersborgabiro commented 8 years ago

You can also track on UUID without setting major and minor IDs, and then handle the distinction in software. That's how I do it in CliqTags Beacon Reader.

graphicgeek commented 8 years ago

I was doing this by just passing the major through, and leaving the minor undefined. It works great on Android. However, on iOS, it seems to depend on the direction of the wind or something, because it would work one day and not the next.

andersborgabiro commented 8 years ago

I always use major and minor wildcards. That no doubt works.

I wish also UUID could be wildcard.

graphicgeek commented 8 years ago

Yeah, that's what I'm doing now. However, I wish I could set a major and leave the minor as a wildcard. Doing that seems to work on Android, but not on iOS.

mobigaurav commented 8 years ago

I tried the way explained by LiamChapman commented on Nov 26, 2015 , but its not ranging or detecting multiple beacons , it's detecting only one.