Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bluetooth: Add acceptAllDevices #458

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions web-bluetooth/_includes/intro.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,36 @@

inputs.forEach(input => {
if (searchParams.has(input.id)) {
input.value = searchParams.get(input.id);
}
input.addEventListener('input', function(event) {
const newSearchParams = new URL(location).searchParams;
if (event.target.value) {
newSearchParams.set(input.id, event.target.value);
if (input.type == 'checkbox') {
input.checked = searchParams.get(input.id);
} else {
newSearchParams.delete(input.id);
input.value = searchParams.get(input.id);
input.blur();
}
history.replaceState({}, '', Array.from(newSearchParams).length ?
location.pathname + '?' + newSearchParams : location.pathname);
});
}
if (input.type == 'checkbox') {
input.addEventListener('change', function(event) {
const newSearchParams = new URL(location).searchParams;
if (event.target.checked) {
newSearchParams.set(input.id, event.target.checked);
} else {
newSearchParams.delete(input.id);
}
history.replaceState({}, '', Array.from(newSearchParams).length ?
location.pathname + '?' + newSearchParams : location.pathname);
});
} else {
input.addEventListener('input', function(event) {
const newSearchParams = new URL(location).searchParams;
if (event.target.value) {
newSearchParams.set(input.id, event.target.value);
} else {
newSearchParams.delete(input.id);
}
history.replaceState({}, '', Array.from(newSearchParams).length ?
location.pathname + '?' + newSearchParams : location.pathname);
});
}
});
});
</script>
11 changes: 2 additions & 9 deletions web-bluetooth/automatic-reconnect-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ async function onButtonClick() {
try {
log('Requesting any Bluetooth Device...');
bluetoothDevice = await navigator.bluetooth.requestDevice({
filters: anyNamedDevice()});
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true});
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
connect();
} catch(error) {
Expand Down Expand Up @@ -52,14 +53,6 @@ async function exponentialBackoff(max, delay, toTry, success, fail) {
}
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}

function time(text) {
log('[' + new Date().toJSON().substr(11, 8) + '] ' + text);
}
12 changes: 3 additions & 9 deletions web-bluetooth/automatic-reconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var bluetoothDevice;
function onButtonClick() {
bluetoothDevice = null;
log('Requesting any Bluetooth Device...');
navigator.bluetooth.requestDevice({filters: anyNamedDevice()})
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true})
.then(device => {
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
Expand Down Expand Up @@ -51,14 +53,6 @@ function exponentialBackoff(max, delay, toTry, success, fail) {
});
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}

function time(text) {
log('[' + new Date().toJSON().substr(11, 8) + '] ' + text);
}
8 changes: 5 additions & 3 deletions web-bluetooth/device-info-async-await.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
check out the <a href="device-info.html">Device Info (Promises)</a> sample.</p>

<form>
<input id="service" type="text" list="services" autofocus placeholder="Bluetooth Service">
<input id="name" type="text" placeholder="Device Name">
<input id="namePrefix" type="text" placeholder="Device Name Prefix">
<label for="allDevices">All Devices</label>
<input id="allDevices" type="checkbox">
<input id="service" type="text" size=17 list="services" placeholder="Bluetooth Service">
<input id="name" type="text" size=17 placeholder="Device Name">
<input id="namePrefix" type="text" size=17 placeholder="Device Name Prefix">
<button>Get Bluetooth Device Info</button>
</form>

Expand Down
10 changes: 9 additions & 1 deletion web-bluetooth/device-info-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ async function onButtonClick() {
filters.push({namePrefix: filterNamePrefix});
}

let options = {};
if (document.querySelector('#allDevices').checked) {
options.acceptAllDevices = true;
} else {
options.filters = filters;
}

try {
log('Requesting Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({filters: filters});
log('with ' + JSON.stringify(options));
const device = await navigator.bluetooth.requestDevice(options);

log('> Name: ' + device.name);
log('> Id: ' + device.id);
Expand Down
8 changes: 5 additions & 3 deletions web-bluetooth/device-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
Await)</a> sample.</p>

<form>
<input id="service" type="text" list="services" autofocus placeholder="Bluetooth Service">
<input id="name" type="text" placeholder="Device Name">
<input id="namePrefix" type="text" placeholder="Device Name Prefix">
<label for="allDevices">All Devices</label>
<input id="allDevices" type="checkbox">
<input id="service" type="text" size=17 list="services" placeholder="Bluetooth Service">
<input id="name" type="text" size=17 placeholder="Device Name">
<input id="namePrefix" type="text" size=17 placeholder="Device Name Prefix">
<button>Get Bluetooth Device Info</button>
</form>

Expand Down
10 changes: 9 additions & 1 deletion web-bluetooth/device-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ function onButtonClick() {
filters.push({namePrefix: filterNamePrefix});
}

let options = {};
if (document.querySelector('#allDevices').checked) {
options.acceptAllDevices = true;
} else {
options.filters = filters;
}

log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice({filters: filters})
log('with ' + JSON.stringify(options));
navigator.bluetooth.requestDevice(options)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that you are not allowed to use acceptAllDevices with filters so you might need some logic for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've fixed that in 4ebbcd1

.then(device => {
log('> Name: ' + device.name);
log('> Id: ' + device.id);
Expand Down
12 changes: 3 additions & 9 deletions web-bluetooth/device-information-characteristics-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ async function onButtonClick() {
try {
log('Requesting any Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({
filters: anyNamedDevice(), optionalServices: ['device_information']});
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['device_information']});

log('Connecting to GATT Server...');
const server = await device.gatt.connect();
Expand Down Expand Up @@ -105,11 +107,3 @@ function getUsbVendorName(value) {
return value +
(value in valueToUsbVendorName ? ' (' + valueToUsbVendorName[value] + ')' : '');
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}
14 changes: 4 additions & 10 deletions web-bluetooth/device-information-characteristics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
function onButtonClick() {
log('Requesting any Bluetooth Device...');
navigator.bluetooth.requestDevice(
{filters: anyNamedDevice(), optionalServices: ['device_information']})
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['device_information']})
.then(device => {
log('Connecting to GATT Server...');
return device.gatt.connect();
Expand Down Expand Up @@ -110,11 +112,3 @@ function getUsbVendorName(value) {
return value +
(value in valueToUsbVendorName ? ' (' + valueToUsbVendorName[value] + ')' : '');
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ async function onButtonClick() {
try {
log('Requesting any Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({
filters: anyNamedDevice(), optionalServices: optionalServices});
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: optionalServices});

log('Connecting to GATT Server...');
const server = await device.gatt.connect();
Expand Down Expand Up @@ -43,11 +45,3 @@ function getSupportedProperties(characteristic) {
}
return '[' + supportedProperties.join(', ') + ']';
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}
14 changes: 4 additions & 10 deletions web-bluetooth/discover-services-and-characteristics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ function onButtonClick() {
.filter(s => s && BluetoothUUID.getService);

log('Requesting any Bluetooth Device...');
navigator.bluetooth.requestDevice(
{filters: anyNamedDevice(), optionalServices: optionalServices})
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();
Expand Down Expand Up @@ -47,11 +49,3 @@ function getSupportedProperties(characteristic) {
}
return '[' + supportedProperties.join(', ') + ']';
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}
12 changes: 3 additions & 9 deletions web-bluetooth/gap-characteristics-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ async function onButtonClick() {
try {
log('Requesting any Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({
filters: anyNamedDevice(), optionalServices: ['generic_access']});
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['generic_access']});

log('Connecting to GATT Server...');
const server = await device.gatt.connect();
Expand Down Expand Up @@ -97,11 +99,3 @@ function getDeviceType(value) {
return value +
(value in valueToDeviceType ? ' (' + valueToDeviceType[value] + ')' : '');
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}
14 changes: 4 additions & 10 deletions web-bluetooth/gap-characteristics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
function onButtonClick() {
log('Requesting any Bluetooth Device...');
navigator.bluetooth.requestDevice(
{filters: anyNamedDevice(), optionalServices: ['generic_access']})
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['generic_access']})
.then(device => {
log('Connecting to GATT Server...');
return device.gatt.connect();
Expand Down Expand Up @@ -107,11 +109,3 @@ function getDeviceType(value) {
return value +
(value in valueToDeviceType ? ' (' + valueToDeviceType[value] + ')' : '');
}

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}
4 changes: 3 additions & 1 deletion web-bluetooth/link-loss-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ async function onReadButtonClick() {
try {
log('Requesting Bluetooth Device...');
const device = await navigator.bluetooth.requestDevice({
filters: [{services: ['link_loss']}]});
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['link_loss']});

log('Connecting to GATT Server...');
const server = await device.gatt.connect();
Expand Down
5 changes: 4 additions & 1 deletion web-bluetooth/link-loss.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var alertLevelCharacteristic;

function onReadButtonClick() {
log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice({filters: [{services: ['link_loss']}]})
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['link_loss']})
.then(device => {
log('Connecting to GATT Server...');
return device.gatt.connect();
Expand Down
17 changes: 4 additions & 13 deletions web-bluetooth/read-characteristic-value-changed-async-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ async function onReadBatteryLevelButtonClick() {
}

async function requestDevice() {
log('Requesting Bluetooth Device...');
log('Requesting any Bluetooth Device...');
bluetoothDevice = await navigator.bluetooth.requestDevice({
filters: anyNamedDevice(), optionalServices: ['battery_service']});
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['battery_service']});
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
}

Expand Down Expand Up @@ -95,14 +97,3 @@ async function onDisconnected() {
log('Argh! ' + error);
}
}


/* Utils */

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}
18 changes: 5 additions & 13 deletions web-bluetooth/read-characteristic-value-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ function onReadBatteryLevelButtonClick() {
}

function requestDevice() {
log('Requesting Bluetooth Device...');
return navigator.bluetooth.requestDevice(
{filters: anyNamedDevice(), optionalServices: ['battery_service']})
log('Requesting any Bluetooth Device...');
return navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['battery_service']})
.then(device => {
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
Expand Down Expand Up @@ -99,13 +101,3 @@ function onDisconnected() {
log('Argh! ' + error);
});
}

/* Utils */

function anyNamedDevice() {
// This is the closest we can get for now to get all devices.
// https://github.com/WebBluetoothCG/web-bluetooth/issues/234
return Array.from('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
.map(c => ({namePrefix: c}))
.concat({name: ''});
}