Repository navigation
Expand file tree
/
Copy pathadapter_js.go
More file actions
114 lines (96 loc) · 3.49 KB
/
Copy pathadapter_js.go
File metadata and controls
114 lines (96 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package bluetooth
import (
"errors"
"syscall/js"
)
var _ BLEAdapter = (*Adapter)(nil)
var errNotEnabled = errors.New("bluetooth: adapter is not enabled, call Enable first")
// Adapter represents the WebBluetooth adapter accessed via navigator.bluetooth.
type Adapter struct {
bluetooth js.Value
connectHandler func(device Device, connected bool)
// devices holds the BluetoothDevice objects returned by Scan, keyed by
// device ID, because Connect needs them again.
devices map[string]js.Value
// disconnectListeners holds the gattserverdisconnected listeners, keyed
// by device ID.
disconnectListeners map[string]js.Func
// notifying holds the characteristics that have a value change listener.
notifying map[*deviceCharacteristic]struct{}
scanning bool
// RequestedServices is the list of service UUIDs to give as
// optionalServices to navigator.bluetooth.requestDevice().
//
// The browser refuses access to a service that is not in this list. Set
// this before you call 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")
}
// navigator.bluetooth only exists in a secure context.
// https://webbluetoothcg.github.io/web-bluetooth/#navigator-extensions
if secure := js.Global().Get("isSecureContext"); secure.Type() == js.TypeBoolean && !secure.Bool() {
return errors.New("bluetooth: WebBluetooth needs a secure context, such as HTTPS or localhost")
}
bt := navigator.Get("bluetooth")
if bt.IsUndefined() {
return errors.New("bluetooth: WebBluetooth is not supported in this browser")
}
// This is false without an adapter, or when a policy blocks WebBluetooth.
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-getavailability
available, err := await(bt.Call("getAvailability"))
if err != nil {
return err
}
if !available.Truthy() {
return errors.New("bluetooth: no adapter, or the browser blocks WebBluetooth")
}
a.bluetooth = bt
// Keep the known devices when Enable runs more than once.
if a.devices == nil {
a.devices = map[string]js.Value{}
a.disconnectListeners = map[string]js.Func{}
a.notifying = map[*deviceCharacteristic]struct{}{}
}
return nil
}
// Reset clears the state of the adapter so that Enable can run again.
//
// The devices from an earlier scan are gone after this, so the user must
// select a device again.
func (a *Adapter) Reset() error {
// Remove the listeners before the disconnect, so that no event arrives
// after the release of the JS function.
for c := range a.notifying {
c.stopListening()
}
for id, listener := range a.disconnectListeners {
if device, ok := a.devices[id]; ok {
device.Call("removeEventListener", "gattserverdisconnected", listener)
}
listener.Release()
}
// The browser keeps the connection until the page closes it.
for _, device := range a.devices {
gatt := device.Get("gatt")
if !gatt.IsUndefined() && gatt.Get("connected").Bool() {
gatt.Call("disconnect")
}
}
a.bluetooth = js.Undefined()
clear(a.devices)
clear(a.disconnectListeners)
a.scanning = false
return nil
}