React native 反应本机打印机
我是react native的新手,我正在制作一个应用程序,它可以通过蓝牙连接到打印机,我正在使用React native 反应本机打印机,react-native,react-native-android,react-native-ble-plx,React Native,React Native Android,React Native Ble Plx,我是react native的新手,我正在制作一个应用程序,它可以通过蓝牙连接到打印机,我正在使用react native ble plx。我已经成功地用蓝牙连接到打印机,但我不知道如何从中打印一些东西 import React, {PureComponent} from 'react'; import {View, Text, Button} from 'react-native'; import {BleManager} from 'react-native-ble-plx'; expor
react native ble plx
。我已经成功地用蓝牙连接到打印机,但我不知道如何从中打印一些东西
import React, {PureComponent} from 'react';
import {View, Text, Button} from 'react-native';
import {BleManager} from 'react-native-ble-plx';
export default class App extends PureComponent {
constructor(props) {
super(props);
this.state = {};
this.manager = new BleManager();
}
componentDidMount() {
const subscription = this.manager.onStateChange(state => {
if (state === 'PoweredOn') {
console.log(state);
this.scanAndConnect();
}
}, true);
}
scanAndConnect() {
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
// Handle error (scanning will be stopped automatically)
console.log(error);
}
// if (device) {
// console.log(device);
// }
if (device.name === 'POSTEK061F') {
// Stop scanning as it's not necessary if you are scanning for one device.
this.manager.stopDeviceScan();
device
.connect()
.then(device => {
return device.discoverAllServicesAndCharacteristics();
})
.then(device => {
// Do work on device with services and characteristics
console.log(device.name);
console.log(device);
})
.catch(error => {
// Handle errors
});
// Proceed with connection.
}
});
}
async printHTML() {
//print something
}
render() {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Button onPress={this.printHTML} title="Print HTML" />
</View>
);
}
}
import React,{PureComponent}来自'React';
从“react native”导入{视图、文本、按钮};
从'react native ble plx'导入{BleManager};
导出默认类应用程序扩展PureComponent{
建造师(道具){
超级(道具);
this.state={};
this.manager=新的BleManager();
}
componentDidMount(){
const subscription=this.manager.onStateChange(state=>{
如果(状态=='PoweredOn'){
console.log(状态);
这个.scanAndConnect();
}
},对);
}
scanAndConnect(){
this.manager.startDeviceScan(null,null,(错误,设备)=>{
如果(错误){
//处理错误(扫描将自动停止)
console.log(错误);
}
//如果(设备){
//控制台日志(设备);
// }
如果(device.name=='POSTEK061F'){
//停止扫描,因为如果要扫描一个设备,则不需要进行扫描。
这是.manager.stopDeviceScan();
装置
.connect()
。然后(设备=>{
返回设备。发现服务和特性();
})
。然后(设备=>{
//在具有服务和特性的设备上工作
console.log(设备名);
控制台日志(设备);
})
.catch(错误=>{
//处理错误
});
//继续连接。
}
});
}
异步打印HTML(){
//打印一些东西
}
render(){
返回(
);
}
}
当用户点击打印按钮,它应该打印一些东西