Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

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

5.00/5 (1 vote)
18 Dec 2014CPOL 12K  
This is an update to my btPrint4 Android application.

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

C++
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:

C++
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:

C++
    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 {
                //TODO: test if this is a storage file or a Assets resource file?
                if(fileName.startsWith("/")){
                    inputStream = new FileInputStream(fileName);
                    addLog("Using regular file: '"+fileName+"'");
                }
                else {
                    inputStream = this.getAssets().open(fileName);
                    addLog("Using demo file: '"+fileName+"'");
                }

                byte[] buf = new byte[2048];
                int readCount = 0;
     ...

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)