Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth headphones button event detection in javascript

I am building a web app where I detect the headphones button event. I succeeded in capturing headphones button event when they are plugged in. Now I am trying to capture Bluetooth headphones next button event. Any help on this please?

Code for headphone button detection.

 document.addEventListener('volumeupbutton', () => {
   //Do something here
 }, false);

I need something similar to this.

like image 690
Shaharyar Kirmani Avatar asked Nov 25 '18 11:11

Shaharyar Kirmani


People also ask

How do I discover Bluetooth devices with JavaScript?

As a security feature, discovering Bluetooth devices with navigator.bluetooth.requestDevice must be triggered by a user gesture such as a touch or a mouse click. We're talking about listening to pointerup, click, and touchend events. The Web Bluetooth API relies heavily on JavaScript Promises.

How to fix “windows can’t detect Bluetooth headphones” issue?

So to fix “Windows can’t detect bluetooth headphones” issues, you can try to re-pair your bluetooth headphones with your PC. Here’s how to do it: 1) If your bluetooth headphones are on, turn them off. 2) Press and hold the power button for a long time until the indicator tells you’re entering pairing mode.

How does Bluetooth device user prompt work?

It supports communication among devices that implement Bluetooth 4.0 or later. When a website requests access to nearby devices using navigator.bluetooth.requestDevice, the browser prompts user with a device chooser where they can pick one device or simply cancel the request. Bluetooth device user prompt.

What is the Web Bluetooth API?

The Web Bluetooth API allows websites to communicate with Bluetooth devices. What if I told you websites could communicate with nearby Bluetooth devices in a secure and privacy-preserving way?


1 Answers

You can use keydown and keyup events for implementing the long press functionality.

// Imprementation of Long Press

const longPressTime = 1500;
let keyDownTimeout;
document.addEventListener('keydown', e => {
  if (keyDownTimeout) {
    return;
  }
  keyDownTimeout = setTimeout(() => {
    // button was held for 1500ms, consider it a long-press
    if (e.code === 'ArrowUp') {
      console.log("Action Performed");
      // do long-press action
    } else {
      console.log("Other action performed");
    }
  }, longPressTime);
});

document.addEventListener('keyup', e => {
  clearTimeout(keyDownTimeout);
  keyDownTimeout = 0;
});
Press any key

The above methods work for single key long press. Refer to KeyCode for key code. Demo of above

like image 113
Akansh Avatar answered Oct 30 '22 18:10

Akansh