我的PhoneGap开发的移动应用程序(HTML5,JQuery的,JS),我想开发一个插件来打印到打印机BT
I developed a mobile app in phonegap (html5, JQuery, JS) and I want to develop a plugin to print to a BT printer.
我下载的打印机制造商的SDK和我导入相应的.jar文件到我的项目。
I download printer manufacturer's SDK and I imported the appropriate .jar file to my project.
我创建以下插件
JS
var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
return cordova.exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeAction", [resultType]);
}
};
Java的
package com.tricedesigns;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import com.starmicronics.stario.StarIOPort;
import com.starmicronics.stario.StarIOPortException;
import com.starmicronics.stario.StarPrinterStatus;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.util.Log;
public class HelloPlugin extends Plugin {
public static final String NATIVE_ACTION_STRING="nativeAction";
public static final String SUCCESS_PARAMETER="success";
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!");
//only perform the action if it is the one that should be invoked
if (NATIVE_ACTION_STRING.equals(action)) {
String resultType = null;
StarIOPort port = null;
byte[] texttoprint = new byte[]{0x1b,0x74,0x0D,(byte) 0x91,(byte) 0x92,(byte) 0x93,(byte) 0x94,(byte) 0x95,(byte) 0x96,(byte) 0x97,(byte) 0x98,(byte) 0x99,0x0A,0x0A,0x0A,0x0A,0x0A};
try
{
port = StarIOPort.getPort("BT:", "mini", 10000, null);
try
{
Thread.sleep(500);
}
catch(InterruptedException e) {}
port.writePort(texttoprint, 0, texttoprint.length);
try
{
Thread.sleep(3000);
}
catch(InterruptedException e) {}
resultType = "success";
}
catch (StarIOPortException e)
{
resultType = "error";
}
if (resultType.equals(SUCCESS_PARAMETER)) {
return new PluginResult(PluginResult.Status.OK, "Yay, Success!!!");
}
else {
return new PluginResult(PluginResult.Status.ERROR, "Oops, Error :(");
}
}
return null;
}
}
HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
<script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>
<script type="text/javascript" charset="utf-8" src="HelloPlugin.js"></script>
<script type="text/javascript">
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
/* When this function is called, Cordova has been initialized and is ready to roll */
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */
function onDeviceReady()
{
// do your thing!
navigator.notification.alert("Cordova is working")
}
function callNativePlugin( returnSuccess ) {
HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
}
function nativePluginResultHandler (result) {
alert("SUCCESS: \r\n"+result );
}
function nativePluginErrorHandler (error) {
alert("ERROR: \r\n"+error );
}
</script>
</head>
<body onload="onBodyLoad()">
<h1>Hey, it's Cordova!</h1>
<button onclick="callNativePlugin('success');">Click to invoke the Native Plugin with an SUCCESS!</button>
<button onclick="callNativePlugin('error');">Click to invoke the Native Plugin with an ERROR!</button>
</body>
</html>
该虚拟设备上运行时
正在给总是报错,因为是打印机没有连接,但在给予以下错误真实设备上运行时:
which when running on virtual device is working giving always error as there is no connection to printer but when running on real device giving the following error:
错误:无法内螺纹创建的处理程序已经不叫尺蠖prepare()
ERROR: Can't create handler inside thread that has not called Looper.prepare()
我在哪里错了?
请指教
我有这似乎有些设备上,同样的问题。成功一体的智能男孩,托比,帮助了我。所以,解决办法是下一个: - 你调用任何StarIOPort的方法之前,你必须检查是否存在尺蠖:
I had the same problem which appeared on some devices. Successfully one smart boy, Toby, helped me. So, solution is the next: - before you call any StarIOPort's methods you have to check if looper exist:
if (Looper.myLooper() == null) {
Looper.prepare();
}
在你的情况下,它会看起来像这样:
in your case it's will looks like this:
try
{
if (Looper.myLooper() == null) {
Looper.prepare();
}
port = StarIOPort.getPort("BT:", "mini", 10000, null);
try
{
Thread.sleep(500);
}
catch(InterruptedException e) {}
port.writePort(texttoprint, 0, texttoprint.length);
try
{
Thread.sleep(3000);
}
catch(InterruptedException e) {}
resultType = "success";
}
catch (StarIOPortException e)
{
resultType = "error";
}
还有一个提醒:而不是
One more advise: instead
port = StarIOPort.getPort("BT:", "mini", 10000, null);
使用刚
port = StarIOPort.getPort("BT:", "mini", 10000);
在插件,您将无法使用右键
in plugin you will not use Context
祝你好运。
上一篇:添加XML字符串的XElement字符串、XML、XElement
下一篇:在 docker 下安装 composer-plugin-api 时出错composer、docker、api、plugin