在 Ionic 2 中,iBeacon 集成抛出“No provider for IBeacon”错误

Bas*_*eer 4 ibeacon ionic2

我正在尝试将 ibeacon 功能集成到 Ionic 2 应用程序中。

\n\n

我正在使用https://ionicframework.com/docs/native/ibeacon/ 插件。

\n\n

按照文档中提到的步骤进行操作。

\n\n
    \n
  1. 创建了一个提供者类。
  2. \n
  3. 添加了插件集成。
  4. \n
  5. 调用主页中的提供程序类。
  6. \n
\n\n

但是在Android设备上运行该应用程序时,出现错误,

\n\n
\n

“导航失败:没有 IBeacon 提供商!”

\n
\n\n

请提出任何修复建议。

\n\n

谢谢。

\n\n

信标提供者类:

\n\n
import { Injectable } from \'@angular/core\';\nimport { Platform, Events } from \'ionic-angular\';\nimport { IBeacon } from \'@ionic-native/ibeacon\';\n\n\n\n/*\n  Generated class for the BeaconProvider provider.\n// \n  See https://angular.io/docs/ts/latest/guide/dependency-injection.html\n  for more info on providers and Angular 2 DI.\n*/\n@Injectable()\nexport class BeaconProvider {\n\n  delegate: any;\n  region: any;\n\n  constructor(public platform: Platform, public events: Events, private ibeacon : IBeacon) {\n  }\n\n  initialise(): any {\n    let promise = new Promise((resolve, reject) => {\n      // we need to be running on a device\n      if (this.platform.is(\'cordova\')) {\n\n        // Request permission to use location on iOS\n        this.ibeacon.requestAlwaysAuthorization();\n\n        // create a new delegate and register it with the native layer\n        this.delegate = this.ibeacon.Delegate();\n\n        // Subscribe to some of the delegate\xe2\x80\x99s event handlers\n        this.delegate.didRangeBeaconsInRegion()\n          .subscribe(\n          data => {\n            this.events.publish(\'didRangeBeaconsInRegion\', data);\n          },\n          error => console.error()\n          );\n\n        // setup a beacon region \xe2\x80\x93 CHANGE THIS TO YOUR OWN UUID\n        this.region = this.ibeacon.BeaconRegion(\'deskBeacon\', \'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0\');\n\n        // start ranging\n        this.ibeacon.startRangingBeaconsInRegion(this.region)\n          .then(\n          () => {\n            resolve(true);\n          },\n          error => {\n            console.error(\'Failed to begin monitoring: \', error);\n            resolve(false);\n          }\n          );\n      } else {\n        console.error(\'This application needs to be running on a device\');\n        resolve(false);\n      }\n    });\n\n    return promise;\n  }\n\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且在主页中,

\n\n
import { Component } from \'@angular/core\';\nimport { NavController } from \'ionic-angular\';\nimport { AuthService } from \'../../providers/auth-service\';\nimport { LoginPage } from \'../login/login\';\nimport { BeaconProvider } from \'../../providers/beacon-provider\';\nimport { BeaconModel } from \'../../models/beacon-module\';\nimport { Platform, Events } from \'ionic-angular\';\nimport { NgZone } from \'@angular/core\';\n\n@Component({\n  selector: \'page-home\',\n  templateUrl: \'home.html\',\n  providers : [BeaconProvider]\n})\n
Run Code Online (Sandbox Code Playgroud)\n

Iri*_*eek 5

添加

从'@ionic-native/ibeacon'导入{IBeacon};到你的app.module.ts

并将 IBeacon 添加到 app.module.ts 中的提供程序中。

这为我解决了这个问题。