Search code examples
javascriptwebbluetoothprogressive-web-apps

Javascript get nearby Bluetooth devices Mac Address


I need my web project to get nearby Bluetooth devices Mac Address. I use this code:

navigator.bluetooth.requestDevice(acceptAllDevices)
.then(device => {
console.log(device)
console.log('> Name:             ' + device.name);
console.log('> Id:               ' + device.id);
console.log('> Connected:        ' + device.gatt.connected);
})
.catch(error => {
console.log('Argh! ' + error);
});

When I run this function get this tab: picture

For unsupported devices, the Bluetooth Mac Address is displayed. But its name property will be null.

However, for recognized devices, it prints the name option. Can I filter the Mac address from the name option?

There may be other ways to query Mac address?


Solution

  • Available filters for Web Bluetooth are "Services", "Name", "Name prefix", and "Manufacturer data" as described at https://web.dev/bluetooth/#request.

    You can try some combination at https://googlechrome.github.io/samples/web-bluetooth/device-info.html and https://googlechrome.github.io/samples/web-bluetooth/manufacturer-data-filter.html

    Regarding the MAC address, it depends on Bluetooth devices. Some devices broadcast either an ephemeral one for privacy, a "static" one, a name which is used for "device.name" when there's one instead of the MAC address.

    Which device are you trying to access? Check out about:bluetooth-internals#devices and try to identify one thing that makes it unique.