我工作的一个自定义插件科尔多瓦整合到一个应用程序离子。我们有一个第三方创建插件科尔多瓦与蓝牙设备连接。运行科尔多瓦平台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;
为了使用这个插件在我们的控制器我需要创建一个角服务的包装,如这里描述。
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;
}
}
})();
注射的探头
服务为一体的控制器正常工作。当我在我的设备上运行这个(使用离子运行的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.js
?
推荐答案
多的试验和错误(和的console.log
ING)我能解决这个问题之后。
After much trial and error (and console.log
ing) 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;
}
相关推荐
最新文章