Skip to content

Commit

Permalink
feat(BeaconRegion.js): On Android wild-card UUIDs are now allowed. iO…
Browse files Browse the repository at this point in the history
…S is still a no-go.
  • Loading branch information
petermetz committed Dec 26, 2017
1 parent 094e449 commit 4553ee7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 36 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<keywords>cordova,ibeacon,beacon,bluetooth,le</keywords>
<repo>https://github.com/petermetz/cordova-plugin-ibeacon.git</repo>

<!-- Version is set to anything because the only feature we use is the device.platform property which was available
since forever. The added benefit is that we don't force the consumers of this plugin to use a certain version of
the device plugin. -->
<dependency id="cordova-plugin-device" version="*" />

<!-- Third party libraries -->
<js-module name="underscorejs" src="www/lib/underscore-min-1.6.js">
<runs/>
Expand Down
94 changes: 58 additions & 36 deletions www/model/BeaconRegion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 4553ee7

Please sign in to comment.