簡體   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