2017-08-17 154 views
0

我是网络蓝牙api的初学者,我想获得我的android蓝牙设备的一些ID或MAC地址...或者通过API识别每个设备的某种方式。 其实我有这个如何获取我的android蓝牙设备的ID或MAC地址?

// 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

可以使用BluetoothDevice.id attribute来分辨两个类似设备。如果设备已被命名为有用的名字,您也可以使用name。 “Nexus 6p(鲍勃的)”。这是一个网络蓝牙Device Info Sample这样做。

您可能会发现这些限制。网络蓝牙尝试以较低的级别暴露蓝牙概念,并且只需对蓝牙规范进行最小限度的更改,但会采取一些措施来提供与网络相关的安全和隐私保护。该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被阻止。

+0

感谢您的回答,我需要获得一个项目的MAC地址。你知道它是怎么做到的?类似的东西 [chrome:// bluetooth-internals /#devices] –

+0

Web蓝牙故意阻止MAC。请参阅规范中的安全和隐私考虑事项,并在此答案中链接以解释原因。 –

+0

请注意,我已经看到一些BLE设备包含一个自定义的BLE特性,暴露MAC地址,因为iOS也会阻止读取MAC地址。 参见:https://github.com/beaufortfrancois/sandbox/blob/0eeeafd507dc93d2b0f9a2f2fb2a33eedf41589a/web-bluetooth/sense-peanut/sense-peanut.js#L64-L78 –

相关问题