如何在Ionic 3中使用Cordova插件蓝牙?

MAD*_*ppy 3 cordova typescript ionic-framework ionic-native

附加信息:

我知道访问cordova插件的常用方法

(<any>window).plugins.myPlugin 

or

declare var yourGloballyAccessablePlugin: any;
Run Code Online (Sandbox Code Playgroud)

但它与插件不同bluetoothle(通过离子3支持的本机蓝牙插件都不够好,因为它们不提供蓝牙功能peripherial如广告)

解决方案尝试:

我在离子论坛上找到了一个相关的问题,并询问他们是如何实现的,到目前为止,我未能重复该过程,到目前为止,没有人回答我的问题,这就是为什么要开放这个问题。

显然,蓝牙公开了一个全局可访问的变量。

如前所述,我在declaration.d.ts文件src夹中添加了以下内容:

declare module 'cordova-plugin-bluetoothle';
import 'cordova-plugin-bluetoothle';
declare var cordova: any;
Run Code Online (Sandbox Code Playgroud)

然后,我尝试像这样访问插件(在手机上对其进行了测试):

import { bluetoothle } from 'cordova-plugin-bluetoothle';

...

(<any>window).bluetoothle
Run Code Online (Sandbox Code Playgroud)

问题:

但是蓝牙对我来说始终是不确定的。由于我是Cordova,Ionic和TypeScript的新手,我想我使用clarifications.d.ts的方式有问题

那么,有人知道我在做什么错如何在ionic 3中使用cordova原生插件bluetoothle?

更新,解决方案尝试2

因此,我尝试按照app.component.ts@Webruster的建议在初始项目结构中运行此代码,并使用蓝牙文档中的init参数:

(这里的唯一目标是查看插件是否正常工作)

进口...

declare var cordova: any;
Run Code Online (Sandbox Code Playgroud)

@Component,类开始和属性...

  constructor(private translate: TranslateService, platform: Platform, settings: Settings, private config: Config, private statusBar: StatusBar, private splashScreen: SplashScreen) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      console.log("I'm ready");
      // your bluetothle methods can be accessed after
      //cordova.plugins.bluetoothle
      // for brevity i added a sample method from repo , it can be changed
      //based on your need
      let initializeResult: object;
      let params: object = {
        "request": true,
        "statusReceiver": false,
        "restoreKey": "bluetoothleplugin"
      };
      cordova.plugins.bluetoothle.initialize(initializeResult, params);

      console.log(JSON.stringify(initializeResult));

      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
    this.initTranslate();
  }
Run Code Online (Sandbox Code Playgroud)

但是这样一来,应用程序甚至无法加载,它只会超时,并且在我运行不带插件代码的应用程序时无法成功输出到服务器的连接。

更新2:

我调试铬应用(以前的错误:在对一个未知的原因,--livereload选项造成的),这是我得到的错误:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'initialize' of undefined
TypeError: Cannot read property 'initialize' of undefined
Run Code Online (Sandbox Code Playgroud)

检查的类型cordovacordova.pluginscordova.plugins.bluetoothle 使用:

  console.log(typeof cordova);
  console.log(typeof cordova.plugins);
  console.log(typeof cordova.plugins.bluetoothle);
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

object
object
undefined
Run Code Online (Sandbox Code Playgroud)

Web*_*ter 5

就像通常的方式一样,您可以使用以下命令进行安装:

ionic plugin add cordova-plugin-bluetoothle
Run Code Online (Sandbox Code Playgroud)

在此之后,在您的import语句之后包括以下行:

declare var cordova:any;
Run Code Online (Sandbox Code Playgroud)

并在平台就绪时使用它:

platform.ready().then(
    () => {
        console.log("I'm ready");
        // your bluetothle methods can be accessed after 
        //cordova.plugins.bluetoothle
        // for brevity i added a sample method from repo , it can be changed 
        //based on your need
        cordova.plugins.bluetoothle.initialize(initializeResult, params);
    }
);
Run Code Online (Sandbox Code Playgroud)