kosbou
kosbou

Reputation: 3999

Android Phonegap Plugin different result on virtual and real device (Looper.prepare() ERROR)

I developed a mobile app in phonegap (html5, JQuery, JS) and I want to develop a plugin to print to a BT printer.

I download printer manufacturer's SDK and I imported the appropriate .jar file to my project.

I create the following plugin

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

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:

ERROR: Can't create handler inside thread that has not called Looper.prepare()

Upvotes: 1

Views: 733

Answers (2)

Alex
Alex

Reputation: 46

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

use just

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

in plugin you will not use Context

Good luck.

Upvotes: 1

kosbou
kosbou

Reputation: 3999

I just have to include my execute inside the below code

        this.ctx.runOnUiThread(new Runnable()
        {
            public void run()
            {
            // my code
            }
        });

Upvotes: 1

Related Questions