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