infoxicator / react-native-star-prnt

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

Unable to Connect and Print TSP650II via Bluetooth #29

Closed sahil6111 closed 5 years ago

sahil6111 commented 5 years ago
export async function getPrinters() {
    let printers = []
    try {
        printers = await StarPRNT.portDiscovery("All");
    } catch (e) {
        console.log(e);
    }
    return printers
}
export async function connectAndPrint(prnt, imageUrl) {
    console.log("started===================>");
    try {
            console.log("select printerprnt", prnt);
            let isConnected = await connect(EmulationPrinter, prnt.portName);
            if (isConnected) {
                console.log("printerprnt is connected, start printing", isConnected);
                let status = await StarPRNT.checkStatus(prnt.portName, EmulationPrinter);
                console.log("status", status);
                if (!status.offline) {
                    console.log("printer online, starting printing");
                    await print(
                        EmulationPrinter,//StarLine
                        prnt.portName,
                        getCommands(imageUrl));
                } else {
                     console.log("Printer is offline");
                }
            } else {
                console.log("printerprnt is not connected", isConnected);
            }
        console.log("stop========================>");
    } catch (e) {
        console.log(e);
    }
}

Printer is discovered using above getPrinters function. But when i try to connect and print it always show printer not connected. Any help is appreciated. Thanks.

sahil6111 commented 5 years ago

Now, i have changed my function to print directly without connecting because the printer is already paired & connected to iPad.

export async function directPrint(prnt, imageUrl) {
    console.log("started===================>");
    try {
        console.log("select printerprnt", prnt);
        await print(
            EmulationPrinter,
            prnt.portName,
            getCommands(imageUrl));
    } catch (e) {
        console.log(e);
    }
}

After this change, a small stub of receipt will feed through the printer, as if to print something but nothing prints. I am trying to print image uri which is captured by 'react-native-view-shot'. @infoxicator @CHOMNANP Need help or any guidance that can help to print. Thanks!

seigel commented 5 years ago

You may wish to check the get status documentation which states:

Checking status of a printer while connected 
to that printer can produce unexpected results.

With some basic code I can connect and print. Also note that I believe for both of your cases you will need to pair the printer with the iPad.

dnlowman commented 5 years ago

It appears that the iOS native module does not support parsing a URI with a file scheme which is why you're experiencing nothing being printed as the image data cannot be resolved.

NSURL *imageURL = [NSURL URLWithString:urlString]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage *image = [UIImage imageWithData:imageData];

https://github.com/infoxicator/react-native-star-prnt/blob/master/ios/RNStarPrnt.m#L537

I think fileURLWithPath will have to be used in order to parse a file based URL.

I've created a PR here attempting to fix this: https://github.com/infoxicator/react-native-star-prnt/pull/31

CHOMNANP commented 5 years ago

@sahil6111 , can you try running this on your device? https://gitlab.com/CHOMNANP/react-native-star-print-example/tree/print

I think can follow the example here if it work.

CHOMNANP commented 5 years ago

If I'm not wrong, we convert image to based64 and paste the base64 format for IOS.

sahil6111 commented 5 years ago

I changed the logic, didn't use the imageUrl, instead i used the append text.