firefox-osb2g

The way to detect usb connection via pc


Is there anyway to detect pc usb connection (not through charger) to Firefox OS device at Gaia layer?

Any callback in Gaia layer also works for me.

The reason I do this because I need to handle some events once the device connects to pc through USB. However, so far I did not find any clue about it.


Solution

  • Certified apps probably have a better way to do this but for a privileged app one approach may be to use a change listener on the sdcard - This will only work if the USB storage option has been enabled in settings.

    var sdcard = navigator.getDeviceStorage('sdcard');
    
    sdcard.addEventListener("change", function (event) {
      var reason = event.reason;
      //if reason is "shared" you know you are connected
      //if reason is "available" you know you are not connected
    });
    

    Make sure to add the device storage permission to the manifest

    "device-storage:sdcard": {
      "access": "readwrite" //could be read only
    }