| 1 | var myDescriptor; |
| 2 | |
| 3 | function onReadButtonClick() { |
| 4 | let serviceUuid = document.querySelector('#service').value; |
| 5 | if (serviceUuid.startsWith('0x')) { |
| 6 | serviceUuid = parseInt(serviceUuid); |
| 7 | } |
| 8 | |
| 9 | let characteristicUuid = document.querySelector('#characteristic').value; |
| 10 | if (characteristicUuid.startsWith('0x')) { |
| 11 | characteristicUuid = parseInt(characteristicUuid); |
| 12 | } |
| 13 | |
| 14 | log('Requesting any Bluetooth Device...'); |
| 15 | navigator.bluetooth.requestDevice({ |
| 16 | // filters: [...] <- Prefer filters to save energy & show relevant devices. |
| 17 | acceptAllDevices: true, |
| 18 | optionalServices: [serviceUuid]}) |
| 19 | .then(device => { |
| 20 | log('Connecting to GATT Server...'); |
| 21 | return device.gatt.connect(); |
| 22 | }) |
| 23 | .then(server => { |
| 24 | log('Getting Service...'); |
| 25 | return server.getPrimaryService(serviceUuid); |
| 26 | }) |
| 27 | .then(service => { |
| 28 | log('Getting Characteristic...'); |
| 29 | return service.getCharacteristic(characteristicUuid); |
| 30 | }) |
| 31 | .then(characteristic => { |
| 32 | log('Getting Descriptor...'); |
| 33 | return characteristic.getDescriptor('gatt.characteristic_user_description'); |
| 34 | }) |
| 35 | .then(descriptor => { |
| 36 | document.querySelector('#writeButton').disabled = |
| 37 | !descriptor.characteristic.properties.write; |
| 38 | myDescriptor = descriptor; |
| 39 | log('Reading Descriptor...'); |
| 40 | return descriptor.readValue(); |
| 41 | }) |
| 42 | .then(value => { |
| 43 | let decoder = new TextDecoder('utf-8'); |
| 44 | log('> Characteristic User Description: ' + decoder.decode(value)); |
| 45 | }) |
| 46 | .catch(error => { |
| 47 | document.querySelector('#writeButton').disabled = true; |
| 48 | log('Argh! ' + error); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | function onWriteButtonClick() { |
| 53 | if (!myDescriptor) { |