Web Bluetooth / Exclusion Filters Sample
Available in Chrome 114+ |
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 retrieve basic
device information from a nearby Bluetooth Low Energy Device featuring
basic exclusion filters. You may want to check out the
Exclusion Filters (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 () {
let filters = populateFiltersById ( ' filters ' );
let exclusionFilters = populateFiltersById ( ' exclusionFilters ' );
let options = { filters , exclusionFilters };
log ( ' Requesting Bluetooth Device... ' );
log ( ' with ' + JSON . stringify ( options ));
navigator . bluetooth . requestDevice ( options )
. then ( device => {
log ( ' > Name: ' + device . name );
log ( ' > Id: ' + device . id );
log ( ' > Connected: ' + device . gatt . connected );
})
. catch ( error => {
log ( ' Argh! ' + error );
});
}
/* Utils */
function populateFiltersById ( id ) {
let filters = [];
let filterService = document . querySelector ( `# ${ id } .service` ). value ;
if ( filterService . startsWith ( ' 0x ' )) {
filterService = parseInt ( filterService );
}
if ( filterService ) {
filters . push ({ services : [ filterService ]});
}
let filterName = document . querySelector ( `# ${ id } .name` ). value ;
if ( filterName ) {
filters . push ({ name : filterName });
}
let filterNamePrefix = document . querySelector ( `# ${ id } .namePrefix` ). value ;
if ( filterNamePrefix ) {
filters . push ({ namePrefix : filterNamePrefix });
}
return filters ;
}