raks
raks

Reputation: 329

Web Bluetooth API get list of all available device nearby

I am looking for an API call that could be used to retrieve all the Bluetooth devices near me. These samples Web Bluetooth talk about getting different characteristics from a single Bluetooth device, however I couldn't find any example which retrieves all the Bluetooth devices(like available bluetooth devices list on windows native Bluetooth app). Is it even supported?

Upvotes: 3

Views: 6187

Answers (3)

ashleedawg
ashleedawg

Reputation: 21619

(Well, this question sent me down a rabbit hole...)

It's a couple years later and we're starting to signs of life from api.Bluetooth.getDevices. I eventually got an experimental demo working in Chrome.

You'll probably need to enable Chrome's "Web Bluetooth experiment". (Firefox has an equivalent but I didn't try it.)

  1. Go to chrome://flags/ then search for bluetooth and enable the API.

  2. Click the buttons on the demo page: Web Bluetooth / Get Devices Sample


Demo code from the same page:

function onGetBluetoothDevicesButtonClick() {
  log('Getting existing permitted Bluetooth devices...');
  navigator.bluetooth.getDevices()
  .then(devices => {
    log('> Got ' + devices.length + ' Bluetooth devices.');
    for (const device of devices) {
      log('  > ' + device.name + ' (' + device.id + ')');
    }
  })
  .catch(error => {
    log('Argh! ' + error);
  });
}

function onRequestBluetoothDeviceButtonClick() {
  log('Requesting any Bluetooth device...');
  navigator.bluetooth.requestDevice({
 // filters: [...] <- Prefer filters to save energy & show relevant devices.
    acceptAllDevices: true
  })
  .then(device => {
    log('> Requested ' + device.name + ' (' + device.id + ')');
  })
  .catch(error => {
    log('Argh! ' + error);
  });
}

Good luck!

Upvotes: 2

Vincent Scheib
Vincent Scheib

Reputation: 18600

A Web Bluetooth Scanning specification draft exists, but implementation isn't started anywhere as of 2018.

Upvotes: 1

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

I personally haven't experimented with Web Bluetooth API a lot, but I think you're looking for Device Discovery and Request Bluetooth Devices:

This version of the Web Bluetooth API specification allows websites, running in the Central role, to connect to remote GATT Servers over a BLE connection. It supports communication among devices that implement Bluetooth 4.0 or later.

When a website requests access to nearby devices using navigator.bluetooth.requestDevice, Google Chrome will prompt user with a device chooser where they can pick one device or simply cancel the request.

The navigator.bluetooth.requestDevice function takes a mandatory Object that defines filters. These filters are used to return only devices that match some advertised Bluetooth GATT services and/or the device name.

Also, here's the list of GATT Services.

Upvotes: 1

Related Questions