nrs jayaram
nrs jayaram

Reputation: 3448

Web Bluetooth - getting error while connect mobile device to web browser via bluetooth

I am trying to get Mobile request data to web browser(Laptop) via Bluetooth, so my fist step is connect system bluetooth to web browser, but getting error message by using below code. or is there any other way to connect mobile to web browser via bluetooth to transfer data?

navigator.bluetooth.requestDevice().then(
function (d){console.log("found Device !!");}, 
function (e){console.log("Oh no !!",e);});

I tried above code in chrome.

Error message :

TypeError: Failed to execute 'requestDevice' on 'Bluetooth': 1 argument required, but only 0 present

Upvotes: 1

Views: 5462

Answers (2)

François Beaufort
François Beaufort

Reputation: 5659

You might want to read https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web which shows you all mandatory options you have to pass:

For instance, requesting Bluetooth devices advertising the Bluetooth GATT Battery Service is this simple:

navigator.bluetooth.requestDevice({ filters: [{ services: ['battery_service'] }] })
.then(device => { /* ... */ })
.catch(error => { console.log(error); });

If your Bluetooth GATT Service is not on the list of the standardized Bluetooth GATT services though, you may provide either the full Bluetooth UUID or a short 16- or 32-bit form.

navigator.bluetooth.requestDevice({
  filters: [{
    services: [0x1234, 0x12345678, '99999999-0000-1000-8000-00805f9b34fb']
  }]
})
.then(device => { /* ... */ })
.catch(error => { console.log(error); });

You can also request Bluetooth devices based on the device name being advertised with the name filters key, or even a prefix of this name with the namePrefix filters key. Note that in this case, you will also need to define the optionalServices key to be able to access some services. If you don't, you'll get an error later when trying to access them.

navigator.bluetooth.requestDevice({
  filters: [{
    name: 'Francois robot'
  }],
  optionalServices: ['battery_service']
})
.then(device => { /* ... */ })
.catch(error => { console.log(error); });

Upvotes: 3

Sarah Groß
Sarah Groß

Reputation: 10879

As the error message tells you, you need to supply an options object to the requestDevice(options) method. See https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice

Upvotes: 1

Related Questions