Packagecom.distriqt.extension.beacon
Classpublic final class Beacon
InheritanceBeacon Inheritance flash.events.EventDispatcher

This class represents the Beacon extension allowing access to monitor a region and range for iBeacons.

This extension implements a cross platform solution for both Android and iOS allowing you to write code to access iBeacons on both platforms without changes.

Events

When the application is running the application will receive region and ranging events directly. Region exit events can take up to a minute, however an entry event should be quick (~1 second).

The application will also receive region and ranging events when in the background as long as:

If the device has been put to sleep the entry events may take a little longer but they will still be received. The ranging events will also not neccessarily be as consistent as when the application is running in the foreground.

Android

The manifest additions required on Android are listed below, in particular you must request permissions for using bluetooth and add in the services to receive the beacon notifications.

<manifestAdditions><![CDATA[
    <manifest android:installLocation="auto">
        <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18" />
        
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        
        <application>
            <service android:enabled="true" android:exported="true" android:isolatedProcess="false" android:label="iBeacon" android:name="com.distriqt.extension.beacon.services.BeaconService"></service>
            <service android:enabled="true" android:name="com.distriqt.extension.beacon.services.BeaconIntentProcessor">
                <meta-data android:name="background" android:value="true" />
                <intent-filter android:priority="1" >
                    <action android:name="air.com.distriqt.test.DID_RANGING" />
                    <action android:name="air.com.distriqt.test.DID_MONITORING" />
                </intent-filter>
            </service>
        </application>
    
    </manifest>
]]></manifestAdditions>
     
Tools and resources

The following list are some useful tools for use during development

References:

View the examples



Public Properties
 PropertyDefined By
  implementation : String
[read-only] The implementation currently in use.
Beacon
  isSupported : Boolean
[static] [read-only] Whether the current device supports the extensions functionality
Beacon
  nativeVersion : String
[read-only] The native version string of the native extension.
Beacon
  service : Beacon
[static] [read-only] The singleton instance of the Beacon class.
Beacon
  version : String
[read-only] The version of this extension.
Beacon
Public Methods
 MethodDefined By
  
Constructor You should not call this directly, but instead use the singleton access
Beacon
  
Retrieves the current authorisation status.
Beacon
  
dispose():void
Disposes the extension and releases any allocated resources.
Beacon
  
Returns true if the current authorisation status is either IN_USE or ALWAYS.
Beacon
  
init(key:String):void
Deprecated: You no longer need to use an application key
[static] Initialises the extension class for use with the provided key.
Beacon
  
Checks the current state of the bluetooth adapter.
Beacon
  
Whether the device can act as a beacon and broadcast appropriately.
Beacon
  
isBroadcasting():Boolean
Beacon
  
isMonitoringRegionWithUUID(uuid:String):Boolean
Checks if there is already a monitor in process for the specified region
Beacon
  
requestAuthorisation(status:String):Boolean
Performs a simple operation to force the display of the permission dialog.
Beacon
  
Starts the current device broadcasting as a beacon.
Beacon
  
startMonitoringRegionWithUUID(uuid:String, identifier:String):Boolean
This function starts monitoring a a region with the specified uuid.
Beacon
  
stopBroadcast():Boolean
Stops the device broadcasting as a beacon.
Beacon
  
stopMonitoringRegionWithUUID(uuid:String, identifier:String):Boolean
Stops monitoring a region for updates.
Beacon
Events
 Event Summary Defined By
   Dispatched when the user changes the authorisation status.Beacon
   Dispatched when an update occurs about the available beacons.Beacon
   Dispatched when the bluetooth adapter changes state to disabled.Beacon
   Dispatched when the bluetooth adapter changes state to enabled.Beacon
   Dispatched if there was an error when starting broadcasting Beacon
   Dispatched when the broadcasting starts successfully.Beacon
   Dispatched when broadcasting stops.Beacon
   Dispatched when the device enters a monitored region.Beacon
   Dispatched when it appears the device has exitted the region.Beacon
   Dispatched when a region is started monitoring for entry and exit.Beacon
Public Constants
 ConstantDefined By
  EXT_CONTEXT_ID : String = com.distriqt.Beacon
[static]
Beacon
  VERSION : String = 5.1.0
[static]
Beacon
Property Detail
implementationproperty
implementation:String  [read-only]

The implementation currently in use. This should be one of the following depending on the platform in use and the functionality supported by this extension:


Implementation
    public function get implementation():String
isSupportedproperty 
isSupported:Boolean  [read-only]

Whether the current device supports the extensions functionality


Implementation
    public static function get isSupported():Boolean
nativeVersionproperty 
nativeVersion:String  [read-only]

The native version string of the native extension.


Implementation
    public function get nativeVersion():String
serviceproperty 
service:Beacon  [read-only]

The singleton instance of the Beacon class.


Implementation
    public static function get service():Beacon

Throws
Error — If there was a problem creating or accessing the extension, or if the developer key is invalid
versionproperty 
version:String  [read-only]

The version of this extension.

This should be of the format, MAJOR.MINOR.BUILD


Implementation
    public function get version():String
Constructor Detail
Beacon()Constructor
public function Beacon()

Constructor You should not call this directly, but instead use the singleton access

Method Detail
authorisationStatus()method
public function authorisationStatus():String

Retrieves the current authorisation status.

Returns
String
dispose()method 
public function dispose():void

Disposes the extension and releases any allocated resources. Once this function has been called, a call to init is neccesary again before any of the extensions functionality will work.

hasAuthorisation()method 
public function hasAuthorisation():Boolean

Returns true if the current authorisation status is either IN_USE or ALWAYS.

Retrieves the current authorisation status of this application.

There are several possible values as defined in the AuthorisationStatus class.

Returns
Boolean — The AuthorisationStatus string

See also

init()method 
public static function init(key:String):void
Deprecated: You no longer need to use an application key

Initialises the extension class for use with the provided key.

Parameters

key:String

isBluetoothEnabled()method 
public function isBluetoothEnabled():Boolean

Checks the current state of the bluetooth adapter. This can be used to check if the user has disabled bluetooth on the device.

Note: On application startup this may return false even though the adapter is turned on. You should delay calls to this function until the application is up and running.

Returns
Boolean — true if the device bluetooth is enabled powered on and false otherwise
isBroadcastAvailable()method 
public function isBroadcastAvailable():Boolean

Whether the device can act as a beacon and broadcast appropriately.

This will return false, if the device doesn't have the required OS version or it doesn't have the appropriate Bluetooth LE hardware.

Additionally it will return false if bluetooth is not enabled on the device, you should check the bluetooth enabled flag before attempting to access this flag.

Returns
Boolean
isBroadcasting()method 
public function isBroadcasting():Boolean

Returns
Boolean — Whether the device is currently broadcasting as a beacon
isMonitoringRegionWithUUID()method 
public function isMonitoringRegionWithUUID(uuid:String):Boolean

Checks if there is already a monitor in process for the specified region

Parameters

uuid:String — The UUID of the region to check

Returns
Boolean — true if the region is being monitored and false otherwise.
requestAuthorisation()method 
public function requestAuthorisation(status:String):Boolean

Performs a simple operation to force the display of the permission dialog.

Note on iOS this will only be shown if the authorisation status is currently NOT_DETERMINED

This allows you to control when the permission dialog is presented.

Parameters

status:String (default = NaN)

Returns
Boolean
startBroadcast()method 
public function startBroadcast(config:BroadcastConfig):Boolean

Starts the current device broadcasting as a beacon.

The application must be running in the foreground for broadcasting to work.

Parameters

config:BroadcastConfig — The configuration to use to start broadcasting as a beacon

Returns
Boolean — true if the broadcast was initiated successfully, and false if it failed.
startMonitoringRegionWithUUID()method 
public function startMonitoringRegionWithUUID(uuid:String, identifier:String):Boolean

This function starts monitoring a a region with the specified uuid. Calling this function the application will start monitoring for entry and exit notifications from the specified region, including the notify entry state on display flag.

Once a region is entered the application will automatically start ranging for beacons. As ranging only works in the foreground your application will receive events only when running in the foreground.

" In general ranging only works in the foreground, but monitoring updates can happen in the background. There are two ways to configure background monitoring, and even when done properly updates can take a long time to come. Because these delays, some people mistakenly believe that monitoring in the background doesn't work. "

When the application is in the foreground, a region change detection can take ~ 1 second.

Multiple regions

If you wish to monitor multiple regions simulataneously you must make sure that the identifier parameter is unique for each region as well as the uuid! If you do not you will only receive updates for the last region you started monitoring. The iOS SDK uses the identifier to determine if the region is unique, and replaces any existing region monitoring if it is the same.

Parameters

uuid:String — The UUID string for the region to start monitoring
 
identifier:String — An identifier string to associate with this region

Returns
Boolean — true if monitoring was successfully started
stopBroadcast()method 
public function stopBroadcast():Boolean

Stops the device broadcasting as a beacon.

Returns
Boolean — true if successful and false if the device wasn't broadcasting
stopMonitoringRegionWithUUID()method 
public function stopMonitoringRegionWithUUID(uuid:String, identifier:String):Boolean

Stops monitoring a region for updates.

Parameters

uuid:String — The UUID string for the region to stop monitoring
 
identifier:String — An identifier string to associate with this region

Returns
Boolean — true if the region monitoring was stopped successfully
Event Detail
authorisation:changed Event
Event Object Type: com.distriqt.extension.beacon.events.AuthorisationEvent
AuthorisationEvent.type property = com.distriqt.extension.beacon.events.AuthorisationEvent.CHANGED

Dispatched when the user changes the authorisation status. The status will indicate the new authorisation status.

beacon:update Event  
Event Object Type: com.distriqt.extension.beacon.events.BeaconEvent
BeaconEvent.type property = com.distriqt.extension.beacon.events.BeaconEvent.BEACON_UPDATE

Dispatched when an update occurs about the available beacons. The beacons will contain the list of available beacons.

bluetooth:state:disabled Event  
Event Object Type: com.distriqt.extension.beacon.events.BeaconEvent
BeaconEvent.type property = com.distriqt.extension.beacon.events.BeaconEvent.BLUETOOTH_STATE_DISABLED

Dispatched when the bluetooth adapter changes state to disabled.

bluetooth:state:enabled Event  
Event Object Type: com.distriqt.extension.beacon.events.BeaconEvent
BeaconEvent.type property = com.distriqt.extension.beacon.events.BeaconEvent.BLUETOOTH_STATE_ENABLED

Dispatched when the bluetooth adapter changes state to enabled.

broadcast:error Event  
Event Object Type: com.distriqt.extension.beacon.events.BroadcastEvent
BroadcastEvent.type property = com.distriqt.extension.beacon.events.BroadcastEvent.BROADCAST_ERROR

Dispatched if there was an error when starting broadcasting

broadcast:start Event  
Event Object Type: com.distriqt.extension.beacon.events.BroadcastEvent
BroadcastEvent.type property = com.distriqt.extension.beacon.events.BroadcastEvent.BROADCAST_START

Dispatched when the broadcasting starts successfully.

broadcast:stop Event  
Event Object Type: com.distriqt.extension.beacon.events.BroadcastEvent
BroadcastEvent.type property = com.distriqt.extension.beacon.events.BroadcastEvent.BROADCAST_STOP

Dispatched when broadcasting stops.

region:enter Event  
Event Object Type: com.distriqt.extension.beacon.events.BeaconEvent
BeaconEvent.type property = com.distriqt.extension.beacon.events.BeaconEvent.REGION_ENTER

Dispatched when the device enters a monitored region. The event will contain a valid region describing the region that has been entered/discovered.

Once this event is dispatched you can expect to get updates about the beacons in the region in periodic intervals

region:exit Event  
Event Object Type: com.distriqt.extension.beacon.events.BeaconEvent
BeaconEvent.type property = com.distriqt.extension.beacon.events.BeaconEvent.REGION_EXIT

Dispatched when it appears the device has exitted the region. You should stop receiving beacon update events.

region:monitoring:start Event  
Event Object Type: com.distriqt.extension.beacon.events.BeaconEvent
BeaconEvent.type property = com.distriqt.extension.beacon.events.BeaconEvent.REGION_MONITORING_START

Dispatched when a region is started monitoring for entry and exit. This usually occurs almost immediately after a call startMonitoringRegionWithUUID

Constant Detail
EXT_CONTEXT_IDConstant
public static const EXT_CONTEXT_ID:String = com.distriqt.Beacon

VERSIONConstant 
public static const VERSION:String = 5.1.0

Examples

The following example demonstrates the base usage of the beacon monitoring, showing the event listeners and monitoring requirements.

     try
     {
      if (Beacon.isSupported)
         {
             Beacon.init( "YOUR_DEVELOPER_KEY" );
             
             Beacon.service.addEventListener( BeaconEvent.REGION_MONITORING_START, beacon_monitoringStartHandler, false, 0, true );
             Beacon.service.addEventListener( BeaconEvent.REGION_ENTER, beacon_regionEnterHandler, false, 0, true );
             Beacon.service.addEventListener( BeaconEvent.REGION_EXIT, beacon_regionExitHandler, false, 0, true );
             Beacon.service.addEventListener( BeaconEvent.BEACON_UPDATE, beacon_beaconUpdateHandler, false, 0, true );
     
             Beacon.service.startMonitoringRegionWithUUID( UUID, "testing" );
         }
     }
     catch (e:Error)
     {
     }
     
     
     ...
     
     
     //
     //    EXTENSION HANDLERS
     //
     
     private function beacon_monitoringStartHandler( event:BeaconEvent ):void
     {
         trace( "monitoring started" );
     }
     
     
     private function beacon_regionEnterHandler( event:BeaconEvent ):void
     {
         trace( "Entered region: " + event.region.uuid );
     }
     
     
     private function beacon_regionExitHandler( event:BeaconEvent ):void
     {
         trace( "Exit region: " + event.region.uuid );
     }
     
     
     private function beacon_beaconUpdateHandler( event:BeaconEvent ):void
     {
         trace( "Beacon update for region: "+event.region.uuid );
         for each (var beacon:BeaconObject in event.beacons)
         {
             trace( "Beacon: "+beacon.major+"."+beacon.minor +"::"+beacon.proximity +"["+beacon.accuracy +"]");
         }
     }