Skip to content

Revisit headless device selection #34

@thegecko

Description

@thegecko

Since implementing the Web Bluetooth extensions for bleat here, I've ben thinking about possibly implementing the headless device selection UI in a different way. I think maybe having a way of registering a developer-created UI which doesn't dirty the main specification could be a different way to go. This could be implemented in one of two ways.

  1. With an event handler
// New, separate root function to register a requestDevice() scan handler
bluetooth.addScanHandler(function(scanner) {
    // This function is called whenever 'requestDevice()' is executed.
    // The scanner object looks like:
    // {
    //    scan: function to start a scan
    //    devices: [] of found devices
    //    resolve: requestDevice.Promise.resolve
    //    reject: requestDevice.Promise.reject
    // }

    // This event fires when scanner.devices are changed
    scanner.addEventListener("deviceschanged", updateUI);

    // When scanner.devices have changed
    function updateUI(device) {
        // Show UI
        scanner.devices.forEach(function(device) {
            ...
        });
    }

    // When a user chooses
    function chooseDevice(index) {
        var device = scanner.devices[index];
        scanner.resolve(device);
    }

    // Begin scan
    scanner.scan();
});
  1. With a callback function
// New, separate root function to register a requestDevice() scan handler
bluetooth.addScanHandler(function(scanner) {
    // This function is called whenever 'requestDevice()' is executed.
    // The scanner object looks like:
    // {
    //    scan: function to start a scan
    //    onDeviceFound: function to register a device found callback
    //    resolve: requestDevice.Promise.resolve
    //    reject: requestDevice.Promise.reject
    // }

    // This function is called when a device is found
    scanner.onDeviceFound(updateUI);

    // Store devices
    var devices = [];
    function updateUI(device) {
        devices.push(device);
        // Show UI
    }

    // When a user chooses
    function chooseDevice(index) {
        var device = devices[index];
        scanner.resolve(device);
    }

    // Begin scan
    scanner.scan();
});

You can then use requestDevice() just as before:

bluetooth.requestDevice({
    ...
})
.then(device => ...

Either of these methods could be used to undertake eddystone scanning, too.

@mikaelkindborg, @jyasskin is this a preferable approach, or do you have any further thoughts?

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions