Printing example of Android Bluetooth connected ESC / POS thermal printer (Bluetooth connection)

A mobile CRM project of the company recently needs to add the function of small ticket printing, which is the kind of small ticket we often see when we order takeout. This mainly involves two pieces of knowledge:

Needless to say, Bluetooth connection is too common. This article mainly introduces this part. But what the hell is ESC / POS print instruction? In short, our common thermal ticket printers support such an instruction. As long as the instruction is sent to the printer according to the instruction format, even printers of different models and brands will perform the same action. For example, there are corresponding instructions for printing a line of text, line feed, bold, etc. this part will be introduced in the next article.

This article is mainly based on official documents. Compared with official documents, it omits a large section of instructions and is easier to get started quickly.

1. Bluetooth permissions

To use the Bluetooth function, first declare the Bluetooth permission in the androidmanifest configuration file:

The Bluetooth permission only allows the establishment of Bluetooth connection and data transmission. However, if you want to perform operations such as Bluetooth device discovery, you also need to apply for Bluetooth_ Admin permissions.

2. Initial configuration

A class Bluetooth adapter is mainly used here. The usage is very simple. Look directly at the code:

In singleton mode, there is only one global instance. As long as it is null, it means that the device does not support Bluetooth, so corresponding processing is required.

If the device supports Bluetooth, then check whether Bluetooth is on:

If Bluetooth is not turned on, after executing startactivityforresult(), a dialog box will pop up asking whether to turn on Bluetooth. Click Yes and Bluetooth will be turned on automatically. After Bluetooth is successfully opened, it will call back to onactivityresult().

In addition to actively turning on Bluetooth, you can also monitor Bluetooth adapter.action_ STATE_ Changed broadcast, including extra_ State and extra_ PREVIoUS_ State has two extra fields. Possible values include state_ TURNING_ ON,STATE_ ON,STATE_ TURNING_ OFF,and STATE_ OFF。 The meaning is very clear, no explanation.

3. Discovery of equipment

After initialization, Bluetooth is turned on. The next step is to scan nearby devices. Just one sentence:

However, this is only the beginning of device discovery. This must be an asynchronous process. We need to register a broadcast, listen to the broadcast of the discovery device, and directly enter the code:

The notes have been written clearly, except Bluetooth device.extra_ In addition to device, there is an extra field bluetoothdevice.extra_ Class, you can get a Bluetooth class object, which is mainly used to save some additional description information of the device, such as whether it is an audio device.

There are two points to note about equipment discovery:

Startdiscovery() can only scan devices whose status is set to discoverable. Android devices are not discoverable by default. To change the device to discoverable status, you need to do the following:

After execution, a dialog window will pop up asking whether the device is allowed to be set to the discoverable state. After clicking yes, the device will be set to the discoverable state.

Startdiscovery () is a very resource consuming operation, so it is necessary to call canceldiscovery () in time to release resources. For example, you must call canceldiscovery() before connecting devices

4. Equipment pairing and connection

4.1 pairing

When connecting to a device for the first time, a prompt box will pop up on the screen asking whether pairing is allowed. The connection can be established only after pairing is successful.

The system will save the information of all successfully paired devices. Therefore, before executing startdiscovery(), you can try to find the paired device first. Because this is a local information reading process, it is much faster than startdiscovery(), and avoid occupying too many resources. If the device is within the coverage of Bluetooth signal, it can directly initiate the connection.

The code to find the paired device is as follows:

The code is very simple without explanation. It is to call bluetoothadapter. Getbondeddevices() to get a set < bluetoothdevice > and traverse to get the paired device information.

4.2 connection

The connection model of Bluetooth device is very similar to that of network connection. Both are client server mode, and data transmission is carried out through a socket. Then, as an Android device, there are three situations:

Because it is to pave the way for the next introduction to connecting thermal printer printing, let's first talk about the connection of Android device as client. Because the printer cannot actively establish a connection with Android devices, the printer must be connected as a server.

4.2.1 connect as a client

No more nonsense, code:

Device. Createrfcommsockettoservicerecord (my_uuid) a UUID needs to be passed in here. This UUID needs special attention. Simply understood, it is a string in the agreed format, which is used to uniquely identify a Bluetooth service.

When the client initiates a connection, the UUID passed in must be the same as that set on the server side! Otherwise, an error will be reported!

What if you are connected to a thermal printer and don't know what the UUID set on the server side is? Don't worry, because some common Bluetooth service protocols already have agreed UUIDs. For example, we connect the thermal printer based on spp serial communication protocol, and its corresponding UUID is "00001101-0000-1000-8000-00805f9b34fb", so the actual call is as follows:

UUIDs of other common Bluetooth services can be searched by yourself. If it is only used for communication between their own applications, in theory, you can define any UUID, as long as the UUIDs used on both sides of the server and client are the same.

4.2.2 connect as server

Let's look at the code:

5. Data transmission

Finally, after the first four steps, everything is ready, only due to the east wind. The last part is actually the simplest, because it simply uses InputStream and OutputStream to send and receive data.

Example code:

This part will also be used in the next article when operating a thermal printer through a mobile phone, so I won't talk about it 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
分享
二维码
< <上一篇
下一篇>>