TiBeacons

iBeacon advertising and scanning in a Titanium module

141
58
Objective-C

Usage

See this example app for usage: TiBeacons Example App

Become an iBeacon:

var TiBeacons = require('org.beuckman.tibeacons');

TiBeacons.addEventListener("advertisingStatus", function(event) {
    Ti.API.info(event.status);
});

TiBeacons.startAdvertisingBeacon({
   uuid : "00000000-0000-0000-0000-000000000000",
   identifier : "TiBeacon Test",
   major: 1,
   minor: 2
});

Start monitoring for iBeacons in one or more regions. This will continue in the background if the proper UIBackgroundModes are listed in tiapp.xml. Once the app has run once, iOS will start your app and run the event handler if it finds one of the monitored regions. The app does not have to be running.


TiBeacons.startMonitoringForRegion({
    uuid : "00000000-0000-0000-0000-000000000000",
    identifier : "Test Region 1",
});

TiBeacons.startMonitoringForRegion({
    uuid : "00000000-0000-0000-0000-000000000001",
    identifier : "Test Region 2 (group-specific)",
    major: 1
});

TiBeacons.startMonitoringForRegion({
    uuid : "00000000-0000-0000-0000-000000000002",
    identifier : "Test Region 3 (device-specific)",
    major: 1,
    minor: 2
});

// To stop monitoring one single region
TiBeacons.stopMonitoringRegion({
    uuid : "00000000-0000-0000-0000-000000000002",
    identifier : "Test Region 3 (device-specific)",
    major: 1,
    minor: 2
});

// To monitor regions and get notified on awake the iPhone
TiBeacons.startMonitoringRegion({
    uuid : "00000000-0000-0000-0000-000000000002",
    identifier : "Test Region 3 (device-specific)",
    major: 1,
    minor: 2,
    notifyEntryStateOnDisplay: "YES" 
});

// To stop monitoring all regions
TiBeacons.stopMonitoringAllRegions();

// Make sure you stopped ranging all beacons after stop monitoring 
TiBeacons.stopRangingForAllBeacons();

Listen for region events:

TiBeacons.addEventListener("enteredRegion", alert);
TiBeacons.addEventListener("exitedRegion", alert);
TiBeacons.addEventListener("determinedRegionState", alert);

Start ranging beacons in a region. This takes takes more energy and will report the approximate distance of the device to the beacon.

TiBeacons.startRangingForBeacons({
    uuid : "00000000-0000-0000-0000-000000000002",
    identifier : "Test Region",
    major: 1, //optional
    minor: 2 //optional
});

Listen for the range events:

TiBeacons.addEventListener("beaconRanges", function(event) {
   alert(event.beacons);
});

Or just listen for beacon proximity changes:

TiBeacons.addEventListener("beaconProximity", function(e){
   alert("beacon "+e.major+"/"+e.minor+" is now "+e.proximity);
});

Permission and Hardware Status

Get notified when the user allows or disallows location services for your app:

TiBeacons.addEventListener("changeAuthorizationStatus", function(e){
   if (e.status != "authorized") {
      Ti.API.error("not authorized");
   }
});

Find out if Bluetooth Low Energy is supported on the current device:


if (TiBeacons.isBLESupported()) {
  Ti.API.error("BLE is supported on this device");
} else {
  Ti.API.error("BLE isn't supported on this device");
]

Find out if bluetooth is on or off (or unauthorized or unsupported or resetting):

TiBeacons.addEventListener("bluetoothStatus", function(e){
   if (e.status != "on") {
      Ti.API.error("bluetooth is not on");
   }
});

TiBeacons.requestBluetoothStatus();