The Web Bluetooth
API lets websites discover and communicate with devices over the
Bluetooth 4 wireless standard using the Generic Attribute Profile (GATT). It is
currently partially implemented in Android M, Chrome OS, Mac, and Windows 10.
This sample illustrates the use of the Web Bluetooth API to read all
characteristic's descriptors of a service from a nearby Bluetooth Low Energy
Device. You may want to try this demo with the BLE Peripheral Simulator App
from the Google
Play Store and check out the Read Descriptors (Async
Await) sample.
Live Output
JavaScript Snippet
functiononButtonClick(){letserviceUuid=document.querySelector('#service').value;if(serviceUuid.startsWith('0x')){serviceUuid=parseInt(serviceUuid);}letcharacteristicUuid=document.querySelector('#characteristic').value;if(characteristicUuid.startsWith('0x')){characteristicUuid=parseInt(characteristicUuid);}log('Requesting any Bluetooth Device...');navigator.bluetooth.requestDevice({// filters: [...] <- Prefer filters to save energy & show relevant devices.acceptAllDevices:true,optionalServices:[serviceUuid]}).then(device=>{log('Connecting to GATT Server...');returndevice.gatt.connect();}).then(server=>{log('Getting Service...');returnserver.getPrimaryService(serviceUuid);}).then(service=>{log('Getting Characteristic...');returnservice.getCharacteristic(characteristicUuid);}).then(characteristic=>{log('Getting Descriptors...');returncharacteristic.getDescriptors();}).then(descriptors=>{letqueue=Promise.resolve();descriptors.forEach(descriptor=>{switch(descriptor.uuid){caseBluetoothUUID.getDescriptor('gatt.client_characteristic_configuration'):queue=queue.then(_=>descriptor.readValue()).then(value=>{log('> Client Characteristic Configuration:');letnotificationsBit=value.getUint8(0)&0b01;log(' > Notifications: '+(notificationsBit?'ON':'OFF'));letindicationsBit=value.getUint8(0)&0b10;log(' > Indications: '+(indicationsBit?'ON':'OFF'));});break;caseBluetoothUUID.getDescriptor('gatt.characteristic_user_description'):queue=queue.then(_=>descriptor.readValue()).then(value=>{letdecoder=newTextDecoder('utf-8');log('> Characteristic User Description: '+decoder.decode(value));});break;caseBluetoothUUID.getDescriptor('report_reference'):queue=queue.then(_=>descriptor.readValue()).then(value=>{log('> Report Reference:');log(' > Report ID: '+value.getUint8(0));log(' > Report Type: '+getReportType(value));});break;default:log('> Unknown Descriptor: '+descriptor.uuid);}});returnqueue;}).catch(error=>{log('Argh! '+error);});}/* Utils */constvalueToReportType={1:'Input Report',2:'Output Report',3:'Feature Report'};functiongetReportType(value){letv=value.getUint8(1);returnv+(vinvalueToReportType?' ('+valueToReportType[v]+')':'Unknown');}