Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom Cordova plugin methods in Angular/Ionic service

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"

The plugin contains a probe.js file containing methods for connecting, reading, polling and other actions.

/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;

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;

        }

    }

})();

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

tracing back to the line containing cordova.plugins.thermProbe.connect().

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

How can I get the methods from /plugins/com.sitename.product/www/probe.js to work in /lib/probe/probe.js?

like image 700
Brett DeWoody Avatar asked Nov 10 '22 09:11

Brett DeWoody


1 Answers

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>

To use the methods:

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;

}
like image 145
Brett DeWoody Avatar answered Nov 15 '22 05:11

Brett DeWoody