docs/examples/hid/hid.js
async function enableHID() {
  const usbConfig = getstrprop('sys.usb.state');
  if (!usbConfig.startsWith('hid,')) {
    log.warn('HID function does not seem to be enabled.');
    const usbConfigWithHid = `hid,${usbConfig}`;
    log.info(`changing USB config to ${usbConfigWithHid}`);

    // WARNING!  If this device is not Kenzo or this otherwise fails, it's likely
    // that adb access will be lost until the next reboot
    setprop('sys.usb.config', usbConfigWithHid);

    // Wait for USB to be reconfigured.
    await sleep(1000);

    const newUsbConfig = getstrprop('sys.usb.state');
    if (newUsbConfig !== usbConfigWithHid) {
      throw new Error(`USB config expected to be ${usbConfigWithHid}, but it was ${newUsbConfig}`);
    }
  }

  // Ensure the two HID devices are present
  Promise.all(
    ['/dev/hidg0', '/dev/hidg1'].map(
      file => fs.access(file, fs.constants.R_OK | fs.constants.W_OK)
    )
  );
}
async function enableHID() {
  const usbConfig = getstrprop('sys.usb.state');
  if (!usbConfig.startsWith('hid,')) {
    log.warn('HID function does not seem to be enabled.');
    const usbConfigWithHid = `hid,${usbConfig}`;
    log.info(`changing USB config to ${usbConfigWithHid}`);

    // WARNING!  If this device is not Kenzo or this otherwise fails, it's likely
    // that adb access will be lost until the next reboot
    setprop('sys.usb.config', usbConfigWithHid);

    // Wait for USB to be reconfigured.
    await sleep(1000);

    const newUsbConfig = getstrprop('sys.usb.state');
    if (newUsbConfig !== usbConfigWithHid) {
      throw new Error(`USB config expected to be ${usbConfigWithHid}, but it was ${newUsbConfig}`);
    }
  }

  // Ensure the two HID devices are present
  Promise.all(
    ['/dev/hidg0', '/dev/hidg1'].map(
      file => fs.access(file, fs.constants.R_OK | fs.constants.W_OK)
    )
  );
}
async function enableHID ( ) { const usbConfig = getstrprop ( 'sys.usb.state' ) ; if ( ! usbConfig . startsWith ( 'hid,' ) ) { log . warn ( 'HID function does not seem to be enabled.' ) ; const usbConfigWithHid = ` ${ usbConfig } ` ; log . info ( ` ${ usbConfigWithHid } ` ) ; // WARNING!  If this device is not Kenzo or this otherwise fails, it's likely // that adb access will be lost until the next reboot setprop ( 'sys.usb.config' , usbConfigWithHid ) ; // Wait for USB to be reconfigured. await sleep ( 1000 ) ; const newUsbConfig = getstrprop ( 'sys.usb.state' ) ; if ( newUsbConfig !== usbConfigWithHid ) { throw new Error ( ` ${ usbConfigWithHid } ${ newUsbConfig } ` ) ; } } // Ensure the two HID devices are present Promise . all ( [ '/dev/hidg0' , '/dev/hidg1' ] . map ( file => fs . access ( file , fs . constants . R_OK | fs . constants . W_OK ) ) ) ; }
Ensures the HID function in the USB Android gadget driver is active
Ensures the HID function in the USB Android gadget driver is active