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
?