如何从Estimote iBeacons获得一致的范围?他们沉默了20秒

时间:2017-07-30 18:03:14

标签: ionic-framework ibeacon ibeacon-android estimote android-ibeacon

我正在使用Ionic 3和Ionic Native IBeacon插件来互动Estimote信标。

我正在尝试测量4个信标。

我的问题是,测距不一致。

信标似乎被检测到5-6次,然后随后的18-19 didRangeBeaconsInRegion事件没有发现任何信标。

如何从信标中获得一致的范围?即使它是一致的,每3或5秒对我的用例来说会好得多。

这是我的设置:

import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import {
  IBeacon,
  IBeaconPluginResult,
  IBeaconDelegate,
  BeaconRegion
} from '@ionic-native/ibeacon';

import { DetectedBeaconModel } from '../models';

@Injectable()
export class DetectBeaconService {

  constructor(
    private ibeacon: IBeacon
  ) { }

  private beaconSubject: BehaviorSubject<DetectedBeaconModel[]> = new BehaviorSubject<DetectedBeaconModel[]>(new Array<DetectedBeaconModel>());

  public detectBeacons(): Observable<DetectedBeaconModel[]> {

    this.setupBeaconDetection();
    return this.beaconSubject.asObservable();

  }

  private setupBeaconDetection(): void {

    const delegate = this.buildDelegate();
    const beaconRegion = this.buildBeaconRegion();
    this.startRanging(beaconRegion);

  }

  private startRanging(beaconRegion: BeaconRegion): Promise<any> {
    return this.ibeacon.startRangingBeaconsInRegion(beaconRegion)
      .catch(error => console.error(error));
  }

  private buildDelegate(): Object {

    let delegate: IBeaconDelegate = this.ibeacon.Delegate();

    // Callback function fired when beacons are detected.
    delegate.didRangeBeaconsInRegion()
      .subscribe(
      result => this.didRangeBeaconsInRegion(result),
      error => console.error(error)
      );

    return delegate;
  }

  private buildBeaconRegion(): BeaconRegion {
    const identifier: string = 'ThisCannotBeNullButIDontKnowWhatItsUsedFor';
    const uuid: string = 'XXXX-XXXX-XXXX-XXXX-XXXX';
    // Setting the major here for testing only,
    // set it back to undefined after testing.
    const major: number = undefined;
    // const major: number = 12345;
    return this.ibeacon.BeaconRegion(identifier, uuid, major, undefined, true);
  }

  private didRangeBeaconsInRegion(pluginResult: IBeaconPluginResult): void {

    console.log('did range');

    // Beacons are not always detected when
    // this event fires, so lets double check
    // to make sure we have some before adding
    // them to the BehaviorSubject.
    if (!pluginResult.beacons) return;

    console.log(`has beacons, count: ${pluginResult.beacons.length}`);
    console.error(pluginResult)
    const beacons: Array<DetectedBeaconModel> = new Array<DetectedBeaconModel>();

    pluginResult.beacons.forEach(beacon => {
      const beaconModel: DetectedBeaconModel = this.mapRangeToDetectedBeaconModel(beacon);
      console.error(beaconModel);

      beacons.push(beaconModel);
    });

    // Publish to the rest of the
    // application the newly ranged 
    // beacons.
    this.beaconSubject.next(beacons);

  }

  private mapRangeToDetectedBeaconModel(beacon: any): DetectedBeaconModel {
    return new DetectedBeaconModel(
      beacon.proximity,
      beacon.major,
      beacon.accuracy
    );
  }
}

0 个答案:

没有答案