2

我是 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';

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>
    );
  }
}

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

4

0 回答 0