繁体   English   中英

android phonegap自定义插件所需的建议

[英]Advice needed for android phonegap custom plugin

我已经在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类,以便正确设置其私有变量。

//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的以下行上分配实际的端口挂钩。 使用getPort时总是使用try,catch。 如果由于连接问题而无法打开端口,则除非您使用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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM