gpt4 book ai didi

java - android phonegap 自定义插件需要的建议

转载 作者:太空狗 更新时间:2023-10-29 13:34:31 26 4
gpt4 key购买 nike

我在 phonegap(html5、JQuery、JS)中开发了一个应用程序,我想开发一个插件来打印到 BT 打印机。

我下载了打印机制造商的 SDK,并使用我在项目中需要的所有方法将适当的 .jar 文件导入到我的项目中。

我按照互联网教程创建了以下插件,以便从 JS 调用打印机制造商 SDK 的 JAVA 方法。

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) {

if (NATIVE_ACTION_STRING.equals(action)) {
this.ctx.runOnUiThread(new Runnable()
{
public void run()
{
String resultType = null;
StarIOPort port = null;
String message = null;
String portName = "bt:";
String portSettings = "mini";
byte[] texttoprint = new byte[]{0x1b, 0x40, 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(portName, portSettings, 10000);

try
{
Thread.sleep(500);
}
catch(InterruptedException e) {}

}
catch (StarIOPortException e)
{

Builder dialog = new AlertDialog.Builder((Context)ctx);
dialog.setNegativeButton("Ok", null);
AlertDialog alert = dialog.create();
alert.setTitle("Failure");
alert.setMessage("Failed to connect to printer");
alert.show();
}
finally
{
if(port != null)
{
try
{
StarIOPort.releasePort(port);
} catch (StarIOPortException e) {}
}
}
}
});
}
return null;
}
}

打印机命令手册说:

GetPort 是您将用来“打开”打印机端口的工具。使用其中一个有效之前提到的 portName 和 portSettings 的输入,你可以通过你的连接字符串到 StarIO 类中,以便它正确设置其私有(private)变量。

//The following would be an actual usage of getPort:
StarIOPort port = null;
try
{
port = StarIOPort.getPort(portName, portSettings, 10000);
}
catch (StarIOPortException e)
{
//There was an error opening the port
}

StarIOPort 是 StarIO 的一部分,这将允许您创建一个“端口”句柄。这上面的示例显示了正在创建的端口并设置为 null 然后被分配了实际的包含 getPort 的以下行上的端口 Hook 。使用 getPort 时始终使用 try,catch。如果端口打不开由于连接问题,您的程序将崩溃,除非您使用尝试,像上面的例子一样捕捉。

上面的插件语法是正确的还是我遗漏了什么?

当我运行我的应用程序时,我总是收到“无法连接到打印机”,即使打印机已打开并连接到我的设备也是如此。

最佳答案

试试这个:

public PluginResult execute(String action, JSONArray data, String callbackId) {
PluginResult result = null;
if (PRINT_ACTION.equals(action))
{
JSONObject printerStatusJSON = new JSONObject();
try {
Boolean prtStatus = false;
String msg ="Failed to connect to printer";
String portName = "";
ArrayList<PortInfo> dvss = PrinterFunctions.getDevices();//BTPortList = StarIOPort.searchPrinter("BT:");

if (Looper.myLooper() == null) {
Looper.prepare();
}

for(PortInfo dvs : dvss) {
Map<String, String> st = PrinterFunctions.CheckStatus(dvs.getPortName(), "mini");//port = StarIOPort.getPort(portName, portSettings, 1000);
if(st.get("status") == "true") {
prtStatus = true;
portName = st.get("portName");
break;
}
msg = st.get("message");
}

if(!portName.isEmpty()) {
PrinterFunctions.PrintSomething(portName, data);//MiniPrinterFunctions.PrintSampleReceipt(String portName, JSONArray data);
}

printerStatusJSON.put("prtStatus", prtStatus);
printerStatusJSON.put("message", msg);

result = new PluginResult(Status.OK, printerStatusJSON);
}
catch (Exception jsonEx) {
Log.e("YourApplicationName", "Got JSON Exception " + jsonEx.getMessage());
jsonEx.printStackTrace();
result = new PluginResult(Status.JSON_EXCEPTION);
}
}
else {
result = new PluginResult(Status.INVALID_ACTION);
Log.e(TAG, "Invalid action : " + action);
}
return result;
}

关于java - android phonegap 自定义插件需要的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12042470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com