From 4553ee751a8854dcfc30d392f714484ca7cc5690 Mon Sep 17 00:00:00 2001 From: "Peter (Somogyvari) Metz" Date: Mon, 25 Dec 2017 22:45:16 -0800 Subject: [PATCH] feat(BeaconRegion.js): On Android wild-card UUIDs are now allowed. iOS is still a no-go. --- README.md | 41 +++++++++++++++++ plugin.xml | 5 +++ www/model/BeaconRegion.js | 94 ++++++++++++++++++++++++--------------- 3 files changed, 104 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index a233743..aeb798c 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,47 @@ cordova.plugins.locationManager.isBluetoothEnabled() ``` +##### Specify wildcard UUID (Android only) + +```javascript +var uuid = cordova.plugins.locationManager.BeaconRegion.WILDCARD_UUID; //wildcard +var identifier = 'SomeIdentifier'; +var major = undefined; +var minor = undefined; +var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid, major, minor); + +var logToDom = function (message) { + console.warn(message); +}; + +var delegate = new cordova.plugins.locationManager.Delegate(); + +delegate.didDetermineStateForRegion = function (pluginResult) { + + logToDom('[DOM] didDetermineStateForRegion: ' + JSON.stringify(pluginResult)); + + cordova.plugins.locationManager.appendToDeviceLog('[DOM] didDetermineStateForRegion: ' + + JSON.stringify(pluginResult)); +}; + +delegate.didStartMonitoringForRegion = function (pluginResult) { + console.log('didStartMonitoringForRegion:', pluginResult); + + logToDom('didStartMonitoringForRegion:' + JSON.stringify(pluginResult)); +}; + +delegate.didRangeBeaconsInRegion = function (pluginResult) { + logToDom('[DOM] didRangeBeaconsInRegion: ' + JSON.stringify(pluginResult)); +}; + +cordova.plugins.locationManager.setDelegate(delegate); + +cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion) + .fail(function(e) { console.error(e); }) + .done(); + +``` + ## Contributions > Contributions are welcome at all times, please make sure that the tests are running without errors diff --git a/plugin.xml b/plugin.xml index f005558..ca4f7bf 100644 --- a/plugin.xml +++ b/plugin.xml @@ -7,6 +7,11 @@ cordova,ibeacon,beacon,bluetooth,le https://github.com/petermetz/cordova-plugin-ibeacon.git + + + diff --git a/www/model/BeaconRegion.js b/www/model/BeaconRegion.js index c268662..604839f 100644 --- a/www/model/BeaconRegion.js +++ b/www/model/BeaconRegion.js @@ -6,9 +6,9 @@ to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -22,73 +22,95 @@ var Region = require('com.unarin.cordova.beacon.Region'); /** * Constructor for {CLBeaconRegion}. - * + * * @param {String} identifier @see {CLRegion} - * - * @param {String} uuid The proximity ID of the beacon being targeted. + * + * @param {String} uuid The proximity ID of the beacon being targeted. * This value must not be blank nor invalid as a UUID. - * + * * @param {Number} major The major value that you use to identify one or more beacons. * @param {Number} minor The minor value that you use to identify a specific beacon. * * @param {BOOL} notifyEntryStateOnDisplay - * + * * @returns {BeaconRegion} An instance of {BeaconRegion}. */ function BeaconRegion (identifier, uuid, major, minor, notifyEntryStateOnDisplay){ - // Call the parent constructor, making sure (using Function#call) - // that "this" is set correctly during the call - Region.call(this, identifier); - - BeaconRegion.checkUuid(uuid); - BeaconRegion.checkMajorOrMinor(major); - BeaconRegion.checkMajorOrMinor(minor); - - this.uuid = uuid; + // Call the parent constructor, making sure (using Function#call) + // that "this" is set correctly during the call + Region.call(this, identifier); + + BeaconRegion.checkUuid(uuid); + BeaconRegion.checkMajorOrMinor(major); + BeaconRegion.checkMajorOrMinor(minor); + + // Coerce the wildcard UUID into a value of `undefined` which is the actual wildcard value for the Android Java code + if (uuid === BeaconRegion.WILDCARD_UUID) { + this.uuid = undefined; + } else { + this.uuid = uuid; + } + this.major = major; this.minor = minor; this.notifyEntryStateOnDisplay = notifyEntryStateOnDisplay; - this.typeName = 'BeaconRegion'; + this.typeName = 'BeaconRegion'; }; // Create a BeaconRegion.prototype object that inherits from Region.prototype. // Note: A common error here is to use "new Region()" to create the -// BeaconRegion.prototype. That's incorrect for several reasons, not least -// that we don't have anything to give Region for the "identifier" -// argument. The correct place to call Region is above, where we call +// BeaconRegion.prototype. That's incorrect for several reasons, not least +// that we don't have anything to give Region for the "identifier" +// argument. The correct place to call Region is above, where we call // it from BeaconRegion. BeaconRegion.prototype = Object.create(Region.prototype); // Set the "constructor" property to refer to BeaconRegion BeaconRegion.prototype.constructor = BeaconRegion; +/** + * @public + * @static + * + * Use this property as the value of the `UUID` parameter to express the intent of specifying a wild-card UUID. + */ +BeaconRegion.WILDCARD_UUID = {}; + BeaconRegion.isValidUuid = function (uuid) { - var uuidValidatorRegex = this.getUuidValidatorRegex(); - return uuid.match(uuidValidatorRegex) != null; + + // https://github.com/petermetz/cordova-plugin-ibeacon/issues/328 + // If we are on Android, then allow the UUID to be specified as a wild-card (omitted) + var isAndroid = device && device.platform === "Android"; + if (uuid === BeaconRegion.WILDCARD_UUID && isAndroid) { + return true; + } + + var uuidValidatorRegex = this.getUuidValidatorRegex(); + return uuid.match(uuidValidatorRegex) != null; }; BeaconRegion.getUuidValidatorRegex = function () { - return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; + return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; }; BeaconRegion.checkUuid = function (uuid) { - if (!BeaconRegion.isValidUuid(uuid)) { - throw new TypeError(uuid + ' is not a valid UUID'); - } + if (!BeaconRegion.isValidUuid(uuid)) { + throw new TypeError(uuid + ' is not a valid UUID'); + } }; BeaconRegion.checkMajorOrMinor = function (majorOrMinor) { - if (!_.isUndefined(majorOrMinor)) { - if (!_.isFinite(majorOrMinor)) { - throw new TypeError(majorOrMinor + ' is not a finite value'); - } - - if (majorOrMinor > BeaconRegion.U_INT_16_MAX_VALUE || - majorOrMinor < BeaconRegion.U_INT_16_MIN_VALUE) { - throw new TypeError(majorOrMinor + ' is out of valid range of values.'); - } - } + if (!_.isUndefined(majorOrMinor)) { + if (!_.isFinite(majorOrMinor)) { + throw new TypeError(majorOrMinor + ' is not a finite value'); + } + + if (majorOrMinor > BeaconRegion.U_INT_16_MAX_VALUE || + majorOrMinor < BeaconRegion.U_INT_16_MIN_VALUE) { + throw new TypeError(majorOrMinor + ' is out of valid range of values.'); + } + } }; BeaconRegion.U_INT_16_MAX_VALUE = (1 << 16) - 1;