infoxicator / react-native-star-prnt

React-Native bridge to communicate with Star Micronics Bluetooth/LAN Printers
MIT License
67 stars 65 forks source link

Connection with printer with bluetooth is very unstable #49

Open bwaiter-official opened 4 years ago

bwaiter-official commented 4 years ago

Hi,

I'm setting up a star TSP 100III with RN 0.61 through Bluetooth. Every time I start to print the connection is really slow and most of the times I end up with this error:

image

Other times, I get this error: image

Is there anyone with such experience in the past? How did you solve or can you suggest any workaround here?

I'll leave my code below so you can see what I'm doing. I'm trying to follow what there is on the documentation but I might missed something there.

import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, ActivityIndicator, Button } from 'react-native';
import {StarPRNT} from 'react-native-star-prnt';
const EMULATION = 'StarGraphic';
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      printer: {},
      isLoading: false,
      isConnected: false
    };
  }

  portDiscovery = async () => {
    this.setState({isLoading: true})
    try {
      const printers = await StarPRNT.portDiscovery('All');
      printers.forEach(p =>{
        if (p.macAddress === "00:11:62:17:11:CB"){
          this.connect(p)
          return
        }
      })
    } catch (e) {
      console.error(e);
    }
  }

  connect = async(printer)=> {
    let isConnect = false;
    try {
      isConnect = await StarPRNT.connect(printer.portName, EMULATION, true);
      console.log('connect success',isConnect); // Printer Connected!
      this.setState({
        isLoading: false,
        printer
      })
    } catch (e) {
      console.log('connection error',e);
    }
  }

  handlePrint = async () => {
    try {
      let status = await StarPRNT.checkStatus(this.state.printer.portName,EMULATION);
      console.log("status",status);
      if(!status.offline){
        console.log("starting...");

        let commands = [];
        commands.push({append:
                "Star Clothing Boutique\n" +
                "123 Star Road\n" +
                "City, State 12345\n" +
                "\n"});
        commands.push({appendCutPaper:StarPRNT.CutPaperAction.PartialCutWithFeed});
        var rslt = await StarPRNT.print(EMULATION, commands, this.state.printer.portName);
        console.log(rslt); // Success!
      }
    } catch (e) {
      console.log(1,e);
    }
  };

  render() {
    console.log(this.state.isLoading)
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Connected: {this.state.printer.modelName}</Text>
        {this.state.isLoading ? (<ActivityIndicator animating={true}/>) : <Button onPress={this.portDiscovery} title="Press here" />}
        {!this.state.isLoading && <Button onPress={this.handlePrint} title="Print" />}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});
jahanzaibhw commented 4 years ago

Hey did you get it resolved? I'm having the same issue.

jahanzaibhw commented 4 years ago

I was doing a mistake that I was calling the connect function in the componentDidMount method which is not required if you are using SMPort to print data. So in short you don't need to connect every time before calling print function simply print with the portName you have discovered by portDiscovery function. Hope it helps anybody :)