13

I've written an UI in HTML5 and JavaScript. I chose this implementation so that I could share the same code between both Android Chrome and Windows 8 RT.

Now, my next objective is to write data to a USB HID. The device that I'm communicating with is treated as an HID. I'm mentioning this part in case there's a way to write to an HID device rather than a USB RAW device.

I've done a lot of research on the matter. But, the HTML5 specifications are changing so often that maybe I missed something.

**Anyway, is there a language or implementation that I can use to share common code to access a USB device on Android and Windows 8 RT?

If there isn't a way to share between the two, how about a suggestion to get USB communication only on Android?**


Below is my research along with conclusions:

  • Silverlight: unsupported. In terms of web plugins, it is not supported on Windows 8 RT or Android

  • Flash: Possible option. It is supported in both operating systems, although they removed support for Android with release of Android 4.1 (Jelly Bean).

  • HTML5: unsupported

  • I looked at the GamePad API, but it seems to only support reading from a gamepad.

  • I looked at Device tag, but this tag no longer exists in the current draft. I read that the "navigator" object replaces it in JavaScript, but it seems completely different. Also, Device only targets acquiring video and audio

  • JavaScript: unsupported. From my understanding, this is because it'd be a security risk.

  • Chrome Packaged App: unsupported. I am unsure about support in Windows 8 RT. But, it is not supported in Android Chrome, either.

  • A USB API exists that accomplishes exactly what I need, but the API is only available to a packaged app.

  • Java applet: unsupported. I am unsure about support in Windows 8 RT. But, it is not supported in Android Chrome, either.

  • There are many resources available regarding how to communicate with USB devices. However, since Java applets aren't supported on smartphones, this ends up being unsupported.

  • A couple of useful posts

  • How can I send and receive data to a serial port or USB port in ASP.NET?

  • Is accessing USB from a web application for cross browser cross OS possible at all?

  • Is that possible to provide a JavaScript API to control USB, LPT, and COM devices?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cheezy
  • 446
  • 1
  • 5
  • 17
  • Any JavaScript solution (HTML is a markup language, so can't really write to HID or anything else) that would allow you to write directly to a USB HID device would be a horrific security hole and almost certainly disallowed by every browser. – Adrian Mar 20 '13 at 21:51
  • Then only way you're going to get this to work is to write native code for both, unfortunately. Also, the Android side is tricky. Some vendors (HTC) break HID. – Brad Mar 20 '13 at 21:52
  • @Adrian thanks for the confirmation. It's unfortunate, but it seems like there's no way to use Javascript for device communication – cheezy Mar 21 '13 at 12:22
  • @Brad I was unaware that some vendors break HID. I'll have to keep that in mind while I'm troubleshooting (eg try the code on multiple devices in case the device's OS doesn't have a full driver stack). For other people's reference, here's an article stating that an HTC device has a minimal bluetooth stack http://community.htc.com/na/htc-forums/android/f/91/p/2336/8570.aspx – cheezy Mar 21 '13 at 12:27

5 Answers5

8

I've actually thought about how to do things similar to this...

Here's is one way sure way to do it if you have control over the computer which has the USB device attached:

Have the computer with the USB device run a web server such as Apache and PHP. Have it only listen to localhost.

Then in the HTML page being viewed, execute an Ajax post to localhost/somescript.php (or CGI or cfm or whatever).

In the PHP/CGI script, communicate to the USB device and then return a JSON string to the browser saying something happened.

Another approach is to use custom URL protocols. You create an executable and "install" it on the client computer along with your custom URL protocol.

Then you can invoke the executable from the browser using your custom URL protocol.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian McGinity
  • 5,777
  • 5
  • 36
  • 46
  • Thanks for the suggestion! This had crossed my mind, but I was really hoping that a standalone method existed. Now I have to make the decision whether to use native code or Ajax and PHP – cheezy Mar 21 '13 at 12:32
  • It is a sure way to overcome the browser barrier. Be careful because the local computer is now running a web server on localhost and it a knowing hacker could exploit it. You could run the localhost web server on a crazy port and pass certain keys in the url, so the localhost would only react when the correct key is given – Brian McGinity Apr 12 '13 at 15:33
  • I was considering using cgi/perl to drive the usb device. PHP does have functions to communicate with usb connected devices. This is a new para-dime to web programming :) – Brian McGinity Apr 12 '13 at 15:37
  • Another approach is to use a custom url protocol. See update above. – Brian McGinity Apr 28 '14 at 14:55
2

If the USB device you want to control is a barcode printer, you can use jZebra.

This is an applet which directly communicates to locally connected printers.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian McGinity
  • 5,777
  • 5
  • 36
  • 46
2

There is one more alternative Web Socket.Create a web socket server which will access the usb device in local system.

Connect your web server using Web Socket from Web Application.Web Socket has standard Api

You can do something like this

  var host = "wss://localhost:25000/test";

  Websokcet ws = new WebSocket(host);

You can create your web sokcet Server using RFC 6455

for older browser you can think of http server as well.

Community
  • 1
  • 1
Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
2

Please have a look at JSFS. It works similar to Chris_vr's approach and is already a working solution.

https://github.com/jsfsproject/jsfs. It's free and licensed under GPL.

wimix
  • 574
  • 1
  • 5
  • 16
1

Writing to a serial port:

var writeSerial = function(str) {
  chrome.serial.write(connectionId, str2ab(str), onWrite);
}

// Convert string to ArrayBuffer
var str2ab = function(str) {
  var buf = new ArrayBuffer(str.length);
  var bufView = new Uint8Array(buf);
  for (var i=0; i<str.length; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}

More is at http://developer.chrome.com/apps/app_hardware.html.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe Oliver
  • 36
  • 3