Ionic2 在Ionic 2中,iBeacon集成抛出;没有IBeacon的提供程序”;错误

Ionic2 在Ionic 2中,iBeacon集成抛出;没有IBeacon的提供程序”;错误,ionic2,ibeacon,Ionic2,Ibeacon,我正在尝试将ibeacon功能集成到Ionic 2应用程序中 我正在使用插件 遵循文档中提到的步骤 创建了一个提供程序类 添加了插件集成 在主页中调用提供程序类 但在android设备上运行应用程序时,出现错误 “导航失败:IBeacon没有提供程序!” 请提出任何解决方案 谢谢 信标提供程序类: import { Injectable } from '@angular/core'; import { Platform, Events } from 'ionic-angular'; import

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

我正在使用插件

遵循文档中提到的步骤

  • 创建了一个提供程序类
  • 添加了插件集成
  • 在主页中调用提供程序类
  • 但在android设备上运行应用程序时,出现错误

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

    请提出任何解决方案

    谢谢

    信标提供程序类:

    import { Injectable } from '@angular/core';
    import { Platform, Events } from 'ionic-angular';
    import { IBeacon } from '@ionic-native/ibeacon';
    
    
    
    /*
      Generated class for the BeaconProvider provider.
    // 
      See https://angular.io/docs/ts/latest/guide/dependency-injection.html
      for more info on providers and Angular 2 DI.
    */
    @Injectable()
    export class BeaconProvider {
    
      delegate: any;
      region: any;
    
      constructor(public platform: Platform, public events: Events, private ibeacon : IBeacon) {
      }
    
      initialise(): any {
        let promise = new Promise((resolve, reject) => {
          // we need to be running on a device
          if (this.platform.is('cordova')) {
    
            // Request permission to use location on iOS
            this.ibeacon.requestAlwaysAuthorization();
    
            // create a new delegate and register it with the native layer
            this.delegate = this.ibeacon.Delegate();
    
            // Subscribe to some of the delegate’s event handlers
            this.delegate.didRangeBeaconsInRegion()
              .subscribe(
              data => {
                this.events.publish('didRangeBeaconsInRegion', data);
              },
              error => console.error()
              );
    
            // setup a beacon region – CHANGE THIS TO YOUR OWN UUID
            this.region = this.ibeacon.BeaconRegion('deskBeacon', 'E2C56DB5-DFFB-48D2-B060-D0F5A71096E0');
    
            // start ranging
            this.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;
      }
    
    
    }
    
    在主页上

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

    您是否尝试过在home.ts的构造函数中添加服务

    constructor(private myService: IBeacon ){
    
    }
    

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

    并将IBeacon添加到app.module.ts中的提供商

    这为我解决了这个问题

    (这适用于离子3,但过程与离子2相似)

    我建议您将IBeacon定义放在
    app.module.ts
    provider
    列表下,如下所示

    @NgModule({
      declarations: [
        MyApp,
      ],
      imports: [
        BrowserModule,
        HttpModule,
        HttpClientModule,
         AngularFireDatabaseModule,
        AngularFireModule.initializeApp(config),
        AngularFireAuthModule,
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: (createTranslateLoader),
            deps: [HttpClient]
          }
        }),
        IonicModule.forRoot(MyApp),
        IonicStorageModule.forRoot()
      ],
      bootstrap: [IonicApp],
      entryComponents: [
        MyApp,
      ],
      providers: [
        Api,
        Items,
        User,
        Camera,
        CardIO,
        NFC,
        Ndef,
        IBeacon,
        Stripe,
        SplashScreen,
        StatusBar,
        { provide: Settings, useFactory: provideSettings, deps: [Storage] },
        // Keep this to enable Ionic's runtime error handling during development
        { provide: ErrorHandler, useClass: IonicErrorHandler },
        FirebaseProvider,
        BarcodeScanner,
        AuthProvider,
        BeaconProvider,
      ]
    })
    export class AppModule { }
    

    您是否在主页的
    提供程序:[InvokedProvider]
    中声明了调用的提供程序?发布相关代码。。没有它,谁也说不出来……)@SagarKulkarni是已添加提供程序。@suraj抱歉。。现在添加了代码。将此行“import{IBeacon}from'@ionic native/IBeacon';”替换为“import{IBeacon}from'ionic native';”。并使用“ibeacon”而不是从ibeacon实例调用ibeacon方法yes。我试了两种方法。1.在构造函数中添加了服务,并使用实例变量来校准beacon方法。2.没有在构造函数中添加服务,我尝试使用IBeacon作为静态变量并调用方法。问题在于将IBeacon标识为provider。好的,尝试将IBeacon添加到app.module.ts文件中,然后在构造函数中添加IBeacon后调用它,看看它是否有效。