1

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

我下载了打印机制造商的 SDK,并将适当的 .jar 文件导入到我的项目中。

我创建了以下插件

js

var HelloPlugin = {

    callNativeFunction: function (success, fail, resultType) {
        return cordova.exec(success, fail, "com.tricedesigns.HelloPlugin", "nativeAction", [resultType]);
    }
};

爪哇

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://iosdevelopertips.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>

当在虚拟设备上运行时,由于没有连接到打印机而总是出错,但在真实设备上运行时会出现以下错误:

错误:无法在未调用 Looper.prepare() 的线程内创建处理程序

4

2 回答 2

1

我遇到了在某些设备上出现的相同问题。一个聪明的男孩托比成功地帮助了我。因此,下一个解决方案是:
- 在调用任何 StarIOPort 的方法之前,您必须检查 looper 是否存在:

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

在你的情况下,它看起来像这样:

 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";
 }

另一个建议:
相反

port = StarIOPort.getPort("BT:", "mini", 10000, null);

只用

port = StarIOPort.getPort("BT:", "mini", 10000);

在插件中你不会使用 Context

祝你好运。

于 2012-09-13T13:53:44.143 回答
1

我只需要将我的执行包含在下面的代码中

        this.ctx.runOnUiThread(new Runnable()
        {
            public void run()
            {
            // my code
            }
        });
于 2012-08-18T11:42:19.713 回答