"Go" Android Printer-no device driver SDK, self-USB printing function

Source: Internet
Author: User

Original: http://blog.csdn.net/johnwcheung/article/details/71576833

Android under the device debugging, if the device provides a driver, according to the manufacturer's driver debugging can be, the device does not provide the driver, only in accordance with the common method of debugging.

For Smart POS, cash registers, and other printing devices, how can the print function be implemented if the vendor does not provide a printing-related SDK? In fact, we can be based on the USB communication mechanism, to implement the print driver.

The entire implementation process is as follows
    1. Initialize the printer: first to obtain the USB manager, and then to register to listen for USB device plug-in changes and request permission to broadcast, and finally list all USB devices, and all request to obtain USB permissions;

    2. Implement this broadcast receiver: when receiving the request permission of the broadcast, obtain a reference to the USB device, the Android system will ask you whether to allow device access, the default is False, when access is allowed to determine whether the USB reference is null, if not empty, the device can be connected, Otherwise, the device is denied access, or if a broadcast that has been removed by a connected print device is received, the connection is closed.

    3. For USB access that can be used, we will acquire the device's feature set (Usbinterface) and communication channel (Usbendpoint) and then create a connection between the host and device to transfer the data.

    4. Finally, we can send instructions to control the printer that is already connected by invoking the above encapsulated print method where we need to print.

USB Printer Implementation
/** * USB Printer * Created by John on 17-5-10. */PublicClassUsbprinter {PrivateStaticFinal String action_usb_permission ="Com.usb.printer.USB_PERMISSION";PrivateStatic Usbprinter minstance;Private Context Mcontext;Private Usbdevice Musbdevice;Private Pendingintent mpermissionintent;Private Usbmanager Musbmanager;Private Usbdeviceconnection musbdeviceconnection;PrivateFinal Broadcastreceiver Musbdevicereceiver =New Broadcastreceiver () {PublicvoidOnReceive (context context, Intent Intent) {String action = intent.getaction ();if (Action_usb_permission.equals (ACTION)) {Synchronized (This) {Usbdevice Usbdevice = Intent.getparcelableextra (Usbmanager.extra_device);if (Intent.getbooleanextra (usbmanager.extra_permission_granted,False) {musbdevice = Usbdevice;}else {Toast.maketext (context,"Permission denied for device" + Usbdevice, Toast.length_short). Show ();}}}Elseif (UsbManager.ACTION_USB_DEVICE_DETACHED.equals (ACTION)) {if (musbdevice! =NULL) {Toast.maketext (context,"Device closed", Toast.length_short). Show ();if (musbdeviceconnection! =NULL) {Musbdeviceconnection.close ();}} } } };PrivateUsbprinter () {}PublicStatic UsbprinterGetInstance () {if (minstance = =NULL) {minstance =New Usbprinter (); }return minstance; }/** * Initialize printer, need to correspond with destroy * *@param context Contexts */PublicStaticvoidInitprinter (Context context) {getinstance (). init (context);}/** * Destroy objects held by the printer */PublicStaticvoidDestroyprinter () {getinstance (). Destroy ();}PrivatevoidInit (Context context) {Mcontext = context; Musbmanager = (Usbmanager) mcontext.getsystemservice (Context.usb_service); Mpermissionintent = Pendingintent.getbroadcast (Mcontext,0,New Intent (Action_usb_permission),0); Intentfilter filter =New Intentfilter (action_usb_permission); Filter.addaction (usbmanager.action_usb_device_detached); Mcontext.registerreceiver (musbdevicereceiver, filter);List all USB devices, and all request to get USB permissions hashmap<string, usbdevice> devicelist = Musbmanager.getdevicelist ();For (Usbdevice device:deviceList.values ()) {musbmanager.requestpermission (device, mpermissionintent);}}PrivatevoidDestroy () {mcontext.unregisterreceiver (musbdevicereceiver);if (musbdeviceconnection! =NULL) {musbdeviceconnection.close (); musbdeviceconnection =Null } Mcontext =Null Musbmanager =Null }/** * Printing method *@param msg */PublicvoidPrint (String msg) {Final String printdata = msg;if (musbdevice! =NULL) {Usbinterface usbinterface = Musbdevice.getinterface (0);for (int i =0; I < Usbinterface.getendpointcount (); i++) {Final Usbendpoint EP = Usbinterface.getendpoint (i);if (ep.gettype () = = Usbconstants.usb_endpoint_xfer_bulk) {if (ep.getdirection () = = usbconstants.usb_dir_out) {musbdeviceconnection = Musbmanager.opendevice (MUsbDevice);if (musbdeviceconnection! =NULL) {Toast.maketext (Mcontext,"Device Connected", Toast.length_short). Show (); Musbdeviceconnection.claiminterface (Usbinterface,true); New Thread (new Runnable () { @Override public void Run () { byte[] bytes = Printdata.getbytes (); int b = Musbdeviceconnection.bulktransfer (EP, Bytes, Bytes.length, 100000); LOG.I ("Return Status", "b-->" + b);}). Start (); Musbdeviceconnection.releaseinterface (Usbinterface); Break ;} }}}} else {toast.maketext (Mcontext, "No available USB print device", Toast.length_short). Show (); }} 

"Go" Android Printer-no device driver SDK, self-USB printing function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.