Traditional Culture Encyclopedia - Traditional stories - uniapp implements Bluetooth receipt printing function
uniapp implements Bluetooth receipt printing function
A recent project added the function of Bluetooth printing of receipts. Since I didn’t know much about Bluetooth printers before, I encountered many pitfalls. I spent some time encapsulating the Bluetooth connection and printing modules into common components and wrote a printing function.
Example, make a note here in case you forget.
Component: What the component example project needs to implement is to connect the App to a Bluetooth printer. The printed content includes: 1. Company logo (local picture) 2. Text data in the specified format and layout (linear, Chinese and English) 3. Signature picture data (
The network image address returned by the interface) First of all, if you want to be able to print, Bluetooth must be connected. Before development, understand the low-power Bluetooth connection operation process: Follow the API to go through the process from connection to printing: Here I will connect Bluetooth and
The printing process is encapsulated in the component. In the parent component, you only need to splice the instructions in the onPrint method, and pass the spliced ??instruction data into the component through the bufferData attribute to perform the printing operation. onPrintSuccess and onPrintFail are callbacks for successful and failed printing respectively.
: The internal implementation of the component is as follows: 1. Click the print button, open the Bluetooth adapter (openBluetoothAdapter), and obtain the connected devices (getConnectedBluetoothDevices). If there are no connected devices, open the search device pop-up box to search for devices (step 2)
, if the device is connected, execute the printing method (step 4). The blesdk here is for ease of use. It puts the uniapp Bluetooth-related APIs into one file and converts the method to asynchronous, which also includes adding CPCL instructions.
Character splicing method.
gbk is a module used to transcode data into a data format acceptable to the printer 2. Open the search device pop-up box (isShowSearch controls the pop-up box to show and hide), click to start the search (startBluetoothDevicesDiscovery), and monitor the new devices found (onfindBlueDevices)
, Bluetooth search operation consumes system resources, so it is recommended to close the search (stopBlueDevicesDiscovery) when the device is connected and the page is destroyed. Here, I added two buttons to control the search switch due to the addition of filter conditions (rssi and device name, device
ID) Therefore, it is necessary to filter the device list monitored by onfindBlueDevices 3. Click on the device list to select the connection device ① Each item in the device list can obtain the name, deviceId and other information of the device. What we need when connecting is the deviceId. Create
Bluetooth connection (createBLEConnection), before that, you can monitor the change of connection status through onBLEConnectionStateChange ② After the connection is successful, turn off the search device switch.
After the connection is successful, you need to obtain device services (getBLEDeviceServices) through deviceId. When obtaining here, you need to set a delay for the method, otherwise the obtained serviceId will be empty. 3. After successfully obtaining the device service, the servicesId array will be returned. Then we need to use deviceId and
serviceId to obtain the characteristic value (getDeviceCharacteristics) ④ After obtaining the characteristic value, you need to find a variable to temporarily store the characteristic value, because the characteristic value is needed when sending data to the printer later.
Close the search pop-up box. 4. After connecting the device, click the print button, and then you can start splicing and printing data.
In step 1, we have made a judgment that if there is a connected device, start splicing data and printing. Here, the splicing task is handed over to the parent page (onPrint). After the splicing is completed, it is passed in through bufferData. After the bufferData data is updated, the data starts to be
Sent to the device, the required parameters are the parameters in opt. deviceId is the device id; serviceId is the service id; characteristicId is the characteristic code; value is the written data, which needs to be converted into GBK format; lastSuccess is the callback when all data is sent successfully.
;onceLength is the length of each data packet sent in subpackage. Because Android and iOS are different, a judgment is added.
sendDataToDevice encapsulates the method of packet sending.
- Previous article:How to distinguish the printing methods of T-shirts?
- Next article:Why is Go not popular in other countries except China, Japan and Korea?
- Related articles
- Characteristics of Kunqu Opera "Pentecostal"
- Rabbit lanterns lotus lanterns a set of four stamps
- Huawei cell phone desktop icon corner markers set
- Tan Dun's Profile (Singer Profile of Aranon)
- Why is the blockchain tamper-proof?
- There are several development zones in Beijing. Where are they and what kind of enterprises do they have?
- Qing Dynasty, a magistrate afraid of his wife, drunkenly said a vulgar couplet, show how to do right?
- QQ dazzle dance should be how to P?
- Measures of Lanzhou University for the Administration of Employment System Personnel
- As a travel agency, how to arrange a complete tour route?