如何使用角/离子服务定制科尔多瓦插件的方法 [英] How to use custom Cordova plugin methods in Angular/Ionic service

查看:177
本文介绍了如何使用角/离子服务定制科尔多瓦插件的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个自定义插件科尔多瓦整合到一个应用程序离子。我们有一个第三方创建插件科尔多瓦与蓝牙设备连接。运行科尔多瓦平台LS 显示了插件已正确安装:

I'm working on integrating a custom Cordova plugin into an Ionic app. We had a 3rd party create a Cordova plugin to interface with a bluetooth device. Running cordova platform ls shows the plugin has been correctly installed:

$ cordova plugin ls
> com.sitename.product 0.0.0 "DeviceProbe"

该插件包含包含连接,阅读,投票等动作方法 probe.js 文件。

/plugins/com.sitename.product/www/probe.js

var callNative = function(service, action, success, error, args) {
    if(args === undefined) args = [];
    cordova.exec(success, error, service, action, args);
};

var thermProbe = {

    // Methods here

};

module.exports = thermProbe;

为了使用这个插件在我们的控制器我需要创建一个角服务的包装,如<一个href=\"http://trinhtrunganh.com/correct-way-to-integrate-cordova-plugin-in-angularjs-ionic-framework/\"相对=nofollow>这里描述。

In order to use the plugin in our controllers I need to create an Angular service wrapper, as described here.

我创建了一个工厂来处理这个问题。

I've created a factory to handle this.

/lib/probe/probe.js

(function() {

    'use strict';

    var serviceId = 'Probe';
    angular.module('thermProbe').factory(serviceId, ['$q', Probe]);

    function Probe($q) {

        var service = {
            'connect': connect,
            'disconnect': disconnect,
            'getReading': getReading,
            'getName': getName, 
            'getHigh': getHigh,
            'getLow': getLow,
            'pollReading': pollReading,
            'stopPolling': stopPolling,
            'isPolling': isPolling
        };

        return service;

        // Method wrappers

        function connect() {

            var q = $q.defer(); 

            if($window.cordova){
              cordova.plugins.thermProbe.connect(function (result) {
                q.resolve(result);
              }, function (err) {
                q.reject(err);
              });
            }
            return q.promise;

        }

    }

})();

注射的探头服务为一体的控制器正常工作。当我在我的设备上运行这个(使用离子运行的Andr​​oid )我收到以下错误:

Injecting the Probe service into a controller works fine. When I run this on my device (using ionic run android) I get the following error:

TypeError: Cannot read property 'connect' of undefined

追溯包含行 cordova.plugins.thermProbe.connect()

我还使用 cordova.plugins.probe.connect(),但收到同样的错误尝试。

I've also tried using cordova.plugins.probe.connect() but receive the same error.

我怎样才能从 /plugins/com.sitename.product/www/probe.js 工作在 / lib中/探针方法/probe.js

推荐答案

多的试验和错误(和的console.log ING)我能解决这个问题之后。

After much trial and error (and console.loging) I was able to solve this.

$窗口对象有一个探头对象包含所有需要的方法。我猜探头名字来自科尔多瓦插件配置。

The $window object has a probe object containing all the needed methods. I'm guessing the probe name comes from the Cordova plugin config.

的plugin.xml

<js-module src="www/probe.js" name="probe">
  <clobbers target="probe" />
</js-module>
<platform name="ios">
  <config-file target="config.xml" parent="/*">
    <feature name="probe">
      <param name="ios-package" value="productName"/>
    </feature>
  </config-file>
  ...
  ...
</platform>
<platform name="android">
  <config-file target="res/xml/config.xml" parent="/*">
    <feature name="probe">
      <param name="android-package" value="com.sitename.productName"/>
      <param name="onload" value="true" />
    </feature>
  </config-file>
  ...
  ...
</platform>

要使用的方法:

function connect() {

  var q = $q.defer(); 

  if($window.probe){
    $window.probe.connect(function (result) {
      q.resolve(result);
    }, function (err) {
      q.reject(err);
    });
  }

  return q.promise;

}

这篇关于如何使用角/离子服务定制科尔多瓦插件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆