Expo环境下导入react-native-esc-pos-printer报getConstants为空错误求助
问题解决与替代方案
错误原因分析
TypeError: Cannot read property 'getConstants' of null 本质是react-native-esc-pos-printer的原生模块未正确初始化,大概率是因为该库未适配Expo预构建流程,导致原生代码未被正确链接到项目中。
推荐替代包及示例
1. react-native-bluetooth-escpos-printer(兼容Expo)
这是成熟的蓝牙ESC/POS打印库,支持Expo项目(需执行预构建),覆盖常用打印需求。
安装步骤:
npm install react-native-bluetooth-escpos-printer npx expo prebuild --clean npx expo run:android
基础使用示例:
import React, { useEffect } from 'react'; import { View, Text, Button } from 'react-native'; import BluetoothEscposPrinter from 'react-native-bluetooth-escpos-printer'; const PrintScreen = () => { // 搜索蓝牙打印机设备 const searchPrinters = async () => { try { const isEnabled = await BluetoothEscposPrinter.checkBluetoothEnabled(); if (!isEnabled) { await BluetoothEscposPrinter.enableBluetooth(); } const devices = await BluetoothEscposPrinter.discoverDevices(); console.log('搜索到的打印机:', devices); } catch (err) { console.error('搜索设备失败:', err); } }; // 连接设备并打印测试内容 const printTest = async (deviceAddress) => { try { const connected = await BluetoothEscposPrinter.connect(deviceAddress); if (connected) { await BluetoothEscposPrinter.printText('测试打印内容\n'); await BluetoothEscposPrinter.printText('第二行内容\n'); await BluetoothEscposPrinter.cutPaper(); await BluetoothEscposPrinter.disconnect(); } } catch (err) { console.error('打印失败:', err); } }; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', gap: 16 }}> <Text>蓝牙打印机测试</Text> <Button title="搜索打印机" onPress={searchPrinters} /> {/* 实际使用时替换为搜索到的设备MAC地址 */} <Button title="打印测试" onPress={() => printTest('XX:XX:XX:XX:XX:XX')} /> </View> ); }; export default PrintScreen;
2. expo-bluetooth + 手动ESC/POS指令(灵活自定义)
如果需要底层控制,可使用expo-bluetooth建立蓝牙连接,手动发送ESC/POS指令,适合定制化需求。
安装步骤:
npx expo install expo-bluetooth
基础使用示例:
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; import * as Bluetooth from 'expo-bluetooth'; const CustomPrintScreen = () => { const [printerId, setPrinterId] = useState(null); // 搜索配对的打印机设备 const searchDevices = async () => { const { status } = await Bluetooth.requestPermissionsAsync(); if (status !== 'granted') return; const isEnabled = await Bluetooth.isEnabledAsync(); if (!isEnabled) await Bluetooth.enableAsync(); const pairedDevices = await Bluetooth.getPairedDevicesAsync(); const targetPrinter = pairedDevices.find(dev => dev.name.includes('POS')); if (targetPrinter) setPrinterId(targetPrinter.id); }; // 发送ESC/POS指令打印 const printContent = async () => { if (!printerId) return; try { const socket = await Bluetooth.createBluetoothSocketAsync(printerId); await socket.connectAsync(); // 初始化打印机 await socket.writeAsync(Buffer.from([0x1B, 0x40])); // 打印文本 await socket.writeAsync(Buffer.from('自定义打印内容\n', 'utf8')); // 执行切纸 await socket.writeAsync(Buffer.from([0x1D, 0x56, 0x00])); await socket.disconnectAsync(); await socket.closeAsync(); } catch (err) { console.error('打印出错:', err); } }; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', gap: 16 }}> <Text>自定义蓝牙打印</Text> <Button title="搜索打印机" onPress={searchDevices} /> <Button title="打印" onPress={printContent} disabled={!printerId} /> </View> ); }; export default CustomPrintScreen;
内容的提问来源于stack exchange,提问作者mgons
相关产品推荐
相关产品推荐

