question

dbriese avatar image
dbriese asked ·

Printing from SDK not working

I wrote a simple test app to print to the clover receipt printer but it's not working. The code completes without error, the print method is called, but no job is added to the queue and nothing prints on the printer.

Here's the code I'm using... any idea?

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final EditText memo = (EditText) findViewById(R.id.receipt);

    final String receipt = memo.getText().toString();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    Account account = CloverAccount.getAccount(MainActivity.this);
                    PrinterConnector pc = new PrinterConnector(MainActivity.this, account, null);
                    List<Printer> pl = null;
                    try {
                        pl = pc.getPrinters();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    } catch (ClientException e) {
                        e.printStackTrace();
                    } catch (ServiceException e) {
                        e.printStackTrace();
                    } catch (BindingException e) {
                        e.printStackTrace();
                    }
                    Printer prn = pl.get(0);
                    new TextPrintJob.Builder().text(receipt).build().print(MainActivity.this, account, prn);
                    return null;
                }

                @Override
                protected void onPostExecute(Void s) {
                    super.onPostExecute(s);
                    Toast.makeText(MainActivity.this, "Printed.", Toast.LENGTH_LONG).show();
                }
            }.execute();
        }
    });
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

dbriese avatar image
dbriese answered ·

For everyone's benefit, you must specify the GET_ACCOUNTS permission in the manifest using a <uses-permission> tag. It does not work to include the permission on the activity or application with an android:permission attribute.

An account must be valid for passing to the print routine and you can't get an account without global GET_ACCOUNTS permission.

    <uses-permission
    android:name="android.permission.GET_ACCOUNTS"/>
Share
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community