Android development: btPrint4 prints demos and files to Bluetooth receipt and label printers

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

    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:

    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 a asset and a real file:

    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 (git)

APK download (here): [Download not found]

Leave a Reply