ohos_beacon_library

Introduction

This project provides a utility repository for beacon range monitoring and distance measurement.

Effect

img

How to Install

ohpm install @ohos/beacon-library 

OpenHarmony

For details about the OpenHarmony ohpm environment configuration, see OpenHarmony HAR.

How to Use

Beacon Component Usage Example

  1. Import the files of the beacon library to the page.

    import { Identifier, Region, BeaconParser, BeaconManager, RangeNotifier,Beacon } from '@ohos/beacon-library'
    import bluetooth from '@ohos.bluetooth.ble';
    
  2. Use the project.

    - Start beacon distance measurement.
    _this.beaconManager.addRangeNotifier(new class implements RangeNotifier {
        didRangeBeaconsInRegion(beacons: Array<Beacon>, region: Region): void {
            _this.beaconList = beacons
        }
    });
    _this.beaconManager.startRangingBeacons(region);
    
    - Stop beacon distance measurement.
    this.beaconManager.stopRangingBeacons(new Region("myRangingUniqueId", null, null, null));
    
    - Start monitoring.
    _this.beaconManager.addMonitorNotifier(new class implements MonitorNotifier {
        INSIDE:number = 1
        OUTSIDE:number = 0
    
        didEnterRegion(region:Region):void {
            _this.enterRegionText = "I just saw a region: " + region.getUniqueId()
        }
        
        didExitRegion(region:Region):void {
            _this.enterRegionText = "I no longer see an beacon in Region:" + region.getUniqueId()
        }
        
        didDetermineStateForRegion(state:number, region:Region):void {
            _this.determineStateText = "I have just switched from seeing/not seeing beacons: " + state
        }
    });
    _this.beaconManager.startMonitoring(region);
    
    - Stop monitoring.
    this.beaconManager.stopMonitoring(new Region("myMonitorUniqueId", null, null, null));
    

Available APIs

@State private beaconManager: BeaconManager = BeaconManager.getInstanceForApplication()

  1. Adds a monitor notifier when a beacon is scanned.

    .beaconManager.addMonitorNotifier(notifier:MonitorNotifier): void

  2. Starts monitoring to search for beacons that match a region.

    .beaconManager.startMonitoring(region:Region ):void

  3. Adds a range notifier when a beacon is scanned.

    .beaconManager.addRangeNotifier(notifier:RangeNotifier):void

  4. Enables beacon distance measurement.

    .beaconManager.startRangingBeacons(region:Region):void

  5. Sets encoding rules for a beacon interpreter.

    new BeaconParser().setBeaconLayout (beaconLayout:String): void

  6. Encapsulates common data types into an identifier type.

    Identifier.parse (stringValue:String, desiredByteLength?:number): Identifier

  7. Constructs a region as a beacon matching condition based on the combination of uniqueId and other parameters.

    Region(uniqueId: string, id1?: Identifier, id2?: Identifier, id3?: Identifier, identifiers?: Array<Identifier>, bluetoothAddress?: string)

Constraints

This project has been verified in the following version:

  • DevEco Studio: 4.1 Canary (4.1.3.317)

  • OpenHarmony SDK: API 11 (4.1.0.36)

Directory Structure

|---- ohos_beacon_library  
|     |---- entry  # Sample code
|     |---- library # Beacon library
|	      |----src
|             |----main
|                 |----ets
|                     |----components
|                         |---- beacon # Beacon library folder
|                             |---- client # Data processing of the client in three scenarios
|                             |---- distance # Device-related settings and distance configuration logic
|                             |---- logging # Log logic
|                             |---- service #Server processing logic
|                             |---- simulator # Beacon calculation
|                             |---- startup # Used to send and receive data
|                             |---- utils # Utility class
|                             |---- AltBeacon.ets # Used to establish beacon communication
|                             |---- BeaconParser.ets # Used to parse Beacon data packages
|                         |---- bluetooth      # Bluetooth library
|                             |---- BleAdvertisement.ets # Beacon broadcast logic
|                             |---- Pdu.ets # Bluetooth data transfer unit
|     |---- README.md  # Readme                   

About obfuscation

  • Code obfuscation, please seeCode Obfuscation
  • If you want the beacon-library library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/beacon-library

How to Contribute

If you find any problem when using the project, submit an issue or a PR.

License

This project is licensed under Apache License 2.0.