Web Bluetooth / Discover Services & Characteristics Sample
Available in Chrome 53+ |
View on GitHub |
Browse Samples
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 discover all
accessible primary services with server.getPrimaryServices
and their
characteristics 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 Discover Services &
Characteristics Sample (Async Await) sample.
alert_notification
automation_io
battery_service
blood_pressure
body_composition
bond_management
continuous_glucose_monitoring
current_time
cycling_power
cycling_speed_and_cadence
device_information
environmental_sensing
generic_access
generic_attribute
glucose
health_thermometer
heart_rate
human_interface_device (blocklisted)
immediate_alert
indoor_positioning
internet_protocol_support
link_loss
location_and_navigation
next_dst_change
phone_alert_status
pulse_oximeter
reference_time_update
running_speed_and_cadence
scan_parameters
tx_power
user_data
weight_scale
Live Output
JavaScript Snippet
function onButtonClick () {
// Validate services UUID entered by user first.
let optionalServices = document . querySelector ( ' #optionalServices ' ). value
. split ( /, ? / ). map ( s => s . startsWith ( ' 0x ' ) ? parseInt ( s ) : s )
. filter ( s => s && BluetoothUUID . getService );
log ( ' Requesting any Bluetooth Device... ' );
navigator . bluetooth . requestDevice ({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices : true ,
optionalServices : optionalServices })
. then ( device => {
log ( ' Connecting to GATT Server... ' );
return device . gatt . connect ();
})
. then ( server => {
// Note that we could also get all services that match a specific UUID by
// passing it to getPrimaryServices().
log ( ' Getting Services... ' );
return server . getPrimaryServices ();
})
. then ( services => {
log ( ' Getting Characteristics... ' );
let queue = Promise . resolve ();
services . forEach ( service => {
queue = queue . then ( _ => service . getCharacteristics (). then ( characteristics => {
log ( ' > Service: ' + service . uuid );
characteristics . forEach ( characteristic => {
log ( ' >> Characteristic: ' + characteristic . uuid + ' ' +
getSupportedProperties ( characteristic ));
});
}));
});
return queue ;
})
. catch ( error => {
log ( ' Argh! ' + error );
});
}
/* Utils */
function getSupportedProperties ( characteristic ) {
let supportedProperties = [];
for ( const p in characteristic . properties ) {
if ( characteristic . properties [ p ] === true ) {
supportedProperties . push ( p . toUpperCase ());
}
}
return ' [ ' + supportedProperties . join ( ' , ' ) + ' ] ' ;
}