反应本机打印机

时间:2019-09-08 06:47:06

标签: react-native react-native-android react-native-ble-plx

我是本机新手,我正在制作可以使用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>
    );
  }
}

当用户单击“打印”按钮时,它应该打印一些东西。

0 个答案:

没有答案