我使用cordova-plugin-ibeacon但没有工作(没有在android中找到信标)

时间:2017-04-11 11:08:23

标签: android cordova plugins ionic2 ibeacon

这是代码

beacon-provider.ts>>

 initialise(): any {
    let promise = new Promise((resolve, reject) => {

      if (this.platform.is('cordova')) {


        IBeacon.enableBluetooth();

        this.delegate = IBeacon.Delegate();

        this.delegate.didRangeBeaconsInRegion()
          .subscribe(
          data => {
            this.events.publish('didRangeBeaconsInRegion', data);
          },
          error => console.error()
          );

        this.region = IBeacon.BeaconRegion('deskBeacon', '24DDF411-8CF1-440C-87CD-E368DAF9C93E');

        IBeacon.startRangingBeaconsInRegion(this.region)
          .then(
          () => {
            resolve(true);
          },
          error => {
            console.error('Failed to begin monitoring: ', error);
            resolve(false);
          }
          );


      } else {
        console.error("This application needs to be running on a device");
        resolve(false);
      }
    });

    return promise;
  }


}

home.ts>>

export class HomePage {

  beacons: BeaconModel[] = [];
  zone: any;

  constructor(public navCtrl: NavController, public platform: Platform, public beaconProvider: BeaconProvider, public events: Events) {

    this.zone = new NgZone({ enableLongStackTrace: false });

  }

  ionViewDidLoad() {
    this.platform.ready().then(() => {
      this.beaconProvider.initialise().then((isInitialised) => {
        if (isInitialised) {
          this.listenToBeaconEvents();
        }
      });
    });
  }

  listenToBeaconEvents() {
    this.events.subscribe('didRangeBeaconsInRegion', (data) => {

      this.zone.run(() => {

        this.beacons = [];

        let beaconList = data.beacons;
        beaconList.forEach((beacon) => {
          let beaconObject = new BeaconModel(beacon);
          this.beacons.push(beaconObject);
        });

      });

    });
  }

}

在此代码中,alert(JSON.stringify(data))的结果为:

{"eventType":"didRangeBeaconslnRegion","region":{"identifier":"desk beacon","uuid":"24DDF411-8CF1-440C-87CD-E368DAF9C93E","typeName":"BeaconRegion"}, "beacons":[]}

字段data.beacons为空。

有什么问题?

我首先尝试使用BLE-central插件,但是, 当我使用BLE-central插件时,我得到了信号,但是如果我从广告中得到这个值,那么它不会给我主要的,次要的价值?

1 个答案:

答案 0 :(得分:2)

有很多事情可能导致这种行为:

  • 确认蓝牙已开启
  • 确认您的应用已获得检测蓝牙设备所需的运行时位置权限。转到设置 - >应用 - > [您的应用名称] - >权限,并确保您看到打开开关的位置条目。
  • 使用现成的探测器应用验证您的信标实际上正在传输您期望的标识符。在此处尝试我的定位应用:https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=en