我知道通常的 way访问cordova插件:
(<any>window).plugins.myPlugin or declare var yourGloballyAccessablePlugin: any;
但它与插件bluetoothle不同(离子3支持的原生蓝牙插件不够好,因为它们不提供蓝牙外围功能,例如广告)
解决方案:
I found a related question on the ionic forums and asked how they achieved this,到目前为止,我很难复制这个过程,到目前为止没有人回答我的问题,这就是为什么这个问题被打开了.
显然,bluetoothle暴露了一个全局可访问的变量.
如上所述,我在我的src文件夹中添加了一个declaration.d.ts文件
具有以下内容:
declare module 'cordova-plugin-bluetoothle'; import 'cordova-plugin-bluetoothle'; declare var cordova: any;
然后我尝试访问插件(在我的手机上测试),如下所示:
import { bluetoothle } from 'cordova-plugin-bluetoothle'; ... (<any>window).bluetoothle
问题:
但bluetoothle对我来说总是不确定的.由于我是cordova,ionic和TypeScript的新手我觉得我使用declarations.d.ts的方式有问题.
那么有人知道我做错了什么,我怎么能在离子3中使用cordova原生插件bluetoothle?
更新,解决方案尝试2:
所以我尝试在@Webruster推荐的初始项目结构app.component.ts中使用bluetoothle documentation的init参数运行此代码:
(这里唯一的目标是查看插件是否有效)
进口…
declare var cordova: any;
@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(); }
但这样应用程序甚至没有加载,它只是超时并输出到服务器的连接是不成功的,当我运行应用程序没有它的工作的插件代码.
更新2:
我用chrome调试了应用程序(之前的错误是由–livereload选项导致的,因为未知原因)
这是我得到的错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'initialize' of undefined TypeError: Cannot read property 'initialize' of undefined
检查cordova,cordova.plugins和cordova.plugins.bluetoothle的类型
有:
console.log(typeof cordova); console.log(typeof cordova.plugins); console.log(typeof cordova.plugins.bluetoothle);
我得到以下结果:
object object undefined就像通常的方式一样,你可以安装它:
ionic plugin add cordova-plugin-bluetoothle
在此之后包括import语句后面的下面一行,如下所示:
declare var cordova:any;
并在平台准备就绪时使用它:
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); } );