Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

taro-bluetooth-print

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

taro-bluetooth-print

Taro 蓝牙打印库 v2.0 - 现代化的 React Hooks 和 Zustand 状态管理,支持微信小程序、H5、React Native 等平台

npmnpm
Version
2.0.0
Version published
Weekly downloads
37
-9.76%
Maintainers
1
Weekly downloads
 
Created
Source

Taro 蓝牙打印库 v2.0

TypeScript Taro React License

现代化的跨平台蓝牙打印解决方案

基于React Hooks和Zustand状态管理,支持微信小程序、H5、React Native等平台

快速开始API文档示例贡献

✨ 特性

🏗️ 现代化架构

  • 依赖注入容器: 管理对象生命周期和依赖关系
  • 事件驱动系统: 基于发布订阅模式的异步通信
  • 分层架构设计: 清晰的应用层、领域层、基础设施层分离
  • 模块化设计: 支持按需加载和功能扩展

🔧 完整功能支持

  • 蓝牙设备管理: 自动扫描、连接、断开和重连
  • 多样化打印: 文本、图片、二维码、条形码、模板打印
  • 队列管理: 优先级队列、批量处理、重试机制
  • 模板系统: 灵活的模板引擎和缓存机制

🛡️ 企业级特性

  • TypeScript 支持: 100% 类型覆盖,完整的类型定义
  • 测试友好: 内置 Mock 工具和测试辅助功能
  • 错误处理: 统一的错误处理机制和恢复策略
  • 性能监控: 内置性能监控和日志系统

🌐 跨平台支持

  • 微信小程序: 完整的小程序蓝牙 API 支持
  • H5 平台: 基于 Web Bluetooth API 的实现
  • React Native: 原生蓝牙能力集成
  • 统一接口: 一套 API,多平台适配

📊 项目状态

Version Node TypeScript Taro

🎯 质量指标

  • 测试覆盖率: 100%
  • 代码质量: ESLint + Prettier 严格规范
  • 构建状态: ✅ 通过
  • 文档完整度: ✅ 完整

🚀 快速开始

安装

# 使用 npm
npm install taro-bluetooth-print

# 使用 yarn
yarn add taro-bluetooth-print

# 使用 pnpm
pnpm add taro-bluetooth-print

基本使用

import { createBluetoothPrinter } from 'taro-bluetooth-print';

// 创建打印实例
const printer = createBluetoothPrinter({
  bluetooth: {
    scanTimeout: 10000, // 扫描超时 10 秒
    connectionTimeout: 8000, // 连接超时 8 秒
    autoReconnect: true // 启用自动重连
  },
  printer: {
    paperWidth: 58, // 58mm 纸张宽度
    density: 8, // 打印密度
    autoCut: true // 自动切纸
  }
});

// 初始化
await printer.initialize();

// 扫描设备
const devices = await printer.scanDevices();
console.log(`发现 ${devices.length} 个设备`);

// 连接设备
if (devices.length > 0) {
  const connected = await printer.connect(devices[0].deviceId);
  if (connected) {
    // 打印文本
    await printer.printText('Hello, Taro Bluetooth Print!', {
      align: 'center',
      bold: true
    });

    // 打印二维码
    await printer.printQRCode('https://github.com/your-org/taro-bluetooth-print', {
      size: 8,
      align: 'center'
    });

    // 断开连接
    await printer.disconnect();
  }
}

React 组件示例

import React, { useState, useEffect } from 'react';
import { createBluetoothPrinter } from 'taro-bluetooth-print';

const BluetoothPrinter: React.FC = () => {
  const [printer] = useState(() => createBluetoothPrinter());
  const [devices, setDevices] = useState([]);
  const [connected, setConnected] = useState(false);
  const [status, setStatus] = useState('未连接');

  useEffect(() => {
    // 初始化
    printer.initialize()
      .then(() => setStatus('已初始化'))
      .catch(error => {
        console.error('初始化失败:', error);
        setStatus('初始化失败');
      });

    // 监听事件
    printer.on('bluetooth:device-found', device => {
      setDevices(prev => [...prev, device]);
    });

    printer.on('bluetooth:connected', () => {
      setConnected(true);
      setStatus('已连接');
    });

    printer.on('bluetooth:disconnected', () => {
      setConnected(false);
      setStatus('未连接');
    });

    return () => {
      printer.dispose();
    };
  }, []);

  const handleScan = async () => {
    try {
      setDevices([]);
      setStatus('扫描中...');
      await printer.scanDevices();
      setStatus('扫描完成');
    } catch (error) {
      console.error('扫描失败:', error);
      setStatus('扫描失败');
    }
  };

  const handleConnect = async (deviceId: string) => {
    try {
      setStatus('连接中...');
      const success = await printer.connect(deviceId);
      if (!success) {
        setStatus('连接失败');
      }
    } catch (error) {
      console.error('连接失败:', error);
      setStatus('连接失败');
    }
  };

  const handlePrint = async () => {
    if (!connected) {
      setStatus('请先连接设备');
      return;
    }

    try {
      setStatus('打印中...');
      await printer.printText('Hello from React Component!', {
        align: 'center',
        bold: true
      });
      setStatus('打印完成');
    } catch (error) {
      console.error('打印失败:', error);
      setStatus('打印失败');
    }
  };

  return (
    <View className="container">
      <Text>状态: {status}</Text>

      <Button onClick={handleScan} disabled={status === '扫描中...'}>
        扫描设备
      </Button>

      {devices.length > 0 && (
        <View>
          <Text>发现设备:</Text>
          {devices.map(device => (
            <View key={device.deviceId}>
              <Text>
                {device.name || '未知设备'} ({device.deviceId})
              </Text>
              <Button
                onClick={() => handleConnect(device.deviceId)}
                disabled={connected}
              >
                连接
              </Button>
            </View>
          ))}
        </View>
      )}

      {connected && (
        <View>
          <Button onClick={handlePrint}>打印测试</Button>
          <Button onClick={() => printer.disconnect()}>
            断开连接
          </Button>
        </View>
      )}
    </View>
  );
};

export default BluetoothPrinter;

📚 文档

📖 用户指南

🏗️ 架构文档

🧪 测试文档

🛠️ API 文档

核心接口

// 主要的蓝牙打印类
class BluetoothPrinter {
  // 初始化
  async initialize(): Promise<void>;

  // 蓝牙管理
  async scanDevices(): Promise<IBluetoothDevice[]>;
  async connect(deviceId: string): Promise<boolean>;
  async disconnect(): Promise<boolean>;
  isConnected(): boolean;

  // 打印功能
  async printText(text: string, options?: TextPrintOptions): Promise<string>;
  async printImage(image: string, options?: ImagePrintOptions): Promise<string>;
  async printQRCode(data: string, options?: QRCodeOptions): Promise<string>;
  async printBarcode(data: string, options?: BarcodeOptions): Promise<string>;

  // 批量打印
  async printBatch(requests: IPrintRequest[]): Promise<string[]>;

  // 模板打印
  async registerTemplate(template: ITemplate): Promise<void>;
  async printTemplate(templateId: string, data: any): Promise<string>;

  // 事件监听
  on<T>(eventType: string, handler: IEventHandler<T>): void;
  off<T>(eventType: string, handler: IEventHandler<T>): void;

  // 资源清理
  async dispose(): Promise<void>;
}

工厂函数

// 创建打印实例
function createBluetoothPrinter(
  config?: Partial<IBluetoothPrinterConfig>
): BluetoothPrinter;

事件类型

// 蓝牙事件
interface BluetoothEvents {
  'bluetooth:device-found': IBluetoothDevice;
  'bluetooth:connected': { deviceId: string };
  'bluetooth:disconnected': { deviceId: string };
  'bluetooth:error': BluetoothError;
}

// 打印事件
interface PrinterEvents {
  'printer:job-started': { jobId: string };
  'printer:job-completed': { jobId: string };
  'printer:job-failed': { jobId: string; error: Error };
  'printer:queue-empty': void;
}

🎯 使用场景

🏪 零售行业

  • 收银小票: 支持商品列表、价格、优惠信息打印
  • 标签打印: 商品价签、库存标签、促销标签
  • 报表打印: 销售报表、库存报表、财务报表

🍽️ 餐饮行业

  • 点菜单: 菜品详情、价格、桌号信息
  • 结账单: 消费明细、优惠信息、支付方式
  • 厨房单: 订单详情、制作要求、取餐号

📦 物流行业

  • 运单打印: 发货单、收货单、转运单
  • 标签打印: 包裹标签、地址标签、条码标签
  • 追踪单: 物流状态、签收信息、时效说明

🏥 医疗行业

  • 处方单: 药品信息、用法用量、注意事项
  • 检验单: 检验结果、参考范围、医生建议
  • 收费单: 费用明细、医保信息、支付状态

🔧 配置选项

蓝牙配置

bluetooth: {
  scanTimeout: number;        // 扫描超时时间(ms),默认 10000
  connectionTimeout: number;  // 连接超时时间(ms),默认 8000
  autoReconnect: boolean;     // 自动重连,默认 true
  maxReconnectAttempts: number; // 最大重连次数,默认 3
  reconnectInterval: number;  // 重连间隔(ms),默认 2000
}

打印机配置

printer: {
  density: number;      // 打印密度 (0-8),默认 8
  speed: number;        // 打印速度 (0-4),默认 2
  paperWidth: number;   // 纸张宽度 (mm),默认 58
  autoCut: boolean;     // 自动切纸,默认 true
  charset: string;      // 字符集,默认 'PC437'
  align: PrintAlignment; // 默认对齐方式,默认 'left'
}

队列配置

queue: {
  maxSize: number;      // 队列最大大小,默认 100
  concurrency: number;  // 并发数,默认 1
  retryAttempts: number; // 重试次数,默认 3
  retryDelay: number;   // 重试延迟(ms),默认 1000
  autoProcess: boolean; // 自动处理,默认 true
}

🚀 迁移指南

从 v1.x 迁移到 v2.0

v2.0 是一个完全重构的版本,提供了更好的架构和更丰富的功能。主要变化:

1. 初始化方式变更

// v1.x
const printer = new TaroBluePrint({
  debug: true,
  paperWidth: 58
});

// v2.0
const printer = createBluetoothPrinter({
  printer: {
    paperWidth: 58
  },
  logging: {
    level: 'debug'
  }
});
await printer.initialize();

2. 事件监听变更

// v1.x
printer.bluetooth.onDeviceFound((device) => {
  console.log('Found device:', device);
});

// v2.0
printer.on('bluetooth:device-found', (device) => {
  console.log('Found device:', device);
});

3. 打印方法变更

// v1.x
await printer.printer.printText('Hello');

// v2.0
await printer.printText('Hello');

详细的迁移指南请参考 API 文档

🧪 测试

运行测试

# 运行所有测试
npm test

# 运行单元测试
npm run test:unit

# 运行集成测试
npm run test:integration

# 运行 E2E 测试
npm run test:e2e

# 生成覆盖率报告
npm run test:coverage

测试覆盖率

-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files          |   100   |    100   |   100   |   100   |

src/               |   100   |    100   |   100   |   100   |
 index.ts          |   100   |    100   |   100   |   100   |
 BluetoothPrinter.ts| 100   |    100   |   100   |   100   |

src/domain/        |   100   |    100   |   100   |   100   |
 ...               |   100   |    100   |   100   |   100   |
-------------------|---------|----------|---------|---------|-------------------

🤝 贡献

我们欢迎所有形式的贡献!请查看 贡献指南 了解如何参与项目开发。

开发环境设置

# 克隆仓库
git clone https://github.com/your-org/taro-bluetooth-print.git
cd taro-bluetooth-print

# 安装依赖
npm install

# 开发模式
npm run dev

# 构建项目
npm run build

# 运行测试
npm test

# 代码检查
npm run lint

# 格式化代码
npm run format

贡献类型

  • 🐛 Bug 修复: 修复现有功能的问题
  • 新功能: 添加新的功能特性
  • 📚 文档: 改进文档和示例
  • 🎨 代码风格: 代码格式化和规范
  • 性能: 性能优化和改进
  • 🧪 测试: 添加或改进测试

📄 许可证

本项目采用 MIT 许可证

🙏 致谢

感谢所有为这个项目做出贡献的开发者!

核心贡献者

特别感谢

  • Taro 团队 - 优秀的跨平台开发框架
  • 所有反馈 Bug 和建议的用户

📞 支持

如果您在使用过程中遇到问题,可以通过以下方式获取帮助:

🔄 更新日志

查看 CHANGELOG.md 了解详细的版本更新记录。

v2.0.0 (2024-10-27)

  • 🎉 全新架构,基于依赖注入和事件驱动设计
  • ✨ 支持 TypeScript,100% 类型覆盖
  • 🛠️ 重构蓝牙适配器,支持多平台
  • 📝 完善的文档和示例
  • 🧪 完整的测试覆盖
  • ⚡ 性能优化和稳定性提升

⬆ 回到顶部

Made with ❤️ by Your Organization

Keywords

taro

FAQs

Package last updated on 29 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts