Android Question bluetooth prinder driver (jar file)

zekigultekin

Member
Licensed User
Longtime User
Hi everybody,
In my project i have to use bluetooth printer. And i have jar file how can i use it.

java code so simple but i coulndt translate it b4a

B4X:
    //Create the Bitmap           
        Bitmap image = BitmapFactory.decodeFile(selectedImagePath);
        synchronized (lock)
        {
            //***************************************************************************
            // PRINT PICTURE
            //***************************************************************************
           
            try
            {
                //Print (Left Align and Fit to printer width)
                prnDevice.printImage(image,CustomPrinter.IMAGE_SCALE_TO_WIDTH , CustomPrinter.IMAGE_SCALE_TO_FIT,0);
           
            }
            catch(CustomException e )
            {               
                //Show Error
                showAlertMsg("Error...", e.getMessage());               
            }
            catch(Exception e )
            {
                showAlertMsg("Error...", "Print Picture Error...");
            }
           
            //***************************************************************************
            // FEEDS and CUT
            //***************************************************************************
 

Attachments

  • customandroidapi.jar
    245 KB · Views: 323

zekigultekin

Member
Licensed User
Longtime User
i have a some demo code for java but i couldn convert it. it uses library.


B4X:
//************************************************************************************
//*                                                                                  *
//* This document contains programming examples.                                    *
//*                                                                                  *
//* CUSTOM S.p.A. grants you a nonexclusive copyright license to use                *
//* all programming code examples from which you can generate similar                *
//* function tailored to your own specific needs.                                    *
//*                                                                                  *
//* All sample code is provided by CUSTOM S.p.A. for illustrative purposes          *
//* only. These examples have not been thoroughly tested under all conditions.      *
//* CUSTOM S.p.A., therefore, cannot guarantee or imply reliability,                *
//* serviceability, or function of these programs.                                  *
//*                                                                                  *
//* In no event shall CUSTOM S.p.A. be liable for any direct, indirect,              *
//* incidental, special, exemplary, or consequential damages (including, but not    *
//* limited to, procurement of substitute goods or services; loss of use, data,      *
//* or profits; or business interruption) however caused and on any theory of        *
//* liability, whether in contract, strict liability, or tort (including negligence  *
//* or otherwise) arising in any way out of the use of this software, even if        *
//* advised of the possibility of such damage.                                      *
//*                                                                                  *
//* All programs contained herein are provided to you "as is" without any            *
//* warranties of any kind.                                                          *
//* The implied warranties of non-infringement, merchantability and fitness for a    *
//* particular purpose are expressly disclaimed.                                    *
//*                                                                                  *
//************************************************************************************

package custom.api.android.demo.bt;

import it.custom.printer.api.android.*;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice;
import android.content.*;
import android.database.Cursor;
import android.graphics.*;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import java.util.Locale;

public class DemoCustomAndroidBTActivity extends Activity
{
    private int INT_SELECT_PICTURE = 1;
   
    private int GETSTATUS_TIME = 1000;        //1sec
   
    public String selectedImagePath = "";
   
    static BluetoothDevice[] btDeviceList = null;
    static CustomPrinter prnDevice = null;
   
    static ListView listDevicesView ; 
    static ArrayAdapter<String> listAdapter; 
   
    static int lastDeviceSelected = -1;
    static int deviceSelected = -1;
   
    private String lock="lockAccess";
   
    static Handler hGetStatus = new Handler();
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);             
       
        //Get API Version               
        String strAPIVersion = CustomAndroidAPI.getAPIVersion();       
                       
        //Change Application Title
        setTitle(getResources().getString(R.string.app_name) + " - API Rel "+strAPIVersion);
       
        //Start the get status thread after GETSTATUS_TIME msec
        hGetStatus.postDelayed(GetStatusRunnable, GETSTATUS_TIME);
       
        //Init everything
        InitEverything(savedInstanceState);
    }   

   
   
    private void InitEverything(Bundle savedInstanceState)
    {
        //If is the 1st time
        if (savedInstanceState == null)
        {                                   
            try
            {
                //Get the list of devices
                btDeviceList = CustomAndroidAPI.EnumBluetoothDevices();
               
                if ((btDeviceList == null) || (btDeviceList.length == 0))
                {
                    //Show Error
                    showAlertMsg("Error...", "No Devices Connected...");                       
                    return;
                }                                 
            }
            catch(CustomException e )
            {
               
                //Show Error
                showAlertMsg("Error...", e.getMessage());
                return;
            }
            catch(Exception e )
            {
               
                //Show Error
                showAlertMsg("Error...", "Enum devices error...");
                return;
            }           
        }
               
        // Find the ListView resource. 
        listDevicesView = (ListView)findViewById( R.id.listViewDevices ); 
     
        // Create and populate a List of Devices
        String[] strDevices = new String[btDeviceList.length];
        for (int i=0;i<btDeviceList.length;i++)
        {
            strDevices[i] = (i+1)+". BT Device : " + btDeviceList[i].getName();                   
        }
       
        ArrayList<String> devicesList = new ArrayList<String>(); 
        devicesList.addAll( Arrays.asList(strDevices) ); 
         
        // Create ArrayAdapter using the list. 
        listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, devicesList);                                     
         
        // Set the ArrayAdapter as the ListView's adapter. 
        listDevicesView.setAdapter( listAdapter );         
       
        listDevicesView.setItemsCanFocus(false);       
        listDevicesView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        deviceSelected = 0;
        listDevicesView.setItemChecked(deviceSelected, true); //Select the 1st
        listDevicesView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {               
                //Save position Value
                deviceSelected = arg2;               
            }
        });
    }
   
    private String getPath(Uri uri)
    {
        String imagePath;
       
        //1st, i try to convert it if it was into the gallery
        try
        {                   
            //if the Uri
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            imagePath = cursor.getString(column_index);
        }
        catch(Exception e)
        {
            //it fails so it is a standard file
            imagePath = uri.getPath();
        }
       
        String imagePathLCase = imagePath.toLowerCase(Locale.ENGLISH);
        //check file extension
        if ( (imagePathLCase.contains("jpg")) || (imagePathLCase.contains("jpeg")) || (imagePathLCase.contains("bmp")) || (imagePathLCase.contains("tiff")) || (imagePathLCase.contains("gif")) || (imagePathLCase.contains("png")))
                return imagePath;       
        else
            return "";
    }
   
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
        {
            //If i select the picture, save the path
            if (requestCode == INT_SELECT_PICTURE)
            {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Button printPictureButton = (Button) findViewById(R.id.buttonPrintPicture);
                //change button text
                if (selectedImagePath != "")
                    printPictureButton.setText(this.getString(R.string.printpicture) + " (" + selectedImagePath+")");
                else
                    printPictureButton.setText(this.getString(R.string.printpicture));
            }           
        }
    }
   
    public void onSelectPictureClick(View view) 
    { 
        //Open the Gallery to select the picture
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");               
        startActivityForResult(Intent.createChooser(intent,null), INT_SELECT_PICTURE);   
    }
   
    public void onPrintPictureClick(View view) 
    { 
        //open device
        if (OpenDevice() == false)
            return;
       
        //Check image
        if (selectedImagePath == "")
        {
            showAlertMsg("Error...", "Select a Picture to Print...");
            return;
        }
       
        //Create the Bitmap           
        Bitmap image = BitmapFactory.decodeFile(selectedImagePath);
        synchronized (lock)
        {
            //***************************************************************************
            // PRINT PICTURE
            //***************************************************************************
           
            try
            {
                //Print (Left Align and Fit to printer width)
                prnDevice.printImage(image,CustomPrinter.IMAGE_SCALE_TO_WIDTH , CustomPrinter.IMAGE_SCALE_TO_FIT,0);
           
            }
            catch(CustomException e )
            {               
                //Show Error
                showAlertMsg("Error...", e.getMessage());               
            }
            catch(Exception e )
            {
                showAlertMsg("Error...", "Print Picture Error...");
            }
           
            //***************************************************************************
            // FEEDS and CUT
            //***************************************************************************
           
            try
            {
                //Feeds (3)
                prnDevice.feed(3);
                //Cut (Total)
                prnDevice.cut(CustomPrinter.CUT_TOTAL);               
            }
            catch(CustomException e )
            {   
                //Only if isn't unsupported
                if (e.GetErrorCode() != CustomException.ERR_UNSUPPORTEDFUNCTION)
                {
                    //Show Error
                    showAlertMsg("Error...", e.getMessage());
                }
            }
            catch(Exception e )
            {
                showAlertMsg("Error...", "Print Picture Error...");
            }
           
            //***************************************************************************
            // PRESENT
            //***************************************************************************
           
            try
            {               
                //Present (40mm)
                prnDevice.present(40);
            }
            catch(CustomException e )
            {   
                //Only if isn't unsupported
                if (e.GetErrorCode() != CustomException.ERR_UNSUPPORTEDFUNCTION)
                {
                    //Show Error
                    showAlertMsg("Error...", e.getMessage());
                }
            }
            catch(Exception e )
            {
                showAlertMsg("Error...", "Print Picture Error...");
            }
        }
    }

    public void onPrintText(View view) 
    { 
        PrinterFont fntPrinterNormal = new PrinterFont();
        PrinterFont fntPrinterBold2X = new PrinterFont();
        String strTextToPrint;
        //open device
        if (OpenDevice() == false)
            return;
       
        //Get Text
        strTextToPrint = ((EditText)findViewById( R.id.EditTextToPrint)).getText().toString();
       
        try
        {
            //Fill class: NORMAL
            fntPrinterNormal.setCharHeight(PrinterFont.FONT_SIZE_X1);                    //Height x1
            fntPrinterNormal.setCharWidth(PrinterFont.FONT_SIZE_X1);                    //Width x1
            fntPrinterNormal.setEmphasized(false);                                        //No Bold
            fntPrinterNormal.setItalic(false);                                            //No Italic
            fntPrinterNormal.setUnderline(false);                                        //No Underline
            fntPrinterNormal.setJustification(PrinterFont.FONT_JUSTIFICATION_CENTER);    //Center
            fntPrinterNormal.setInternationalCharSet(PrinterFont.FONT_CS_DEFAULT);        //Default International Chars
           
            //Fill class: BOLD size 2X
            fntPrinterBold2X.setCharHeight(PrinterFont.FONT_SIZE_X2);                    //Height x2
            fntPrinterBold2X.setCharWidth(PrinterFont.FONT_SIZE_X2);                    //Width x2
            fntPrinterBold2X.setEmphasized(true);                                        //Bold
            fntPrinterBold2X.setItalic(false);                                            //No Italic
            fntPrinterBold2X.setUnderline(false);                                        //No Underline
            fntPrinterBold2X.setJustification(PrinterFont.FONT_JUSTIFICATION_CENTER);    //Center           
            fntPrinterBold2X.setInternationalCharSet(PrinterFont.FONT_CS_DEFAULT);        //Default International Chars           
        }
        catch(CustomException e )
        {
           
            //Show Error
            showAlertMsg("Error...", e.getMessage());
        }
        catch(Exception e )
        {
            showAlertMsg("Error...", "Set font properties error...");
        }
       
        //***************************************************************************
        // PRINT TEXT
        //***************************************************************************
               
        synchronized (lock)
        {           
            try
            {
                //Print Text (NORMAL)
                prnDevice.printText(strTextToPrint, fntPrinterNormal);
                prnDevice.printTextLF(strTextToPrint, fntPrinterNormal);
                //Print Text (BOLD size 2X)
                prnDevice.printTextLF(strTextToPrint, fntPrinterBold2X);
            }
            catch(CustomException e )
            {               
                //Show Error
                showAlertMsg("Error...", e.getMessage());
            }
            catch(Exception e )
            {
                showAlertMsg("Error...", "Print Text Error...");
            }
        }
    }
   
    public void onExit(View view) throws Throwable 
    {
        try
        {
            if (prnDevice != null)
            {
                //Close device
                prnDevice.close();
            }
        }
        catch(CustomException e )
        {               
            //Show Error
            showAlertMsg("Error...", e.getMessage());
        }
        catch(Exception e )
        {           
        }
       
        //Force Close
        android.os.Process.killProcess(android.os.Process.myPid());
    }
   
    private Runnable GetStatusRunnable = new Runnable()
    {
        public void run()
        {
            String printerName;
            int deviceShowStatus = View.INVISIBLE;
            CheckBox ckbox;
            TextView txtView;
           
            //If the device is open
            if (prnDevice != null)
            {
                synchronized (lock)
                {
                    try
                    {
                       
                        //Get printer Status
                        PrinterStatus prnSts = prnDevice.getPrinterFullStatus();
                       
                        //Check it: NOPAPER
                        ckbox = (CheckBox)findViewById( R.id.checkBoxNOPAPER);                                   
                        ckbox.setChecked(prnSts.stsNOPAPER);
                       
                        //Check it: PAPER ROLLING
                        ckbox = (CheckBox)findViewById( R.id.checkBoxROLLING);                                   
                        ckbox.setChecked(prnSts.stsPAPERROLLING);
                       
                        //Check it: LF KEY PRESSED
                        ckbox = (CheckBox)findViewById( R.id.checkBoxLF);                                   
                        ckbox.setChecked(prnSts.stsLFPRESSED);
                       
                        //Get printer name
                        printerName = prnDevice.getPrinterName();
                       
                        //Show Text PrinterName
                        txtView = (TextView)findViewById( R.id.textPrinterName);                                   
                        txtView.setText("Printer Name:" + printerName + " (" +prnDevice.getPrinterInfo()+")");
                       
                        deviceShowStatus = View.VISIBLE;
                       
                    }
                    catch(CustomException e )
                    {
                       
                    }
                    catch(Exception e)
                    {
                       
                    }
                }
            }
               
            //Show / Hide Check NOPAPER
            ckbox = (CheckBox)findViewById( R.id.checkBoxNOPAPER);                                   
            ckbox.setVisibility(deviceShowStatus);
           
            //Show / Hide Check PAPER ROLLING
            ckbox = (CheckBox)findViewById( R.id.checkBoxROLLING);                                   
            ckbox.setVisibility(deviceShowStatus);
           
            //Show / Hide Check LF KEY PRESSED
            ckbox = (CheckBox)findViewById( R.id.checkBoxLF);                                   
            ckbox.setVisibility(deviceShowStatus);
           
            //Show / Hide Text PrinterName
            txtView = (TextView)findViewById( R.id.textPrinterName);                                   
            txtView.setVisibility(deviceShowStatus);
       
            //run again in GETSTATUS_TIME msec
            hGetStatus.postDelayed(GetStatusRunnable, GETSTATUS_TIME);
        }
    };
   
    //Open the device if it isn't already opened
    public boolean OpenDevice()
    {
        //Device not selected
        if (deviceSelected == -1)
        {
            showAlertMsg("Error...", "No Printer Device Selected...");
            return false;
        }
       
        //If i changed the device
        if (lastDeviceSelected != -1)
        {
            if (deviceSelected != lastDeviceSelected)
            {
                try
                {
                    //Force close
                    prnDevice.close();
                }
                catch(CustomException e )
                {
                   
                    //Show Error
                    showAlertMsg("Error...", e.getMessage());
                    return false;
                }
                catch(Exception e )
                {
                    //Show error
                    return false;
                }
                prnDevice = null;
            }
        }
       
        //If i never open it
        if (prnDevice == null)
        {
            try
            {                               
                //Open and connect it
                prnDevice = new CustomAndroidAPI().getPrinterDriverBT(btDeviceList[deviceSelected]);
                //Save last device selected
                lastDeviceSelected = deviceSelected;
                return true;
            }
            catch(CustomException e )
            {
               
                //Show Error
                showAlertMsg("Error...", e.getMessage());
                return false;
            }
            catch(Exception e )
            {
                showAlertMsg("Error...", "Open Print Error...");
                //open error
                return false;
            }
        }
        //Already opened
        return true;
       
    }   
   
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);
        // Save UI state changes to the savedInstanceState.
        // This bundle will be passed to onCreate if the process is
        // killed and restarted.                       
    }
       
   
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        //Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
               
    }
   
    void showAlertMsg(String title,String msg)
    {
        AlertDialog.Builder dialogBuilder;       
        dialogBuilder = new AlertDialog.Builder(this);       
       
        dialogBuilder.setNeutralButton( "OK", new DialogInterface.OnClickListener()
        {           
            public void onClick(DialogInterface dialog, int which) {               
                dialog.dismiss();               
            }
        });
       
        dialogBuilder.setTitle(title);       
        dialogBuilder.setMessage(msg);       
        dialogBuilder.show();
       
    }   
}
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code will hopefully help you get started:
1. Get a reference to the BluetoothDevice:
B4X:
Dim r As Reflector
r.Target = Serial1 'Serial1 is an initialized Serial object
Dim adapter As JavaObject = r.GetField("blueAdapter")
Dim BluetoothDevice As JavaObject = adapter.RunMethod("getRemoteDevice", Array("printer mac address"))
2. Initialize prnDevice:
B4X:
Dim prnDevice As JavaObject
prnDevice = prnDevice.InitializeNewInstance("it.custom.printer.api.android.CustomAndroidAPI", null).RunMethod( _
 "getPrinterDriverBT", Array(BluetoothDevice))
'
prnDevice.printImage(image,CustomPrinter.IMAGE_SCALE_TO_WIDTH , CustomPrinter.IMAGE_SCALE_TO_FIT,0)
3. Print:
B4X:
Dim CustomPrinter As JavaObject
CustomPrinter.InitializeStatic("it.custom.printer.api.android.CustomPrinter")
prnDevice.RunMethod("printImage", Array(image, CustomPrinter.GetField("IMAGE_SCALE_TO_WIDTH"), _
 CustomPrinter.GetField("CustomPrinter.IMAGE_SCALE_TO_FIT", 0))
 
Upvote 0

zekigultekin

Member
Licensed User
Longtime User
where is my mistake ?

Ma97RGg.jpg
a>
 
Upvote 0

zekigultekin

Member
Licensed User
Longtime User
now i am trying to bu now its give diffent error

every javaobject.


B4X:
Parsing code.                          0.00
Compiling code.                        Error
Error compiling program.
Error description: 'as' expected.
Occurred on line: 49
Dim BluetoothDevice As JavaObject = adapter.RunMethod("getRemoteDevice", Array("50:E8:00:F5:89:5C"))
Word: (
 
Upvote 0

zekigultekin

Member
Licensed User
Longtime User
i have added additionaljar and i compiler the project. and it run successful. but when i press the button to printting.

B4X:
** Activity (main) Resume **
java.lang.reflect.InvocationTargetException


    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:109)
    at b4a.example.main._button1_click(main.java:399)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
    at android.view.View.performClick(View.java:4084)
    at android.view.View$PerformClick.run(View.java:16987)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4794)


    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: Function failed: Error 8 (Init Communication Error)
    at it.custom.printer.api.android.CustomAndroidAPI.getPrinterDriverBT(Unknown Source)
    ... 21 more
 
Upvote 0

zekigultekin

Member
Licensed User
Longtime User
i am sure device is paired. When i used the printer's demo program, it working very well. erel please help me. i have to print it. my project last step is this.
 
Upvote 0

MDimitris

Member
Licensed User
Hi, i have the same problem.

Caused by: Function failed: Error 8 (Init Communication Error)
at it.custom.printer.api.android.CustomAndroidAPI.getPrinterDriverBT(Unknown Source)
... 21 more


Could you tell me how you solved it?
thank you in advanced
 
Upvote 0
Top