/
bluetooth.clj
205 lines (148 loc) · 9.22 KB
/
bluetooth.clj
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(ns chromex.app.bluetooth
"Use the chrome.bluetooth API to connect to a Bluetooth
device. All functions report failures via chrome.runtime.lastError.
* available since Chrome 37
* https://developer.chrome.com/apps/bluetooth"
(:refer-clojure :only [defmacro defn apply declare meta let partial])
(:require [chromex.wrapgen :refer [gen-wrap-helper]]
[chromex.callgen :refer [gen-call-helper gen-tap-all-events-call]]))
(declare api-table)
(declare gen-call)
; -- functions --------------------------------------------------------------------------------------------------------------
(defmacro get-adapter-state
"Get information about the Bluetooth adapter.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [adapter-info] where:
|adapter-info| - Object containing the adapter information.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/apps/bluetooth#method-getAdapterState."
([] (gen-call :function ::get-adapter-state &form)))
(defmacro get-device
"Get information about a Bluetooth device known to the system.
|device-address| - Address of device to get.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [device-info] where:
|device-info| - Object containing the device information.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/apps/bluetooth#method-getDevice."
([device-address] (gen-call :function ::get-device &form device-address)))
(defmacro get-devices
"Get a list of Bluetooth devices known to the system, including paired and recently discovered devices.
|filter| - Some criteria to filter the list of returned bluetooth devices. If the filter is not set or set to {},
returned device list will contain all bluetooth devices. Right now this is only supported in ChromeOS, for
other platforms, a full list is returned.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [device-infos] where:
|device-infos| - Array of object containing device information.
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/apps/bluetooth#method-getDevices."
([filter] (gen-call :function ::get-devices &form filter))
([] `(get-devices :omit)))
(defmacro start-discovery
"Start discovery. Newly discovered devices will be returned via the onDeviceAdded event. Previously discovered devices
already known to the adapter must be obtained using getDevices and will only be updated using the |onDeviceChanged| event
if information about them changes.Discovery will fail to start if this application has already called startDiscovery.
Discovery can be resource intensive: stopDiscovery should be called as soon as possible.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [].
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/apps/bluetooth#method-startDiscovery."
([] (gen-call :function ::start-discovery &form)))
(defmacro stop-discovery
"Stop discovery.
This function returns a core.async channel of type `promise-chan` which eventually receives a result value.
Signature of the result value put on the channel is [].
In case of an error the channel closes without receiving any value and relevant error object can be obtained via
chromex.error/get-last-error.
https://developer.chrome.com/apps/bluetooth#method-stopDiscovery."
([] (gen-call :function ::stop-discovery &form)))
; -- events -----------------------------------------------------------------------------------------------------------------
;
; docs: https://github.com/binaryage/chromex/#tapping-events
(defmacro tap-on-adapter-state-changed-events
"Fired when the state of the Bluetooth adapter changes.
Events will be put on the |channel| with signature [::on-adapter-state-changed [state]] where:
|state| - The new state of the adapter.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/apps/bluetooth#event-onAdapterStateChanged."
([channel & args] (apply gen-call :event ::on-adapter-state-changed &form channel args)))
(defmacro tap-on-device-added-events
"Fired when information about a new Bluetooth device is available.
Events will be put on the |channel| with signature [::on-device-added [device]] where:
|device| - https://developer.chrome.com/apps/bluetooth#property-onDeviceAdded-device.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/apps/bluetooth#event-onDeviceAdded."
([channel & args] (apply gen-call :event ::on-device-added &form channel args)))
(defmacro tap-on-device-changed-events
"Fired when information about a known Bluetooth device has changed.
Events will be put on the |channel| with signature [::on-device-changed [device]] where:
|device| - https://developer.chrome.com/apps/bluetooth#property-onDeviceChanged-device.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/apps/bluetooth#event-onDeviceChanged."
([channel & args] (apply gen-call :event ::on-device-changed &form channel args)))
(defmacro tap-on-device-removed-events
"Fired when a Bluetooth device that was previously discovered has been out of range for long enough to be considered
unavailable again, and when a paired device is removed.
Events will be put on the |channel| with signature [::on-device-removed [device]] where:
|device| - https://developer.chrome.com/apps/bluetooth#property-onDeviceRemoved-device.
Note: |args| will be passed as additional parameters into Chrome event's .addListener call.
https://developer.chrome.com/apps/bluetooth#event-onDeviceRemoved."
([channel & args] (apply gen-call :event ::on-device-removed &form channel args)))
; -- convenience ------------------------------------------------------------------------------------------------------------
(defmacro tap-all-events
"Taps all valid non-deprecated events in chromex.app.bluetooth namespace."
[chan]
(gen-tap-all-events-call api-table (meta &form) chan))
; ---------------------------------------------------------------------------------------------------------------------------
; -- API TABLE --------------------------------------------------------------------------------------------------------------
; ---------------------------------------------------------------------------------------------------------------------------
(def api-table
{:namespace "chrome.bluetooth",
:since "37",
:functions
[{:id ::get-adapter-state,
:name "getAdapterState",
:callback? true,
:params
[{:name "callback",
:type :callback,
:callback {:params [{:name "adapter-info", :type "bluetooth.AdapterState"}]}}]}
{:id ::get-device,
:name "getDevice",
:callback? true,
:params
[{:name "device-address", :type "string"}
{:name "callback", :type :callback, :callback {:params [{:name "device-info", :type "bluetooth.Device"}]}}]}
{:id ::get-devices,
:name "getDevices",
:callback? true,
:params
[{:name "filter", :optional? true, :since "67", :type "object"}
{:name "callback",
:type :callback,
:callback {:params [{:name "device-infos", :type "[array-of-bluetooth.Devices]"}]}}]}
{:id ::start-discovery,
:name "startDiscovery",
:callback? true,
:params [{:name "callback", :optional? true, :type :callback}]}
{:id ::stop-discovery,
:name "stopDiscovery",
:callback? true,
:params [{:name "callback", :optional? true, :type :callback}]}],
:events
[{:id ::on-adapter-state-changed,
:name "onAdapterStateChanged",
:params [{:name "state", :type "bluetooth.AdapterState"}]}
{:id ::on-device-added, :name "onDeviceAdded", :params [{:name "device", :type "bluetooth.Device"}]}
{:id ::on-device-changed, :name "onDeviceChanged", :params [{:name "device", :type "bluetooth.Device"}]}
{:id ::on-device-removed, :name "onDeviceRemoved", :params [{:name "device", :type "bluetooth.Device"}]}]})
; -- helpers ----------------------------------------------------------------------------------------------------------------
; code generation for native API wrapper
(defmacro gen-wrap [kind item-id config & args]
(apply gen-wrap-helper api-table kind item-id config args))
; code generation for API call-site
(def gen-call (partial gen-call-helper api-table))