2

I'm new to react-native, I'm making app which can connect to printer with bluetooth I'm using react-native-ble-plx . I had successfully connect to my printer with bluetooth but I don't know to to print some thing from it.

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

when user click on print button it should print some thing.

chirag prajapati
  • 579
  • 6
  • 22

0 Answers0