Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider devicechange event for when a new granted bluetooth device is made available, #257

Closed
beaufortfrancois opened this issue Aug 1, 2016 · 7 comments

Comments

@beaufortfrancois
Copy link
Member

In the same way, devicechange event is triggered when a new media input or output device is made available, it would be nice to add a similar event for when a new granted bluetooth device is made available.

Source: https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/wdSV0zUuvJQ

navigator.bluetooth.addEventListener('devicechange', event => {
    console.log(event.target.device.id);
    console.log(event.target.device.name);
    console.log(event.target.device.uuids;
    console.log(event.target.device.gatt.connected);
});
@jracle
Copy link

jracle commented Aug 3, 2016

@beaufortfrancois what is your use case, please clarify? I guess it is for other UA to be notified ?

When you say

hen a new granted bluetooth device is made available

do you mean device is discovered and granted though requestDevice ?

Shall we also discriminate deviceaddedfrom deviceremoved (say device is un-paired)?

Thanks.

@beaufortfrancois
Copy link
Member Author

do you mean device is discovered and granted though requestDevice ?

Yes. Instead of using BroadcastChannel, it would be nice to have this kind of info baked in the API to allow several tabs to sync their states.

I guess the deviceremoved event would also be nice.

@jracle
Copy link

jracle commented Aug 4, 2016

@jyasskin thanks for the clarification, I'd like having it implemented too then.

@jyasskin
Copy link
Member

jyasskin commented Aug 4, 2016

navigator.permissions.query() returns an object that has an event that fires when permission has changed. I think developers can use that to solve this use case?

@beaufortfrancois
Copy link
Member Author

For the sake of completeness, is this how it would work?

navigator.permissions.query({
  name: "bluetooth",
  deviceId: sessionStorage.lastDevice /* because we've stored it previously */
}).then(result => {
  result.addEventListener('change', function(event) {
    // Permission status has changed for this device
    console.log(event.target);
  })
})

If result.devices were to return multiple devices, would there be multiple events fired for each device?

Is this how do I access a newly paired BluetoothDevice object with this change triggered event?

navigator.permissions.query({
  name: "bluetooth",
  optionalServices: [ 'battery_service' ], 
}).then(result => {
  result.addEventListener('change', function(event) {
    console.log(event.target); /* Is this BluetoothDevice?
  })
})

@jyasskin Sorry for this, I'm still having hard time getting the full Permissions API scope for Web Bluetooth.

@jyasskin
Copy link
Member

jyasskin commented Aug 5, 2016

Close. To detect that you've lost access to the device, use:

navigator.permissions.query({
  name: "bluetooth",
  deviceId: sessionStorage.lastDevice /* because we've stored it previously */
}).then(result => {
  if (result.devices.length == 0) {
    console.log("Already lost access");
  } else {
    assert(result.devices[0].id == sessionStorage.lastDevice);
    result.addEventListener('change', function(event) {
      assert(result == event.target);
      if (result.devices.length == 0)
        console.log("Lost access");
    });
  }
})

To learn about newly-paired devices, use:

navigator.permissions.query({
  name: "bluetooth",
  services: [ 'battery_service' ], 
}).then(result => {
  result.addEventListener('change', function(event) {
    for (let device in result.devices) {
      if (!(device.id in knownDeviceIds)) {
        console.log("New device!", device);
      }
    }
  });
})

If result.devices were to return multiple devices, would there be multiple events fired for each device?

There would be an event for each time result.devices changes, which might be just once if the user revokes all devices in one UI action, or multiple if the user revokes them one at a time.

@beaufortfrancois
Copy link
Member Author

Based on @jyasskin code samples, I believe devicechange events are not necessary. All we have to do is to help developers with official code samples to understand how it works.
Closing this issue then.
Feel free to reopen if something's not clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants