Simple implementation of POS printer call under Android

This paper is based on gp58 series, which is compatible with ESC / POS instruction set and is universal to Epson printers

Device debugging under Android. If the device provides a driver, it can be debugged according to the manufacturer's driver; The device does not provide a driver and can only be debugged according to the general method. The USB interface is used to control the printer output.

1. Get the USB manager first

A delay intention is used to receive the broadcast during USB access. When the broadcast is received, it indicates that there is a new device access.

Add a boardcast action

Get the reference of the USB device, and the Android system will ask you whether to allow the device to access. The default is false; When access is allowed, it will judge whether the USB reference is null. If it is not empty, setdevice will be called to create a connection, otherwise the connection will be closed.

In setdevice, we can obtain the function set (usbinterface) and communication channel (usbindpoint) of the device. At the same time, we also create a connection between host and device to transmit data.

2. Create a new usbadmin in the relevant class and call openusb. First, go to the setdevice() method above to obtain the device reference. When the connection channel is established, list all USB devices. When the device reference does not exist, list all USB devices and request to obtain USB permission.

3. After the above two are completed, we can send instructions to control the connected printer. Here we use the standard ESC / POS instruction set, which is the hardware default and pasted with code. The instruction set here adopts decimal representation or can be replaced with hexadecimal.

4. After the above is completed, you can convert the string you need into a byte array and call the sendcommand method to print

This step only adds a synchronization lock and does not open a new thread to handle it. There is no problem on the machine, but the article on USB communication mechanism mentioned that it should be put into asynchronous threads, which should be noted here.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>