When should one use WebHID as opposed to WebUSB? - javascript

I have a proprietary USB device that has a flashing functionality over USB. I would like to replicate this flashing functionality from within the browser, but I am not certain what API to use.
Visiting chrome://usb-internals/ to inspect my device gives me the following information:
The device advertises itself with class code 8: mass storage. The device does not show up in my file system, e.g. it is not a normal USB pendrive. According to this StackOverflow answer, WebUSB is blocked from accessing mass storage devices due to security reasons, and I should use WebHID instead.
Using WebHID, however, still did not allow me to connect to my device. This is the sample code I used:
const filter = [
{
vendorId: 0xabcd, // correct VID:PID obtained via lsusb
productId: 0x1234
}
];
const [device] = await navigator.hid.requestDevice({ filter });
Furthermore, visiting chrome://device-log/ makes a distinct difference between USB and HID devices. When I plug in a mouse, for example, the Chrome device log shows that a USB HID device connected. When I plug in a USB pendrive, I get two lines in the debug log: one HID device, one mass storage device. When I plug in my proprietary drive, I get a single line: USB Mass Storage device.
How could I convince WebHID to make a connection to my proprietary mass storage device?

Not possible without major changes:
USB mss storage uses bulk endpoints, HID is transferred via Control and Interrupt end points.
You can implement both Mass storage and HID on a single USB device (with an IAD), but the bootloader code for HID would look significantly different from the mass storage one.
USB pendrives don't usually support HID unless there is an LED or button present.

You could install a WinUSB driver for the device (Zadig makes it very easy) and then connect to the device using WebUSB.
In case you need to write your own SCSI layer, here's how we did it for a blood glucose meter that also mounts as a mass storage device to transfer data.

Related

Web Bluetooth - Auto Connect to paired device

Is there a way to auto connect to a paired device with the web Bluetooth ?
So i can get rid of the annoying pop up device selector from google..
As of this moment, I have implemented navigator.bluetooth.getDevices() behind a flag in Chrome. To use the API, the Experimental Web Platform features flag in chrome://flags needs to be enabled. Please follow Issue 577953: bluetooth: Get permitted devices for updates on getDevices().
For reconnecting to a device, I'm also implementing the watchAdvertisements() API to allow apps to receive an Event when system perceives an advertisement packet from device on which this was called on. Please follow Issue 654897: bluetooth: Implement watchAdvertisements() for updates on this API.
These two are part of a bigger project for implementing persistent permissions for Web Bluetooth to allow sites to reconnect to devices that they already had permission to connect to via the device chooser prompt.

Sync multiple devices without internet React Native

I have connect multiple android devices to the same WiFi network but internet connection is not working. I want to sync data between all devices. Is there anyway to do the data synchronization on local network without internet ? in short I want to sync data between devices using local network.
Sure! It's not trivial though.
If you're looking to do this without using a "static" server or database or similar on the local network,
you'll need a way for the devices to be able to discover each other (the easiest is manually, i.e. by entering an IP address)
listen to network requests – on one device if you're looking for a star/server-client topology, or on all of them if you're looking for a mesh where all devices sync with all of them
have the devices talk to each other to sync data as required.

Remembering the device and reconnecting to it

I'm currently doing a project with the web bluetooth in js and I wonder if there is a way to save the device object and automatically connecting to it instead of choosing the device in the pop up every time.
I've tried saving the device in the local storage and tried to get it in the cookies but they both failed.
There is a way, but it implies enabling some Chrome feature flags on a Chrome 85+
You can have a look at the documentation.
To sum it up, after enabling those two feature-flags in chrome://flags :
#enable-experimental-web-platform-features
#web-bluetooth-new-permissions-backend
The Web Bluetooth spec will be updated with the algorithm for getDevices(). The devices returned by getDevices() may contain devices that are not currently in range and connected. The BluetoothDevice::watchAdvertisements() API can be used to detect when Bluetooth devices come into range of the Bluetooth radio. Then calling BluetoothRemoteGATTServer.connect() should resolve successfully if the device is able to be connected to.

Chrome WebUSB not recognizing devices (CAC cards, smart cards)

I have made a script to load all of the USB devices connected to Chrome using chrome.usb.getDevices. So far, it has listed a second-generation iPod touch as well as a mouse, keyboard, and two unknown items from Intel. However, it has not recognized any USB flash drives, and it does not recognize any Smart Cards that I have. I've also installed Chrome's Smart Card Connector app into Chrome, but I can still not get Chrome to recognize these Smart Cards. I can't continue to use Java nor ActiveX to for smartcard interactions in the browser. How can I get CAC/smartcard authentication through the browser without having to install a slew of other dependencies?
Edit: I also installed WinUSB as their drivers in place of their default HID drivers, but the results remained.
This Chrome USB Devices states that not all USB devices are supported and can be read:
Caveats
Not all devices can be accessed through the USB API. In general,
devices are not accessible because either the Operating System's
kernel or a native driver holds them off from user space code. Some
examples are devices with HID profiles on OSX systems, and USB pen
drives.
On most Linux systems, USB devices are mapped with read-only
permissions by default. To open a device through this API, your user
will need to have write access to it too. A simple solution is to set
a udev rule. Create a file /etc/udev/rules.d/50-yourdevicename.rules
with the following content:
SUBSYSTEM=="usb", ATTR{idVendor}=="[yourdevicevendor]", MODE="0664", GROUP="plugdev"

How to initiate a new device pairing using the Chrome web bluetooth API?

Using navigator.bluetooth.requestDevice(), I can access bluetooth devices I've already set up a pair with using native OSX bluetooth pairing, but no previously-unpaired devices appear, even when their attributes match my generic query. Available devices show up in a Chrome modal requesting user consent to pair, but the only device that shows up is the one I've already paired with.
Am I misunderstanding the intended use case here, or is there another way to establish a connection with a nearby (previously unpaired) device from Chrome?
Docs: https://webbluetoothcg.github.io/web-bluetooth/
(See Example 2)
function bluetoothConnect() {
navigator.bluetooth.requestDevice({filters: [{services: ['generic_access']}]})
.then(device => {console.log(`Connected to: ${device.name}`)})
.catch(console.error);
}
First, Mac OS X is not yet fully implemented as we speak. Only discovery and GATT server connect/disconnect are working for now. See the Chrome Implementation status at https://github.com/WebBluetoothCG/web-bluetooth/blob/gh-pages/implementation-status.md. Check out Chrome OS, Linux and Android M (Android Lollipop workaround).
Regarding your specific issue, I believe that generic_access is not broadcasted by a nearby BLE device but is found because you've already paired (cached) this device. If your device is named "foo" for instance, you can go to https://googlechrome.github.io/samples/web-bluetooth/device-info.html and fill "foo" as the Device Name and hit "Get Bluetooth Device Info" button.
I would recommend you give a try to all Web Bluetooth samples at https://googlechrome.github.io/samples/web-bluetooth/index.html as well.
To complement the other answer, please be aware of the Chrome's chrome://bluetooth-internals tab/tool. This tool can not only list available devices and their services (which I don't think you can do with the Web Bluetooth API unless you requested the services in optionalServices or filters/services) and the characteristics of those services.
In the device list, there is a Forget button which becomes active once you connect to the devices GATT server using the Inspect button. This should allow you to full unpair and pair again in cases where you need to test the flow end to end.
Edit: Actually after doing some experimentation around this, the device still remains paired even after using the Forget button. Probably a bug in Chrome, however, the cache of the paired devices seems to be per-profile. This means the real answer is to:
Use the Incognito mode and in there the pairing flow will initiate from the get-go for each new Incognito session.

Categories

Resources