0

After setting up a printer through services plugin (e.g. Steps 1..3) on Android OS, my working flow should:

  • Press "print" button
  • Show dialog with available printers (previously defined on Android settings)
  • Execute pdf printing

Is it possible to acquire this list of available printers inside my own application? How?

So far, closest solution I got running through google's documentation was to open my pdf on a web preview and from there let Android handle everything. However, if possible, I wouldn't like to break my UX. After selecting my printer, ideal scenario would be to print pdfs directly.

Thanks in advance

------- STEPS -------

  1. Android Printing Settings

Android Printing Settings]

  1. Installed Services

Installed Services

  1. Printers List

Printers List

Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77

2 Answers2

0

Is it possible to acquire this list of available printers inside my own application? How?

Yes you can use PrintService: https://developer.android.com/reference/android/printservice/PrintService

A print service is responsible for discovering printers, adding discovered printers, removing added printers, and updating added printers.

The Android docs also have (somewhat outdated, but still useful) lessons on using the printing-related APIs: https://developer.android.com/training/printing

This sample includes code related to printing a PDF: https://developer.android.com/training/printing/custom-docs

Sample code from the docs:

// Connect to the print manager
private fun doPrint() {
    activity?.also { context ->
        // Get a PrintManager instance
        val printManager = context.getSystemService(Context.PRINT_SERVICE) as PrintManager
        // Set job name, which will be displayed in the print queue
        val jobName = "${context.getString(R.string.app_name)} Document"
        // Start a print job, passing in a PrintDocumentAdapter implementation
        // to handle the generation of a print document
        printManager.print(jobName, MyPrintDocumentAdapter(context), null)
    }
}
// Compute print document info
override fun onLayout(
        oldAttributes: PrintAttributes?,
        newAttributes: PrintAttributes,
        cancellationSignal: CancellationSignal?,
        callback: LayoutResultCallback,
        extras: Bundle?
) {
    // Create a new PdfDocument with the requested page attributes
    pdfDocument = PrintedPdfDocument(activity, newAttributes)

    // Respond to cancellation request
    if (cancellationSignal?.isCanceled == true) {
        callback.onLayoutCancelled()
        return
    }

    // Compute the expected number of printed pages
    val pages = computePageCount(newAttributes)

    if (pages > 0) {
        // Return print information to print framework
        PrintDocumentInfo.Builder("print_output.pdf")
                .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                .setPageCount(pages)
                .build()
                .also { info ->
                    // Content layout reflow is complete
                    callback.onLayoutFinished(info, true)
                }
    } else {
        // Otherwise report an error to the print framework
        callback.onLayoutFailed("Page count calculation failed.")
    }
}
Trevor Halvorson
  • 490
  • 1
  • 9
  • 20
  • PrintService doesn't provide access to printers that were previously settle through Settings, therefore that's not an acceptable solution to this question. – Victor Oliveira Apr 07 '19 at 21:54
0

So far, no proper solution was found. Possible alternatives are:

  1. Try to access printers through shared preferences stored by services plugins (e.g. PrintHand).

  2. If you just to want to ease acquisition of printer's IP address & case you're using devices with bar code scanner, you could place a label on the printer with its IP Address and make a scan to store the address on a session/singleton field.

I took the second path.

Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77