Giter Site home page Giter Site logo

killfish / cordova-plugin-ibeacon Goto Github PK

View Code? Open in Web Editor NEW

This project forked from petermetz/cordova-plugin-ibeacon

0.0 2.0 0.0 214 KB

An iBeacon plugin for Phonegap / Cordova 3.x and upwards. Supports iOS, and later on possibly Android (contributions are welcome)

License: Apache License 2.0

Objective-C 14.43% Dart 2.78% CSS 4.65% JavaScript 78.13%

cordova-plugin-ibeacon's Introduction

iBeacon Cordova Plugin Cordova / Phonegap iBeacon plugin

Features

  • Ranging
  • Monitoring
  • Advertising as iBeacon (only works in foreground mode)

Installation

cordova plugin add https://github.com/petermetz/cordova-plugin-ibeacon.git

Usage

The plugin's API closely mimics the one exposed through the CLLocationManager in iOS 7. There is some added sugar as well, like the ability to interact with multiple iBeacons through a single call.

Standard CLLocationManager functions

Creating CLBeaconRegion DTOs
/**
 * Function that creates a CLBeaconRegion data transfer object.
 * 
 * @throws Error if the CLBeaconRegion cannot be created.
 */
function createBeacon() {
   var identifier = 'beaconAtTheMacBooks'; // optional
   var major = 1111; // optional
   var minor = 2222; // optional
   var uuid = '9C3B7561-1B5E-4B80-B7E9-31183E73B0FB'; // mandatory

   // throws an error if the parameters are not valid
   var beacon = new IBeacon.CLBeaconRegion(uuid, major, minor, identifier);
   return beacon;   
} 
Start monitoring a single iBeacon

var onDidDetermineStateCallback = function (result) {
     console.log(result.state);
};

var beacon = createBeacon();
IBeacon.startMonitoringForRegion(beacon, onDidDetermineStateCallback);

Stop monitoring a single iBeacon

var beacon = createBeacon();
IBeacon.stopMonitoringForRegion(beacon);

Start ranging a single iBeacon

var onDidRangeBeacons = function (result) {
   console.log('onDidRangeBeacons() ', result);
};

var beacon = createBeacon();
IBeacon.startRangingBeaconsInRegion(beacon, onDidRangeBeacons);


Stop ranging a single iBeacon

var beacon = createBeacon();
IBeacon.stopRangingBeaconsInRegion(beacon);

Determine if advertising is turned on.
IBeacon.isAdvertising(function(pluginResult) {
    var isAdvertising = pluginResult.isAdvertising;
    console.log('isAdvertising:' + isAdvertising);
});
Start advertising device as an iBeacon
var beacon = createBeacon();
var onPeripheralManagerDidStartAdvertising = function(pluginResult) {
    console.log('onPeripheralManagerDidStartAdvertising() pluginResult: ', pluginResult);
}

IBeacon.startAdvertising(beacon, onPeripheralManagerDidStartAdvertising);

Stopping the advertising
IBeacon.stopAdvertising(); // optionally you can specify a success callback as the first parameter

Convenience methods

Handle multiple beacons with the same call:

var beacon1 = createBeacon(); 
var beacon2 = createBeacon(); 
var beacon3 = createBeacon(); 
var beacons = [beacon1, beacon2, beacon3]; 

IBeacon.startMonitoringForRegions(beacons); 
IBeacon.startRangingBeaconsInRegions(beacons);

Contributions

Contributions are welcome at all times, please make sure that the tests are running without errors before submitting a pull request.

How to execute the tests - OS X

Prerequisites Of The Test Runner

dart test/run_tests.dart

Executing the test runner will do the following:

  • Generates a Cordova project
  • Add the iOS platform
  • Installs the iBeacon plugin from the local file-system.
  • Launches XCode by opening the project.

How to execute the tests - Non OS X

  • Open an app which has Cordova iBeacon plugin installed in XCode
  • Install it onto a device or simulator
  • Open Safari
  • Go to the dev tools window
  • Paste the code below (the contents of test/test_www_assets/js/tests.js) into the Javascript console
  • Run the snippet, there should not be any errors.
try {
	function createBeacon(index) {
		var addition = parseInt(index);
		addition = isFinite(addition) ? addition : 0;
		var identifier = 'cordova-ibeacon-plugin-test'; // optional
		var major = 1111; // optional
		var minor = 1111 + addition; // optional
		var uuid = '9C3B7561-1B5E-4B80-B7E9-31183E73B0FB'; // mandatory

		// throws an error if the parameters are not valid
		var beacon = new IBeacon.CLBeaconRegion(uuid, major, minor, identifier);
		return beacon;
	}

	// should not throw any errors since the major and minor parameters are optional
	var beaconWithoutMajorOrMinor = new IBeacon.CLBeaconRegion('dummyUuid', null, null, 'dummyIdentifier');
	if (!(beaconWithoutMajorOrMinor instanceof IBeacon.CLBeaconRegion)) {
		throw new Error('Test failed. CLBeaconRegion constructor did not return an instance of CLBeaconRegion');
	}

	// should not throw any errors since the major and minor parameters are optional
	var beaconWithoutMajorOrMinor2 = new IBeacon.CLBeaconRegion('dummyUuid', undefined, undefined, 'dummyIdentifier');
	if (!(beaconWithoutMajorOrMinor2 instanceof IBeacon.CLBeaconRegion)) {
		throw new Error('Test failed. CLBeaconRegion constructor did not return an instance of CLBeaconRegion');
	}

	// should throw an error, because major and minor has to be integers, if they were defined
	var exceptionThrown = false;
	try {
		new IBeacon.CLBeaconRegion('dummyUuid', '', '', 'dummyIdentifier');
	} catch (error) {
		exceptionThrown = true;
	}
	if (exceptionThrown !== true) {
		throw new Error('Test failed. CLBeaconRegion constructor accepted major/minor to be String');
	}

	// should throw an error, because major and minor has to be integers, if they were defined
	var exceptionThrown = false;
	try {
		new IBeacon.CLBeaconRegion('dummyUuid', NaN, NaN, 'dummyIdentifier');
	} catch (error) {
		exceptionThrown = true;
	}
	if (exceptionThrown !== true) {
		throw new Error('Test failed. CLBeaconRegion constructor accepted major/minor to be NaN');
	}

	var b1 = createBeacon();
	var b2 = createBeacon();
	var b3 = createBeacon();

	var arrayOfBeacons = [b1, b2, b3];

	var onDidDetermineStateCallback = function (result) {
		console.log(result.state);
	};

	var beacon = createBeacon();
	IBeacon.startMonitoringForRegion(beacon, onDidDetermineStateCallback);
	IBeacon.stopMonitoringForRegion(beacon);

	IBeacon.startMonitoringForRegions(arrayOfBeacons, onDidDetermineStateCallback);
	IBeacon.stopMonitoringForRegions(arrayOfBeacons);

	var onDidRangeBeacons = function (result) {
		console.log('onDidRangeBeacons() ', result);
	};
	IBeacon.startRangingBeaconsInRegion(beacon, onDidRangeBeacons);
	IBeacon.stopRangingBeaconsInRegion(beacon);

	IBeacon.startRangingBeaconsInRegions(arrayOfBeacons, onDidRangeBeacons);
	IBeacon.stopRangingBeaconsInRegions(arrayOfBeacons);

	IBeacon.isAdvertising(function (pluginResult) {
		var isAdvertising = pluginResult.isAdvertising;
		console.log('isAdvertising:' + isAdvertising);
		if (isAdvertising === true) {
			throw new Error('Test case failed for `isAdvertising` #1');
		}

		// TODO This is ugly, define more top level callbacks to make it cleaner.
		var onPeripheralManagerDidStartAdvertising = function (pluginResult) {
			console.log('onPeripheralManagerDidStartAdvertising() pluginResult: ', pluginResult);

			IBeacon.isAdvertising(function (pluginResult) {
				var isAdvertising = pluginResult.isAdvertising;
				console.log('isAdvertising:' + isAdvertising);
				if (isAdvertising !== true) {
					throw new Error('Test case failed for `isAdvertising` #2');
				}
				IBeacon.stopAdvertising(function () {

					IBeacon.isAdvertising(function (pluginResult) {
						var isAdvertising = pluginResult.isAdvertising;
						console.log('isAdvertising:' + isAdvertising);
						// FIXME The CBPeripheralManager is not KVO compilant and provides no way to
						// get notified when the advertising really shut down.
						// if (isAdvertising === true) {
						//   throw new Error('Test case failed for `isAdvertising` #3');
						// }
					});

				});
			});
		}

		IBeacon.startAdvertising(createBeacon(), onPeripheralManagerDidStartAdvertising);
	});


	if (app && app.receivedEvent) {
		app.receivedEvent('deviceready');
	} else {
		alert('Tests were successful.');
	}


} catch (error) {
	alert('There were test failures. \n' + error.message);
}

cordova-plugin-ibeacon's People

Contributors

petermetz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.