2017-08-17 156 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 –

相關問題