Reactjs 使用esc蓝牙打印机插件进行本机反应';找不到插件函数
大家好,提前感谢你们抽出时间。事实是,这是我第一次用react native编写代码,我的任务是通过蓝牙连接打印机并使其打印。我对react有基本的了解,并获得了一些react native的基本知识,但我遇到的这个问题更多的是关于依赖项/包等,我不熟悉。我的问题是,当我在expo上运行我的应用程序时,基本上没有获得插件组件函数(“我得到了这个错误:“TypeError:EscPos.initBluetoothConnectionListener不是一个函数。(在'EscPos.initBluetoothConnectionListener()'中,'EscPos.initBluetoothConnectionListener'是未定义的)” 我采取的步骤是: 第一:在npm上安装expo 然后:在我的文件夹中初始化expo 然后:安装npm插件包 最后:在它不起作用后,我尝试将react native与该插件链接,但没有成功 这是我的两个js文件:Reactjs 使用esc蓝牙打印机插件进行本机反应';找不到插件函数,reactjs,react-native,android-bluetooth,Reactjs,React Native,Android Bluetooth,大家好,提前感谢你们抽出时间。事实是,这是我第一次用react native编写代码,我的任务是通过蓝牙连接打印机并使其打印。我对react有基本的了解,并获得了一些react native的基本知识,但我遇到的这个问题更多的是关于依赖项/包等,我不熟悉。我的问题是,当我在expo上运行我的应用程序时,基本上没有获得插件组件函数(“我得到了这个错误:“TypeError:EscPos.initBluetoothConnectionListener不是一个函数。(在'EscPos.initBlue
import EscPos from "@leesiongchan/react-native-esc-pos";
import { Alert } from "react-native";
class BluetoothComponent extends Component {
constructor(props) {
super(props);
this.initDevice();
}
initDevice() {
EscPos.addListener("bluetoothDeviceFound", (event) => {
if (event.state === EscPos.BLUETOOTH_DEVICE_FOUND) {
Alert.alert("Device Found!");
Alert.alert("Device Name : " + event.deviceInfo.name);
Alert.alert("Device MAC Address : " + event.deviceInfo.macAddress);
}
});
EscPos.addListener("bluetoothStateChanged", (event) => {
if (event.state === EscPos.BLUETOOTH_CONNECTED) {
EscPos.stopScan();
Alert.alert("Device Connected!");
Alert.alert("Device Name : " + event.deviceInfo.name);
Alert.alert("Device MAC Address : " + event.deviceInfo.macAddress);
}
});
}
printTest() {
Alert.alert('Printing...');
this.testPrinter();
};
scanTest() {
EscPos.scanDevices();
};
async testPrinter() {
try {
// Can be `network` or `bluetooth`
EscPos.setConfig({ type: "bluetooth" });
// Connects to your printer
// If you use `bluetooth`, second parameter is not required.
await EscPos.connect("10.10.10.10");
// Once connected, you can setup your printing size, either `PRINTING_SIZE_58_MM`, `PRINTING_SIZE_76_MM` or `PRINTING_SIZE_80_MM`
EscPos.setPrintingSize(EscPos.PRINTING_SIZE_58_MM);
// 0 to 8 (0-3 = smaller, 4 = default, 5-8 = larger)
EscPos.setTextDensity(8);
// Test Print
await EscPos.printSample();
// // Cut half!
// await EscPos.cutPart();
// // You can also print image! eg. "file:///longpath/xxx.jpg"
// await EscPos.printImage(file.uri);
// // You can also print image with a specific width offset (scale down image by offset pixels)! eg. "file:///longpath/xxx.jpg"
// await EscPos.printImageWithOffset(file.uri, offset);
// Print your design!
await EscPos.printDesign(receipt);
// Print QR Code, you can specify the size
// await EscPos.printQRCode("Proxima b is the answer!", 200);
// // Print Barcode
// // printBarCode({code}, {type}, {width}, {height}, {font}, {fontPosition})
// // type: 65=UPC-A; 66=UPC-E; 67=EAN13; 68=EAN8; 69=CODE39; 70=ITF; 71=CODABAR; 72=CODE93; 73=CODE128}
// // width: 2-6
// // height: 0-255
// // font: 0=FontA; 1=FontB
// // fontPosition: 0=none; 1=top; 2=bottom; 3=top-bottom
// await EscPos.printBarcode("Your barcode here", 73, 3, 100, 0, 2);
// // Cut full!
// await EscPos.cutFull();
// // Beep!
// await EscPos.beep();
// // Kick the drawer! Can be either `kickCashDrawerPin2` or `kickCashDrawerPin5`
// await EscPos.kickCashDrawerPin2();
// Disconnect
await EscPos.disconnect();
} catch (error) {
console.error(error);
}
};
render() {
return null;
}
}
export default BluetoothComponent;
从“React”导入React,{Component};
从“/BluetoothComponent”导入BluetoothComponent;
进口{
文本,
看法
按钮
}
从“反应本族语”;
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
this.bluetooth=React.createRef();
}
打印测试(){
this.bluetooth.current.printTest();
}
扫描测试(){
this.bluetooth.current.scanTest();
}
render(){
返回(
this.printTest()}/>
this.scanTest()}/>
);
}
}
这是github的插件:
任何其他的澄清,欢迎你问我,提前谢谢大家
import React, { Component } from "react";
import BluetoothComponent from "./BluetoothComponent";
import {
Text,
View,
Button
}
from "react-native";
export default class App extends Component {
constructor(props) {
super(props);
this.bluetooth = React.createRef();
}
printTest() {
this.bluetooth.current.printTest();
}
scanTest() {
this.bluetooth.current.scanTest();
}
render() {
return (
<View style={{ padding: 50 }}>
<BluetoothComponent ref={this.bluetooth} />
<Button title="Print" onPress={() => this.printTest()} />
<Text />
<Button title="Connect To BlueTooth" onPress={() => this.scanTest()} />
</View>
);
}
}