如何获取我的Android蓝牙设备的ID或MAC地址?

时间:2017-08-17 18:13:31

标签: html5 bluetooth web-bluetooth

我是web-bluetooth api的初学者,我想得到我的android蓝牙设备的ID或MAC地址...... 或者通过API识别每个设备的某种方式。 其实我有这个

// navigator.bluetooth.requestDevice({filters: [{services: ['battery_service']}]})
navigator.bluetooth.requestDevice({acceptAllDevices: true, optionalServices: ['device_information']})
    .then(device => device.gatt.connect())
        .then(server => {
         // Getting device information
            return server.getPrimaryService('device_information');
        })
            .then(service => {
            // Getting serialNumber
                return service.getCharacteristic('serial_number_string');
            })
                .then(characteristic => {
                    // Reading serialNumber
                    return characteristic.readValue();
                })
                    .then(value => {
                        console.log('Serial Number is ' + value.getUint8(0));
                    })
                        .catch(error => {
                            console.log(error);
                        });

1 个答案:

答案 0 :(得分:2)

您可以使用BluetoothDevice.id attribute分辨两个类似的设备。如果设备已被有效命名,您也可以使用name,例如“Nexus 6p(Bob's)”。这是一个Web蓝牙Device Info Sample就是这么做的。

您可能会发现这些限制。 Web蓝牙尝试以较低的级别公开蓝牙概念,而蓝牙规范的变化很小,但确实采取了一些措施来提供与网络相关的安全和隐私保护。 blocklist包含:

# org.bluetooth.characteristic.serial_number_string
# Block access to standardized unique identifiers, for privacy reasons.
00002a25-0000-1000-8000-00805f9b34fb

您读取序列号的代码应生成一个控制台消息,指示您访问阻止列表以获取更多信息。

请参阅与Bluetooth device identifiers相关的规范中的安全和隐私注意事项,了解阻止这些ID被阻止的原因。