Android Development: btPrint4 Prints Demos and Files to Bluetooth Receipt and Label Printers

Dec 18, 2014

1 min read

C++

Windows

Author picture

by hjgode

Contributor

12k Views

This is an update to my btPrint4 Android application. The app now supports ‘printing’ of files. You are no longer tied to the provided demo files. ‘Printing’ here means it sends the file as is to the printer. So, watch your step and do not send files that your printer does not understand.

btprin4_file_main btprint4_file_selected btprint4_file_browse

Added a file browser activity: a class named item to hold file information, a FileChooser class and a FileArrayAdapter. Then the needed layout files are added.

The main screen now has a new button to start the FileBrowser (FileChooser).

CPP
    boolean bFileBrowserStarted=false;
    void startFileBrowser(){
        if(bFileBrowserStarted)
            return;
        bFileBrowserStarted=true;
        Intent intent1 = new Intent(this, FileChooser.class);
        startActivityForResult(intent1, REQUEST_SELECT_FILE);
    }

The main code is extended to recognize the new activity result:

CPP
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
            case REQUEST_SELECT_FILE:
                if (resultCode == RESULT_OK) {
                    String curFileName = data.getStringExtra("GetFileName");
                    String curPath = data.getStringExtra("GetPath");
                    if(!curPath.endsWith("/")) {
                        curPath += "/";
                    }
                    String fullName=curPath+curFileName;
                    mTxtFilename.setText(fullName);
                    addLog("Filebrowser Result_OK: '"+fullName+"'");
                }
                bFileBrowserStarted=false;
                break;
            case REQUEST_SELECT_DEMO_FILE:

The code to print was simple to change, as we just need to distinguish between an asset and a real file:

CPP
    void printFile() {
        String fileName = mTxtFilename.getText().toString();
        if (btPrintService.getState() != btPrintFile.STATE_CONNECTED) {
            myToast("Please connect first!", "Error");
            //PROBLEM: this Toast does not work!
            //Toast.makeText(this, "please connect first",Toast.LENGTH_LONG);
            return; //does not match file pattern for a print file
        }

        //do a query if escp
        if (fileName.startsWith("escp")) {
            byte[] bufQuery = escpQuery();
            btPrintService.write(bufQuery);
        }
        if (mTxtFilename.length() > 0) {
            //TODO: add code
            InputStream inputStream = null;
            ByteArrayInputStream byteArrayInputStream;
            Integer totalWrite = 0;
            StringBuffer sb = new StringBuffer();
            try {

Code changes at GitHub

Download APK

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)