-
Notifications
You must be signed in to change notification settings - Fork 190
feat: add WASM/WebBluetooth support #436
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
Draft
acouvreur
wants to merge
1
commit into
tinygo-org:dev
Choose a base branch
from
acouvreur:add-webbluetooth
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.