For users who struggled with the basics, this is what I learned:
- Bluetooth on HTML 5 works only in Chrome/Edge: Mozilla and Safari both officially said they won't implement it due to security/privacy reasons (https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#browser_compatibility)
- It only works with HTTPS or localhost. If it doesn't seem to work, be sure you are connected to a secure server. HTTP won't work.
- For security reason, any bluetooth scanning/connecting operation must be triggered by the user, so you must link it to e.g. a button press
- This is the basic code to scan for ALL devices (remember to call this upon a button click for example):
function requestDevice() {
navigator.bluetooth.requestDevice({
acceptAllDevices: true
})
.then(device => {
print(`Connected!`)
}
)}
If you already know for example the name(s) of your device, you can filter by the name, so you will ONLY see those with that name:
function requestDevice() {
navigator.bluetooth.requestDevice({
acceptAllDevices: false,
filters: [
{ name: 'MyLovedBLEDevice'}
]
})
.then(device => {
print(`Connected!`)
}
)}
Now my question is... If I know that I want to connect to that device name, every time... And if there are two, ok, doesn't matter, the first or a random one, as long as it's called like that...
Click, and (if it's available of course) it's connected.
Is it at all possible?