Skip to content

Unable to detect printer using webusb api #1

@kundanscorpio

Description

@kundanscorpio

I am able to read data from bar code scanner using webusb api following the steps mentioned on url:
https://developers.google.com/web/updates/2016/03/access-usb-devices-on-the-web
However when I try to integrate a receipt printer (Model Epson: UB-U09) on same usb port using this file

File: printer.html

<textarea id="printContent"></textarea>
  • Needs chrome://flags/#enable-experimental-web-platform-features - Experimental Web Platform Features
  • Needs chrome://flags/#enable-webusb - Web USB
<script> var device;
function setup(device) {
	return device.open()
	.then(() => device.selectConfiguration(1))
	.then(() => device.claimInterface(0))
}

function print() {
	var string = document.getElementById("printContent").value + "\n";
	var encoder = new TextEncoder();
	var data = encoder.encode(string);
	device.transferOut(1, data)
	.catch(error => { console.log(error); })
}

function connectAndPrint() {
	if (device == null) {
	alert('device11');
		
		navigator.usb.requestDevice({ filters: [{ vendorId: 1208 }] })			
		.then(selectedDevice => {
			device = selectedDevice;
			console.log(device);
			return setup(device);
		})
		.then(() => print())
		.catch(error => { console.log(error); })
	}
	else
		print();
}


navigator.usb.getDevices()
.then(devices => {
	if (devices.length > 0) {
		device = devices[0];
		return setup(device);
	}
})
.catch(error => { console.log(error); });

</script>

It opens with a pop-up saying no device-found. I am using following version of chrome on windows 10:
Version 60.0.3112.113 (Official Build) (64-bit)
I tried detecting the same device on windows 8 machine and I see following javascript error on console:
Uncaught TypeError: Cannot read property 'requestDevice' of undefined
at connectAndPrint (printer.html:30)
at HTMLInputElement.onclick (printer.html:4)
I have tried using both running the same code on http server as well as https but with no luck. The same port with same file printer.html detects my iPhone and shows as paired but does not detect the printer.
When I click on Print I get a pop-up saying no devices found. Any help appreciated.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions