如何在playstation浏览器中使用usb,蓝牙或手柄api读取手柄值?



我尝试将游戏手柄与ps浏览器连接,但浏览器给了我一个错误,那个导航器。蓝牙和导航。u盘未定义

这是更容易连接手柄与chrome浏览器和读取值之后。我想把Chrome浏览器作为程序运行在我网站的playstation浏览器上。

有没有什么简单的方法可以把游戏手柄和ps浏览器连接起来?

const gamepadName = document.getElementById('gamepad');
const xAxes = document.getElementById('xAxes');
const yAxes = document.getElementById('yAxes');
const xButton = document.getElementById('xButton');
const yButton = document.getElementById('yButton');
const aButton = document.getElementById('aButton');
const bButton = document.getElementById('bButton');
const bluetoothBtn = document.getElementById('bluetoothDevices');
const usbBtn = document.getElementById('usbDevices');
//connect with bluetooth
bluetoothBtn.addEventListener('click', async function onClick() {
  try {
    const options = {
      filters: [{
        services: [0x1800, 0x180F, 0x181C]
      }]
    };
    const device = await navigator.bluetooth.requestDevice(options);
    console.log(device);
  } catch (error) {
    console.log("Something went wrong. " + error);
  }
});
//Connect with usb
usbBtn.addEventListener('click', async function onClick() {
  const device = await navigator.usb.requestDevice({
    filters: [{
      vendorId: 0x054c,
      productId: 0x05c4
    }]
  });
  console.log(device);
});

//Get Gamepad Values
const threashold = 0.2;
const updateGamePad = (gamepad) => {
  gamepadName.innerHTML = gamepad.id;
  xAxes.innerHTML = gamepad.axes[0] >= threashold || gamepad.axes[0] <= -threashold ? gamepad.axes[0] : 0;
  yAxes.innerHTML = gamepad.axes[1] >= threashold || gamepad.axes[1] <= -threashold ? gamepad.axes[1] : 0;
  aButton.innerHTML = gamepad.buttons[0].pressed ? true : false;
  bButton.innerHTML = gamepad.buttons[1].pressed ? true : false;
  xButton.innerHTML = gamepad.buttons[2].pressed ? true : false;
  yButton.innerHTML = gamepad.buttons[3].pressed ? true : false;
  if (gamepad.axes[0].value < 0.5) {
    var mouse = Mouse.current;
    mouse.x += 150;
    alert(mouse)
  }
}
const draw = () => {
  const gamepads = navigator.getGamepads();
  for (let i = 0; i < gamepads.length; i++) {
    if (gamepads[i] && gamepads[i].connected) {
      updateGamePad(gamepads[i]);
    }
  }
  window.requestAnimationFrame(draw);
}
window.addEventListener('gamepadconnected', event => {
  window.requestAnimationFrame(draw);
});
<div class="list">
  <div class="sublist">
    <div>Gamepad:</div>
    <div id="gamepad"></div>
  </div>
  <div class="sublist">
    <div>X-axis:</div>
    <div id="xAxes"></div>
  </div>
  <div class="sublist">
    <div>Y-axis:</div>
    <div id="yAxes"></div>
  </div>
  <div class="sublist">
    <div>Button X:</div>
    <div id="xButton"></div>
  </div>
  <div class="sublist">
    <div>Button Y:</div>
    <div id="yButton"></div>
  </div>
  <div class="sublist">
    <div>Button B:</div>
    <div id="bButton"></div>
  </div>
  <div class="sublist">
    <div>Button A:</div>
    <div id="aButton"></div>
  </div>
  <div class="sublist">
    <button id="bluetoothDevices">Get bluetooth devices</button>
  </div>
  <div class="sublist">
    <button id="usbDevices">Get usb devices</button>
  </div>

这里有两件事:首先,你可以使用Gamepad API访问标准游戏控制器(包括PlayStation控制器,但不包括它们的一些特殊功能)。其次,底层设备api,如WebHID、Web蓝牙、等。,理论上可以用来访问控制器不支持的功能,但实际上PlayStation浏览器不支持。

相关内容

  • 没有找到相关文章

最新更新