title: How to use custom Cordova plugin methods in Angular/Ionic service
body: <p>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 <code>cordova platform ls</code> shows the plugin has been correctly installed:</p>
  
  <pre><code>$ cordova plugin ls
  &gt; com.sitename.product 0.0.0 "DeviceProbe"
  </code></pre>
  
  <p>The plugin contains a <code>probe.js</code> file containing methods for connecting, reading, polling and other actions.</p>
  
  <p><strong>/plugins/com.sitename.product/www/probe.js</strong></p>
  
  <pre><code>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;
  </code></pre>
  
  <p>In order to use the plugin in our controllers I need to create an Angular service wrapper, as <a href="http://trinhtrunganh.com/correct-way-to-integrate-cordova-plugin-in-angularjs-ionic-framework/" rel="nofollow">described here</a>.</p>
  
  <p>I've created a factory to handle this. </p>
  
  <p><strong>/lib/probe/probe.js</strong></p>
  
  <pre><code>(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;
  
          }
  
      }
  
  })();
  </code></pre>
  
  <p>Injecting the <code>Probe</code> service into a controller works fine. When I run this on my device (using <code>ionic run android</code>) I get the following error:</p>
  
  <pre><code>TypeError: Cannot read property 'connect' of undefined
  </code></pre>
  
  <p>tracing back to the line containing <code>cordova.plugins.thermProbe.connect()</code>.</p>
  
  <p>I've also tried using <code>cordova.plugins.probe.connect()</code> but receive the same error. </p>
  
  <p>How can I get the methods from <code>/plugins/com.sitename.product/www/probe.js</code> to work in <code>/lib/probe/probe.js</code>?</p>