taro-bluetooth-print 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Your Name
3
+ Copyright (c) 2025 Agions
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,25 +2,89 @@
2
2
 
3
3
  一个基于 Taro.js 的跨平台蓝牙打印库,支持微信小程序、H5、React Native 和鸿蒙等多平台,支持 ESC/POS 指令,可用于连接蓝牙打印机并发送打印数据。
4
4
 
5
- ## 特性
6
-
7
- - 支持多平台:微信小程序、H5、React Native、鸿蒙OS
8
- - 支持 ESC/POS 打印指令
9
- - 提供简洁易用的 API
10
- - 支持打印文本、图片、条形码、二维码、收据等
11
- - 支持文本格式化(对齐、加粗、下划线等)
12
- - 支持打印模板系统,便于重用
13
- - TypeScript 支持
14
-
15
- ## 安装
5
+ <p align="center">
6
+ <img src="https://img.shields.io/npm/v/taro-bluetooth-print" alt="npm version">
7
+ <img src="https://img.shields.io/npm/l/taro-bluetooth-print" alt="license">
8
+ <img src="https://img.shields.io/npm/dt/taro-bluetooth-print" alt="downloads">
9
+ </p>
10
+
11
+ ## 📑 目录
12
+
13
+ - [特性](#特性)
14
+ - [安装](#安装)
15
+ - [快速开始](#快速开始)
16
+ - [详细使用示例](#详细使用示例)
17
+ - [基本使用](#基本使用)
18
+ - [打印文本](#打印文本)
19
+ - [打印图片](#打印图片)
20
+ - [打印收据](#打印收据)
21
+ - [打印条形码](#打印条形码)
22
+ - [打印二维码](#打印二维码)
23
+ - [使用打印模板](#使用打印模板)
24
+ - [配置项](#配置项)
25
+ - [平台支持](#平台支持)
26
+ - [常见问题](#常见问题)
27
+ - [实际应用场景](#实际应用场景)
28
+ - [API 文档](#api-文档)
29
+ - [贡献指南](#贡献指南)
30
+ - [许可证](#许可证)
31
+
32
+ ## ✨ 特性
33
+
34
+ - 🌐 **多平台支持**:微信小程序、H5、React Native、鸿蒙OS
35
+ - 🖨️ **完整的 ESC/POS 支持**:兼容大多数热敏打印机
36
+ - 🔌 **简洁的 API**:易于使用的接口设计
37
+ - 📄 **多样化打印内容**:文本、图片、条形码、二维码、收据等
38
+ - 🎨 **文本格式化**:支持对齐、加粗、下划线等多种格式
39
+ - 📋 **模板系统**:内置打印模板,方便重用
40
+ - 📱 **TypeScript 支持**:完整的类型定义
41
+
42
+ ## 📦 安装
16
43
 
17
44
  ```bash
45
+ # 使用 npm
18
46
  npm install taro-bluetooth-print --save
19
- # 或
47
+
48
+ # 使用 yarn
20
49
  yarn add taro-bluetooth-print
50
+
51
+ # 使用 pnpm
52
+ pnpm add taro-bluetooth-print
53
+ ```
54
+
55
+ ## 🚀 快速开始
56
+
57
+ 只需几行代码,即可实现打印功能:
58
+
59
+ ```typescript
60
+ import Taro from '@tarojs/taro';
61
+ import TaroBluePrint from 'taro-bluetooth-print';
62
+
63
+ // 初始化打印库
64
+ const printer = new TaroBluePrint();
65
+
66
+ // 连接打印机并打印
67
+ async function printDemo() {
68
+ // 初始化蓝牙
69
+ await printer.bluetooth.init();
70
+
71
+ // 连接到打印机(示例ID,实际使用时需要先扫描获取)
72
+ const connected = await printer.bluetooth.connect('你的打印机ID');
73
+
74
+ if (connected) {
75
+ // 打印文本
76
+ await printer.printer.printText('Hello, Taro Print!');
77
+
78
+ // 切纸并结束
79
+ await printer.printer.cut();
80
+
81
+ // 断开连接
82
+ await printer.bluetooth.disconnect();
83
+ }
84
+ }
21
85
  ```
22
86
 
23
- ## 使用示例
87
+ ## 📝 详细使用示例
24
88
 
25
89
  ### 基本使用
26
90
 
@@ -94,22 +158,41 @@ scanAndConnect();
94
158
  ### 打印文本
95
159
 
96
160
  ```typescript
97
- await printer.printer.printText('Hello, World!', {
98
- align: 'center',
99
- bold: true,
100
- doubleHeight: true
161
+ // 基本文本打印
162
+ await printer.printer.printText('Hello, World!');
163
+
164
+ // 带格式的文本打印
165
+ await printer.printer.printText('居中加粗大字体', {
166
+ align: 'center', // 对齐方式: 'left' | 'center' | 'right'
167
+ bold: true, // 是否加粗
168
+ doubleHeight: true, // 是否倍高
169
+ doubleWidth: false, // 是否倍宽
170
+ underline: false, // 是否添加下划线
171
+ fontType: 'A' // 字体类型: 'A' | 'B' | 'C'
101
172
  });
173
+
174
+ // 打印多行文本
175
+ await printer.printer.printText([
176
+ '第一行文本',
177
+ { text: '第二行加粗文本', bold: true },
178
+ { text: '第三行右对齐文本', align: 'right' }
179
+ ]);
102
180
  ```
103
181
 
104
182
  ### 打印图片
105
183
 
106
184
  ```typescript
185
+ // 打印网络图片
107
186
  await printer.printer.printImage('https://example.com/logo.png', {
108
- maxWidth: 300,
109
- dithering: true
187
+ maxWidth: 300, // 最大宽度(像素)
188
+ dithering: true // 是否启用抖动算法(提高黑白图片质量)
110
189
  });
111
190
 
112
- // 或打印Base64图片
191
+ // 打印本地图片(小程序)
192
+ const tempFilePath = 'wxfile://temp-file-path';
193
+ await printer.printer.printImage(tempFilePath);
194
+
195
+ // 打印Base64图片
113
196
  await printer.printer.printImage('data:image/png;base64,...', {
114
197
  maxWidth: 300
115
198
  });
@@ -118,8 +201,9 @@ await printer.printer.printImage('data:image/png;base64,...', {
118
201
  ### 打印收据
119
202
 
120
203
  ```typescript
204
+ // 打印简单收据
121
205
  await printer.printer.printReceipt({
122
- title: '收据',
206
+ title: '消费小票',
123
207
  merchant: '示例商店',
124
208
  items: [
125
209
  { name: '商品1', price: 10.5, quantity: 2 },
@@ -130,23 +214,62 @@ await printer.printer.printReceipt({
130
214
  footer: '感谢您的惠顾,欢迎再次光临!',
131
215
  logo: 'https://example.com/logo.png' // 可选
132
216
  });
217
+
218
+ // 打印带有更多信息的收据
219
+ await printer.printer.printReceipt({
220
+ title: '消费小票',
221
+ merchant: '示例商店',
222
+ address: '北京市朝阳区xx路xx号',
223
+ phone: '010-12345678',
224
+ orderNo: 'ORD12345678',
225
+ items: [
226
+ { name: '商品1', price: 10.5, quantity: 2 },
227
+ { name: '商品2', price: 5.0, quantity: 1 },
228
+ { name: '商品3(八折优惠)', price: 20.0, quantity: 1, discount: 0.8 }
229
+ ],
230
+ subtotal: 41.0,
231
+ discount: 4.0,
232
+ tax: 3.7,
233
+ total: 40.7,
234
+ payment: {
235
+ method: '微信支付',
236
+ amount: 40.7
237
+ },
238
+ date: '2023-09-28 15:30:45',
239
+ operator: '收银员: 张三',
240
+ footer: '感谢您的惠顾,欢迎再次光临!\n请保留小票作为退换凭证',
241
+ qrcode: 'https://example.com/receipt/12345',
242
+ logo: 'https://example.com/logo.png'
243
+ });
133
244
  ```
134
245
 
135
246
  ### 打印条形码
136
247
 
137
248
  ```typescript
249
+ // 打印简单条形码
250
+ await printer.printer.printBarcode('123456789');
251
+
252
+ // 打印带有配置的条形码
138
253
  await printer.printer.printBarcode('123456789', {
139
- height: 80,
140
- align: 'center'
254
+ height: 80, // 高度,默认为 80
255
+ width: 2, // 宽度,默认为 2
256
+ position: 'below', // 文本位置: 'none' | 'above' | 'below' | 'both'
257
+ align: 'center', // 对齐方式: 'left' | 'center' | 'right'
258
+ type: 'EAN13' // 条码类型: 'UPC-A' | 'UPC-E' | 'EAN13' | 'EAN8' | 'CODE39' | 'ITF' | 'CODABAR' | 'CODE93' | 'CODE128'
141
259
  });
142
260
  ```
143
261
 
144
262
  ### 打印二维码
145
263
 
146
264
  ```typescript
265
+ // 打印简单二维码
266
+ await printer.printer.printQRCode('https://example.com');
267
+
268
+ // 打印带有配置的二维码
147
269
  await printer.printer.printQRCode('https://example.com', {
148
- size: 8,
149
- align: 'center'
270
+ size: 8, // 尺寸因子 (1-16),默认为 8
271
+ errorCorrection: 'M', // 纠错级别: 'L'(7%) | 'M'(15%) | 'Q'(25%) | 'H'(30%)
272
+ align: 'center' // 对齐方式: 'left' | 'center' | 'right'
150
273
  });
151
274
  ```
152
275
 
@@ -173,29 +296,222 @@ const commands = await receiptTemplate.build();
173
296
 
174
297
  // 发送打印命令
175
298
  await printer.printer.sendCommands(commands);
299
+
300
+ // 或者直接使用内置模板打印
301
+ await printer.printer.printWithTemplate('receipt', {
302
+ title: '消费小票',
303
+ merchant: '示例商店',
304
+ // 其他数据...
305
+ });
306
+ ```
307
+
308
+ ## ⚙️ 配置项
309
+
310
+ ### 初始化配置
311
+
312
+ ```typescript
313
+ const printer = new TaroBluePrint({
314
+ debug: true, // 是否开启调试模式,默认 false
315
+ encoding: 'GBK', // 编码方式,支持 'GBK'、'UTF-8' 等,默认 'GBK'
316
+ characterSet: 'CHINA', // 字符集,默认 'CHINA'
317
+ beep: false, // 打印完成是否蜂鸣提示,默认 false
318
+ paperWidth: 58, // 纸张宽度(mm),默认 58mm,支持 58/80mm
319
+ autoCut: true // 打印完成是否自动切纸,默认 true
320
+ });
176
321
  ```
177
322
 
178
- ## API 文档
323
+ ### 蓝牙配置
179
324
 
180
- 请查看完整的API文档以了解更多详情:[API文档](https://github.com/yourusername/taro-bluetooth-print/blob/main/docs/API.md)
325
+ ```typescript
326
+ // 蓝牙扫描配置
327
+ await printer.bluetooth.startDiscovery({
328
+ timeout: 10000, // 扫描超时时间(毫秒),默认 10000ms
329
+ services: ['1812'], // 要搜索的服务 UUID,默认打印服务 1812
330
+ allowDuplicatesKey: false // 是否允许重复上报设备,默认 false
331
+ });
332
+ ```
181
333
 
182
- ## 平台支持
334
+ ## 📱 平台支持
183
335
 
184
336
  | 功能 | 微信小程序 | H5 | React Native | 鸿蒙OS |
185
- | --- | --- | --- | --- | --- |
186
- | 设备扫描 | | ✓* | | |
187
- | 设备连接 | | ✓* | | |
188
- | 文本打印 | | | | |
189
- | 图片打印 | | | | |
190
- | 条码打印 | | | | |
191
- | 二维码打印 | | | | |
337
+ | --- | :---: | :---: | :---: | :---: |
338
+ | 设备扫描 | | ✅* | | |
339
+ | 设备连接 | | ✅* | | |
340
+ | 文本打印 | | | | |
341
+ | 图片打印 | | | | |
342
+ | 条码打印 | | | | |
343
+ | 二维码打印 | | | | |
344
+
345
+ > **注意**:H5 环境需要支持 Web Bluetooth API 的浏览器。目前 Chrome、Edge、Opera 等基于 Chromium 的浏览器支持此功能,且需要在 HTTPS 环境下使用。Safari 不支持 Web Bluetooth API。
346
+
347
+ ### 平台特殊配置
348
+
349
+ #### 微信小程序
350
+
351
+ 需要在 `app.json` 中添加蓝牙相关权限:
352
+
353
+ ```json
354
+ {
355
+ "permission": {
356
+ "scope.bluetooth": {
357
+ "desc": "请求获取蓝牙权限用于连接打印机"
358
+ }
359
+ }
360
+ }
361
+ ```
362
+
363
+ #### H5
364
+
365
+ H5 环境下,需要用户主动触发蓝牙操作(如点击按钮),不能自动调用蓝牙 API。
366
+
367
+ #### React Native
368
+
369
+ 需要安装依赖:
370
+
371
+ ```bash
372
+ npm install react-native-ble-plx --save
373
+ # 或
374
+ yarn add react-native-ble-plx
375
+ ```
376
+
377
+ 且需要在 Android 和 iOS 项目中进行相关权限配置。
378
+
379
+ ## ❓ 常见问题
380
+
381
+ ### Q: 为什么连接不到打印机?
382
+
383
+ A: 请检查以下几点:
384
+ 1. 确保打印机已开启并处于可发现模式
385
+ 2. 确保蓝牙已启用且权限已授予
386
+ 3. 检查设备与打印机之间的距离
387
+ 4. 尝试重启打印机
388
+ 5. 有些打印机可能需要配对,请先在系统设置中配对
389
+
390
+ ### Q: 为什么图片打印质量不佳?
391
+
392
+ A: 图片打印质量受多种因素影响:
393
+ 1. 启用 `dithering: true` 选项可提高黑白图片质量
394
+ 2. 调整 `maxWidth` 为打印机支持的最佳宽度(一般为 384 像素或更小)
395
+ 3. 优先使用简单、清晰的图片,避免复杂的渐变和细节
396
+
397
+ ### Q: 打印中文出现乱码怎么办?
398
+
399
+ A: 中文乱码通常是编码问题:
400
+ 1. 确保初始化时使用正确的编码,对于中文通常使用 'GBK':
401
+ ```typescript
402
+ const printer = new TaroBluePrint({ encoding: 'GBK' });
403
+ ```
404
+ 2. 确保设置了正确的字符集:
405
+ ```typescript
406
+ await printer.printer.setCharacterSet('CHINA');
407
+ ```
408
+
409
+ ### Q: 在H5环境下无法使用怎么办?
410
+
411
+ A:
412
+ 1. 确保使用支持 Web Bluetooth API 的浏览器(Chrome、Edge、Opera等)
413
+ 2. 确保在 HTTPS 环境下运行
414
+ 3. 蓝牙操作必须由用户交互触发(如点击按钮)
415
+ 4. 在某些操作系统(如macOS上的某些浏览器)可能存在限制
416
+
417
+ ## 🔍 实际应用场景
418
+
419
+ ### 零售小票打印
420
+
421
+ 适用于商店、超市、餐厅等需要打印购物小票、订单小票的场景。
422
+
423
+ ```typescript
424
+ // 打印小票示例
425
+ await printer.printer.printReceipt({
426
+ title: '消费小票',
427
+ merchant: '好又多超市',
428
+ address: '北京市海淀区中关村大街1号',
429
+ phone: '010-12345678',
430
+ orderNo: 'ORD20230928001',
431
+ items: [
432
+ { name: '牛奶 250ml', price: 4.5, quantity: 2 },
433
+ { name: '面包', price: 8.0, quantity: 1 },
434
+ { name: '水果', price: 15.8, quantity: 1 }
435
+ ],
436
+ subtotal: 32.8,
437
+ discount: 3.0,
438
+ total: 29.8,
439
+ payment: { method: '微信支付', amount: 29.8 },
440
+ date: new Date().toLocaleString(),
441
+ footer: '感谢您的惠顾,欢迎再次光临!'
442
+ });
443
+ ```
444
+
445
+ ### 物流快递标签
446
+
447
+ 适用于物流公司、电商平台等需要打印快递标签的场景。
448
+
449
+ ```typescript
450
+ // 打印快递标签
451
+ await printer.printer.printText('顺丰速运', { align: 'center', bold: true, doubleHeight: true });
452
+ await printer.printer.printLine();
453
+ await printer.printer.printText('收件人: 张三');
454
+ await printer.printer.printText('电话: 138****1234');
455
+ await printer.printer.printText('地址: 北京市朝阳区xxx路xxx号');
456
+ await printer.printer.printLine();
457
+ await printer.printer.printBarcode('SF1234567890', { height: 80, position: 'below' });
458
+ await printer.printer.printQRCode('SF1234567890');
459
+ await printer.printer.cut();
460
+ ```
461
+
462
+ ### 会员卡及优惠券
463
+
464
+ 适用于商场、酒店、健身房等需要打印会员卡、优惠券的场景。
192
465
 
193
- *注意:H5 环境需要支持 Web Bluetooth API 的浏览器。目前 Chrome、Edge、Opera 等基于 Chromium 的浏览器支持此功能,且需要在 HTTPS 环境下使用。
466
+ ```typescript
467
+ // 打印会员卡
468
+ await printer.printer.printText('VIP会员卡', { align: 'center', bold: true, doubleHeight: true });
469
+ await printer.printer.printImage('https://example.com/vip-logo.png', { maxWidth: 300 });
470
+ await printer.printer.printText('会员姓名: 李四', { align: 'center' });
471
+ await printer.printer.printText('会员等级: 金卡会员', { align: 'center' });
472
+ await printer.printer.printText('有效期至: 2024-12-31', { align: 'center' });
473
+ await printer.printer.printQRCode('https://example.com/member/12345');
474
+ await printer.printer.printText('扫码查看会员权益', { align: 'center' });
475
+ await printer.printer.cut();
476
+ ```
477
+
478
+ ## 📚 API 文档
479
+
480
+ 完整的 API 文档请参考:[API文档](docs/API.md)
481
+
482
+ ## 🤝 贡献指南
483
+
484
+ 非常欢迎您为 taro-bluetooth-print 项目贡献代码!以下是贡献的步骤:
485
+
486
+ 1. Fork 本仓库
487
+ 2. 创建您的特性分支 (`git checkout -b feature/amazing-feature`)
488
+ 3. 提交您的修改 (`git commit -m 'Add some amazing feature'`)
489
+ 4. 推送到分支 (`git push origin feature/amazing-feature`)
490
+ 5. 打开一个 Pull Request
491
+
492
+ ### 开发环境设置
493
+
494
+ ```bash
495
+ # 克隆仓库
496
+ git clone https://github.com/Agions/taro-bluetooth-print.git
497
+
498
+ # 安装依赖
499
+ cd taro-bluetooth-print
500
+ npm install
501
+
502
+ # 运行构建
503
+ npm run build
504
+ ```
194
505
 
195
- ## 许可证
506
+ ### 贡献类型
196
507
 
197
- MIT
508
+ 您可以通过多种方式贡献:
509
+ - 修复 bug
510
+ - 添加新特性
511
+ - 改进文档
512
+ - 优化性能
513
+ - 添加测试用例
198
514
 
199
- ## 贡献
515
+ ## 📄 许可证
200
516
 
201
- 欢迎提交 Issue PR!
517
+ 本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件
@@ -1,4 +1,8 @@
1
- import { BluetoothDevice } from './adapter';
1
+ import { BluetoothDevice, ConnectOptions, DiscoveryOptions } from '../types';
2
+ /**
3
+ * 蓝牙管理器类
4
+ * 负责蓝牙设备的扫描、连接和通信
5
+ */
2
6
  export declare class BluetoothManager {
3
7
  private adapter;
4
8
  private isInitialized;
@@ -7,6 +11,9 @@ export declare class BluetoothManager {
7
11
  private discoveredDevices;
8
12
  private serviceUUID;
9
13
  private characteristicUUID;
14
+ /**
15
+ * 创建蓝牙管理器实例
16
+ */
10
17
  constructor();
11
18
  /**
12
19
  * 初始化蓝牙模块
@@ -14,8 +21,9 @@ export declare class BluetoothManager {
14
21
  init(): Promise<boolean>;
15
22
  /**
16
23
  * 开始搜索蓝牙设备
24
+ * @param options 搜索选项
17
25
  */
18
- startDiscovery(): Promise<boolean>;
26
+ startDiscovery(options?: DiscoveryOptions): Promise<boolean>;
19
27
  /**
20
28
  * 停止搜索蓝牙设备
21
29
  */
@@ -26,9 +34,10 @@ export declare class BluetoothManager {
26
34
  getDiscoveredDevices(): Promise<BluetoothDevice[]>;
27
35
  /**
28
36
  * 连接蓝牙设备
29
- * @param deviceId 蓝牙设备ID
37
+ * @param deviceId 设备ID
38
+ * @param options 连接选项
30
39
  */
31
- connect(deviceId: string): Promise<boolean>;
40
+ connect(deviceId: string, options?: ConnectOptions): Promise<boolean>;
32
41
  /**
33
42
  * 断开蓝牙连接
34
43
  */
@@ -38,6 +47,24 @@ export declare class BluetoothManager {
38
47
  * @param data 要写入的数据
39
48
  */
40
49
  writeData(data: ArrayBuffer): Promise<boolean>;
50
+ /**
51
+ * 检查是否已连接
52
+ */
53
+ isConnected(): boolean;
54
+ /**
55
+ * 获取已连接的设备ID
56
+ */
57
+ getConnectedDeviceId(): string | null;
58
+ /**
59
+ * 获取适配器初始化状态
60
+ */
61
+ getInitStatus(): boolean;
62
+ /**
63
+ * 设置服务和特征值UUID
64
+ * @param serviceUUID 服务UUID
65
+ * @param characteristicUUID 特征值UUID
66
+ */
67
+ setServiceUUIDs(serviceUUID: string, characteristicUUID: string): void;
41
68
  /**
42
69
  * 销毁蓝牙模块
43
70
  */