[ad_1]
This code works completely on Home windows 10 and 11 on a laptop/desktop however fails to detect greater than two bluetooth gadgets utilizing it on Android (Chrome or Chrome beta) or Apple iPhone utilizing bluefy or pill. Bluetooth and site permissions are set appropriately and Experimental Options are set in Chrome & Chrome Beta. Backend permissions are additionally set on Chrome and Chrome Beta.
I would like this code to detect bluetooth gadgets within the quick space. The issue appears to be with the command “navigator.bluetooth.getDevices()”. Does not appear to work correctly on cellular gadgets.
Any assist could be significantly appreciated.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Appropriate" content material="IE=edge">
<meta title="description" content material="Pattern illustrating using Net Bluetooth / Watch Ads & Join.">
<meta title="viewport" content material="width=device-width, initial-scale=1">
<title>Net Bluetooth / Watch Ads & Join Pattern</title>
<script>
// Add a world error occasion listener early on within the web page load, to assist make sure that browsers
// which do not help particular performance nonetheless find yourself displaying a significant message.
window.addEventListener('error', operate(error) {
if (ChromeSamples && ChromeSamples.setStatus) {
console.error(error);
ChromeSamples.setStatus(error.message + ' (Your browser might not help this characteristic.)');
error.preventDefault();
}
});
</script>
</head>
<physique>
<script>
if('serviceWorker' in navigator) {
// navigator.serviceWorker.register('service-worker.js');
}
</script>
<script>
window.addEventListener('DOMContentLoaded', operate() {
const searchParams = new URL(location).searchParams;
const inputs = Array.from(doc.querySelectorAll('inputhttps://stackoverflow.com/q/72733099'));
inputs.forEach(enter => {
if (searchParams.has(enter.id)) {
if (enter.kind == 'checkbox') {
enter.checked = searchParams.get(enter.id);
} else {
enter.worth = searchParams.get(enter.id);
enter.blur();
}
}
if (enter.kind == 'checkbox') {
enter.addEventListener('change', operate(occasion) {
const newSearchParams = new URL(location).searchParams;
if (occasion.goal.checked) {
newSearchParams.set(enter.id, occasion.goal.checked);
} else {
newSearchParams.delete(enter.id);
}
historical past.replaceState({}, '', Array.from(newSearchParams).size ?
location.pathname + '?' + newSearchParams : location.pathname);
});
} else {
enter.addEventListener('enter', operate(occasion) {
const newSearchParams = new URL(location).searchParams;
if (occasion.goal.worth) {
newSearchParams.set(enter.id, occasion.goal.worth);
} else {
newSearchParams.delete(enter.id);
}
historical past.replaceState({}, '', Array.from(newSearchParams).size ?
location.pathname + '?' + newSearchParams : location.pathname);
});
}
});
});
</script>
<button id="connectToBluetoothDevices">Connect with Bluetooth Gadgets</button>
<button id="requestBluetoothDevice">Request Bluetooth System</button>
<script>
var ChromeSamples = {
log: operate() {
var line = Array.prototype.slice.name(arguments).map(operate(argument) {
return typeof argument === 'string' ? argument : JSON.stringify(argument);
}).be a part of(' ');
doc.querySelector('#log').textContent += line + 'n';
},
clearLog: operate() {
doc.querySelector('#log').textContent="";
},
setStatus: operate(standing) {
doc.querySelector('#standing').textContent = standing;
},
setContent: operate(newContent) {
var content material = doc.querySelector('#content material');
whereas(content material.hasChildNodes()) {
content material.removeChild(content material.lastChild);
}
content material.appendChild(newContent);
}
};
</script>
<h3>Stay Output</h3>
<div id="output" class="output">
<div id="content material"></div>
<div id="standing"></div>
<pre id="log"></pre>
</div>
<script>
if (/Chrome/(d+.d+.d+.d+)/.check(navigator.userAgent)){
// Let's log a warning if the pattern is just not speculated to execute on this
// model of Chrome.
if (87 > parseInt(RegExp.$1)) {
ChromeSamples.setStatus('Warning! Take note this pattern has been examined with Chrome ' + 87 + '.');
}
}
</script>
<script>operate onConnectToBluetoothDevicesButtonClick() {
log('Getting present permitted Bluetooth gadgets...');
navigator.bluetooth.getDevices()
.then(gadgets => {
log('> Received ' + gadgets.size + ' Bluetooth gadgets.');
// These gadgets is probably not powered on or in vary, so scan for
// commercial packets from them earlier than connecting.
for (const system of gadgets) {
connectToBluetoothDevice(system);
}
})
.catch(error => {
log('Argh! ' + error);
});
}
operate connectToBluetoothDevice(system) {
const abortController = new AbortController();
system.addEventListener('advertisementreceived', (occasion) => {
log('> Acquired commercial from "' + system.title + '"...');
// Cease watching ads to preserve battery life.
abortController.abort();
log('Connecting to GATT Server from "' + system.title + '"...');
system.gatt.join()
.then(() => {
log('> Bluetooth system "' + system.title + ' linked.');
})
.catch(error => {
log('Argh! ' + error);
});
}, { as soon as: true });
log('Watching ads from "' + system.title + '"...');
system.watchAdvertisements({ sign: abortController.sign })
.catch(error => {
log('Argh! ' + error);
});
}
operate onRequestBluetoothDeviceButtonClick() {
log('Requesting any Bluetooth system...');
navigator.bluetooth.requestDevice({
// filters: [...] <- Favor filters to save lots of power & present related gadgets.
acceptAllDevices: true
})
.then(system => {
log('> Requested ' + system.title);
})
.catch(error => {
log('Argh! ' + error);
});
}
</script>
<script>
log = ChromeSamples.log;
operate isWebBluetoothEnabled() {
if (navigator.bluetooth) {
return true;
} else {
ChromeSamples.setStatus('Net Bluetooth API is just not obtainable.n' +
'Please be certain that the "Experimental Net Platform options" flag is enabled.');
return false;
}
}
</script>
<script>
if (isWebBluetoothEnabled()) {
doc.querySelector('#connectToBluetoothDevices').addEventListener('click on', operate() {
onConnectToBluetoothDevicesButtonClick();
});
doc.querySelector('#requestBluetoothDevice').addEventListener('click on', operate() {
onRequestBluetoothDeviceButtonClick();
});
}
</script>
</physique>
</html>
[ad_2]
