问题描述
我正在将自定义 Cordova 插件集成到 Ionic 应用程序中.我们让第 3 方创建了一个 Cordova 插件来与蓝牙设备连接.运行 cordova platform 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
文件,其中包含连接、读取、轮询和其他操作的方法.
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;
为了在我们的控制器中使用插件,我需要创建一个 Angular 服务包装器,如 此处描述.
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;
}
}
})();
将 Probe
服务注入控制器工作正常.当我在我的设备上运行它时(使用 ionic run android
),我收到以下错误:
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/probe.js
中工作?
How can I get the methods from /plugins/com.sitename.product/www/probe.js
to work in /lib/probe/probe.js
?
推荐答案
经过多次反复试验(和 console.log
ing),我能够解决这个问题.
After much trial and error (and console.log
ing) I was able to solve this.
$window
对象有一个 probe
对象,其中包含所有需要的方法.我猜 probe
名称来自 Cordova 插件配置.
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;
}
这篇关于如何在 Angular/Ionic 服务中使用自定义 Cordova 插件方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!