Skip to content
Draft
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
44 changes: 44 additions & 0 deletions adapter_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package bluetooth

import (
"errors"
"syscall/js"
)

var _ BLEAdapter = (*Adapter)(nil)

// Adapter represents the WebBluetooth adapter accessed via navigator.bluetooth.
type Adapter struct {
bluetooth js.Value
connectHandler func(device Device, connected bool)

// RequestedServices is the list of service UUIDs that will be passed as
// optionalServices to navigator.bluetooth.requestDevice(). WebBluetooth
// requires services to be declared upfront — you cannot discover or
// access services that are not listed here or matched by a filter.
//
// Set this before calling Scan().
RequestedServices []UUID
}

// DefaultAdapter is the default adapter using the navigator.bluetooth API.
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = &Adapter{
connectHandler: func(device Device, connected bool) {},
}

// Enable configures the BLE stack. It must be called before any
// Bluetooth-related calls (unless otherwise indicated).
func (a *Adapter) Enable() error {
navigator := js.Global().Get("navigator")
if navigator.IsUndefined() {
return errors.New("bluetooth: navigator is not available")
}
bt := navigator.Get("bluetooth")
if bt.IsUndefined() {
return errors.New("bluetooth: WebBluetooth is not supported in this browser")
}
a.bluetooth = bt
return nil
}
38 changes: 38 additions & 0 deletions examples/webbluetooth/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebBluetooth – TinyGo Device Information</title>
<style>
body { font-family: system-ui, sans-serif; max-width: 720px; margin: 2rem auto; padding: 0 1rem; }
button { font-size: 1.1rem; padding: 0.5rem 1.5rem; cursor: pointer; }
#log { background: #f5f5f5; border: 1px solid #ddd; border-radius: 4px; padding: 1rem; margin-top: 1rem; min-height: 120px; }
#log p { margin: 0.25rem 0; font-family: monospace; font-size: 0.9rem; }
</style>
</head>
<body>
<h1>WebBluetooth – TinyGo Device Information</h1>
<p>Click the button below to connect to a BLE device and read its Device Information service (manufacturer, model, firmware).</p>
<button id="connectBtn" disabled>Loading WASM…</button>
<div id="log"></div>

<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("wasm.wasm"), go.importObject).then((result) => {
go.run(result.instance);
const btn = document.getElementById("connectBtn");
btn.textContent = "Connect";
btn.disabled = false;
btn.addEventListener("click", () => {
if (typeof btConnect === "function") {
btConnect();
} else {
alert("WASM not ready yet");
}
});
});
</script>
</body>
</html>
Binary file added examples/webbluetooth/html/wasm.wasm
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should not commit that file.

Will remove it and add the README with tinygo commands to generate the wasm file.

Binary file not shown.
Loading
Loading