Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:36:15+00:00 2026-05-27T23:36:15+00:00

i was made a bluetooth printer application (android based) for printing some text using

  • 0

i was made a bluetooth printer application (android based) for printing some text using datecs DPP-350 printer device. this program use a datecs external library such as bluetoohconnector and RFComm package. it works nicely, here’s the code:

package com.myapp.MobilePrinter1;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.djarum.MobilePrinter1.BluetoothConnector;
import com.datecs.api.card.FinancialCard;
import com.datecs.api.printer.Printer;
import com.datecs.api.printer.PrinterInformation;
import com.datecs.api.printer.ProtocolAdapter;
import com.datecs.api.printer.ProtocolAdapter.Channel;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MobilePrinter1Activity extends Activity {

    public static final String CONNECTION_STRING = "connection_string";

    private final Handler mHandler = new Handler();

    private final Thread mConnectThread = new Thread() {
        @Override
        public void run() {
            String connectionString = "bth://00:01:90:E6:40:52";

            showProgress("Connecting");

            if (connectionString.startsWith("bth://")) {
                String address = connectionString.substring(6);
                connectBth(address);
            } else {
                throw new IllegalArgumentException("Unsupported connection string");
            }

            dismissProgress();
        }   

        void connectBth(String address) {
            //setPrinterInfo(R.drawable.help, address);            

            try {
                mBthConnector = BluetoothConnector.getConnector(MobilePrinter1Activity.this);
                mBthConnector.connect(address);
                mPrinter = getPrinter(
                        mBthConnector.getInputStream(), 
                        mBthConnector.getOutputStream());               
            } catch (IOException e) {
                //error(R.drawable.bluetooth, e.getMessage());              
                return;
            }                       

            mPrinterInfo = getPrinterInfo();            
        }

        Printer getPrinter(InputStream in, OutputStream out) throws IOException {
            ProtocolAdapter adapter = new ProtocolAdapter(in, out);
            Printer printer = null;

            if (adapter.isProtocolEnabled()) {
                Channel channel = adapter.getChannel(ProtocolAdapter.CHANNEL_PRINTER);
                InputStream newIn = channel.getInputStream();
                OutputStream newOut = channel.getOutputStream();
                printer = new Printer(newIn, newOut);
            } else {
                printer = new Printer(in, out);
            }

            return printer;
        }

        PrinterInformation getPrinterInfo() {
            PrinterInformation pi = null;

            try {
                pi = mPrinter.getInformation();
                //setPrinterInfo(R.drawable.printer, pi.getName());                
            } catch (IOException e) {
                e.printStackTrace();                                
            }

            return pi;
        }
    };

    private BluetoothConnector mBthConnector;
    private Printer mPrinter;
    private PrinterInformation mPrinterInfo;
    private ProgressDialog mProgressDialog;
    private BluetoothConnector mConnector;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            mConnector = BluetoothConnector.getConnector(this);
        } catch (IOException e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
            finish();           
        }

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                printText();                
            }           
        });

        findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                printBarcode();                 
            }           
        });  

        findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                printImage();               
            }           
        });
    }

    public void printText() {
        new Thread() {                  
            @Override
            public void run() {
                //showProgress(R.string.printing_text);
                doPrintText2();
                dismissProgress();
            }
        }.start();                              
    } 

    public void printBarcode() {
        new Thread() {                  
            @Override
            public void run() {
                //showProgress(R.string.printing_text);
                doPrintBarcode();
                dismissProgress();
            }
        }.start();                              
    } 

    public void printImage() {
        new Thread() {                  
            @Override
            public void run() {
                //showProgress(R.string.printing_text);
                doPrintImage();
                dismissProgress();
            }
        }.start();                              
    } 

    @Override
    protected void onStart() {
        super.onStart();
        mConnectThread.start();        
    }   

    @Override
    protected void onStop() {
        super.onStop();

        if (mBthConnector != null) {
            try {
                mBthConnector.close();
            } catch (IOException e) {
                e.printStackTrace();
            }           
        }       
    }   

    private void showProgress(final String text) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mProgressDialog = ProgressDialog.show(
                        MobilePrinter1Activity.this,
                        "Please wait", 
                        text,
                        true);                            
            }           
        });             
    }

    private void showProgress(int resId) {
        showProgress(getString(resId));
    }

    private void dismissProgress() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {                
                mProgressDialog.dismiss();             
            }           
        });     
    }            

    private void doPrintSelfTest() {
        try {           
            mPrinter.printSelfTest();                       
        } catch (IOException e) {
            //error(R.drawable.selftest, getString(R.string.failed_print_self_test) + ". " + 
                    //e.getMessage());
        }
    }

    private void doPrintText2() {
        EditText EditText1;
        EditText1=(EditText)findViewById(R.id.editText1);
        String temp;
        try {           
            mPrinter.reset();  
            mPrinter.printTaggedText(EditText1.getText().toString());   
            //mPrinter.printTaggedText("Testing Testing!!"); 
            mPrinter.feedPaper(110);
        } catch (IOException e) {
            //error(R.drawable.text, getString(R.string.failed_print_text) + ". " + 
                    //e.getMessage());          
        }
    }

    private void doPrintBarcode() {
        EditText EditText1;
        EditText1=(EditText)findViewById(R.id.editText1);
        try {           
            mPrinter.reset();

            mPrinter.setBarcode(Printer.ALIGN_CENTER, false, 2, Printer.HRI_BOTH, 100);
            mPrinter.printBarcode(Printer.BARCODE_CODE128, EditText1.getText().toString());
            mPrinter.feedPaper(38);

            mPrinter.feedPaper(110);
        } catch (IOException e) {
            //error(R.drawable.barcode, getString(R.string.failed_print_barcode) + ". " +
                    //e.getMessage());          
        }
    }
    private void doPrintImage() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo_djarum);
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        final int[] argb = new int[width * height];     
        bitmap.getPixels(argb, 0, width, 0, 0, width, height);              

        try {
            mPrinter.reset();            
            mPrinter.printImage(argb, width, height, Printer.ALIGN_LEFT, true);
            mPrinter.feedPaper(110);            
        } catch (IOException e) {
            Toast.makeText(MobilePrinter1Activity.this, e.getMessage(), 1).show();
        }
    }

    private void dialog(final int id, final String title, final String msg) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                AlertDialog dlg = new AlertDialog.Builder(MobilePrinter1Activity.this)
                .setTitle(title)
                .setMessage(msg)               
                .create();  
                dlg.setIcon(id);
                dlg.show();             
            }           
        });             
    }

    private void error(final int resIconId, final String message) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {             
                AlertDialog dlg = new AlertDialog.Builder(MobilePrinter1Activity.this)
                .setTitle("Error")
                .setMessage(message)               
                .create();
                dlg.setIcon(resIconId);
                dlg.setOnDismissListener(new OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        MobilePrinter1Activity.this.finish();
                    }                   
                });
                dlg.show();             
            }           
        });             
    }

    private void setPrinterInfo(final int resIconId, final String text) {
        mHandler.post(new Runnable() {          
            @Override
            public void run() {
                //((ImageView)findViewById(R.id.icon)).setImageResource(resIconId);
                //((TextView)findViewById(R.id.name)).setText(text);
            }
        });
    }
}

the main problems now is how to call this program from phonegap? i’ve tried using droidGap but it will give me error when i start the printer’s thread. has anyone know how to solved this?? many thanks..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:36:16+00:00Added an answer on May 27, 2026 at 11:36 pm

    I dont think that you can invoke too many APIs from the standard android browser (except some like location, contacts n all), but what is possible is other way round embedding a webview in a native app(which can be your above mentioned thread code) and invoking this code from a Javascript event using JavaScript Interface apis of android platform (which is pretty straight forward).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i make Android application that can communicate to bluetooth printer for printing some text,
I made an application that is using Android SDK 2.1. This application uses bluetooth
I made the android Bluetooth-Chat tutorial. Instead of using the OnClickListeners in the main
I made a program to connect to a device via Bluetooth and send the
I made some classes using xjc. public class MyType { @XmlElementRefs({ @XmlElementRef(name = MyInnerType,
currently writes an application to connect to the device BTLink Bluetooth to Serial Adapter
Made this custom alert box: <script type="text/javascript"> $(function () { var $alert = $('#alert');
I wish I could find this list before I made it. If for some
Made an application which targets .net 4 client. Created an installer using Inno Setup
Made this nice little loop for hiding and showing div's, works as a charm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.