首页 > 解决方案 > 在 ionic 3 应用程序 (cordova-androidwear) 上使用 cordova 插件

问题描述

我有一个在地图上显示标记的 ionic 3 应用程序。但现在我有一个新请求,要求该应用程序将其移植到可穿戴设备(Android Wear 和 Apple Watch)。在手表上的地图中显示标记和一些信息...

经过大量搜索和论坛上的任何问题,我得到了一个 android wear 插件https://github.com/tgardner/cordova-androidwear但它是一个cordova插件。科尔多瓦的代码是:

            function watch(handle) {
                var self = this;
                AndroidWear.onDataReceived(handle, function(e) {
                    self.dataReceived(e.data);
                });

                self.handle = handle;
            }

            watch.prototype = {
                dataReceived: function(data) {
                    app.logEvent("AndroidWear message received: " + data);
                },

                sendMessage: function(message) {
                    AndroidWear.sendData(this.handle, message);
                    app.logEvent("AndroidWear message sent!");
                }
            };

            var app = {
                watch: null,

                initialize: function() {
                    this.bindEvents();
                },

                bindEvents: function() {
                    var self = this;
                    document.addEventListener('deviceready', function() {
                        self.onDeviceReady();
                    }, false);
                },

                onDeviceReady: function() {
                    var self = this;
                    self.receivedEvent('deviceready');

                    if(AndroidWear) {
                        AndroidWear.onConnect(function(e) {
                            self.logEvent("AndroidWear connection established");
                            self.watch = new watch(e.handle);
                        });
                    }

                    var sendButton = document.getElementById("sendMessage");
                    sendButton.addEventListener("click", function() {
                        if(self.watch) {
                            self.watch.sendMessage("Mensaje");
                        }
                    });
                },
                receivedEvent: function(id) {
                    var parentElement = document.getElementById(id);
                    var listeningElement = parentElement.querySelector('.listening');
                    var receivedElement = parentElement.querySelector('.received');

                    listeningElement.setAttribute('style', 'display:none;');
                    receivedElement.setAttribute('style', 'display:block;');

                    this.logEvent('Received Event: ' + id);
                },

                logEvent: function(message) {
                    var events = document.getElementById("events");
                    var el = document.createElement("li");

                    el.innerHTML = message;
                    events.appendChild(el);
                }
            };

但我需要将科尔多瓦代码翻译成离子代码。我尝试这样做:

    (<any>window).AndroidWear.onConnect(() =>{
      console.log("Androidwear : CONECTADO " );
    })

    (<any>window).AndroidWear.onDataReceived((data) =>{
      console.log("Plugin: " + data);
    })

但我收到这样的执行错误 [INFO:CONSOLE(13355)] “没有设置导航根:TypeError: window.AndroidWear.onConnect(...) is not a function”,来源:file:///android_asset /www/build/main.js (13355)

我从来没有将 Cordova 代码翻译成 ionic 3,所以也许我犯了愚蠢的错误。

如何翻译该代码以在 ionic 3 应用程序上使用 android-wear 插件?

谢谢。

标签: ionic-frameworkionic3cordova-pluginswear-oswearables

解决方案


推荐阅读