@@ -7,14 +7,15 @@ import {CONNTYPE} from '../constants.js';
7
7
import { Workflow } from './workflow.js' ;
8
8
import { GenericModal , DeviceInfoModal } from '../common/dialogs.js' ;
9
9
import { sleep } from '../common/utilities.js' ;
10
+ import { bluetooth } from 'webbluetooth' ;
10
11
11
12
const bleNusServiceUUID = 'adaf0001-4369-7263-7569-74507974686e' ;
12
13
const bleNusCharRXUUID = 'adaf0002-4369-7263-7569-74507974686e' ;
13
14
const bleNusCharTXUUID = 'adaf0003-4369-7263-7569-74507974686e' ;
14
15
15
16
const BYTES_PER_WRITE = 20 ;
16
17
17
- let btnRequestBluetoothDevice , btnBond , btnReconnect ;
18
+ let btnRequestBluetoothDevice , btnReconnect ;
18
19
19
20
class BLEWorkflow extends Workflow {
20
21
constructor ( ) {
@@ -30,10 +31,9 @@ class BLEWorkflow extends Workflow {
30
31
this . partialWrites = true ;
31
32
this . type = CONNTYPE . Ble ;
32
33
this . buttonStates = [
33
- { reconnect : false , request : false , bond : false } ,
34
- { reconnect : false , request : true , bond : false } ,
35
- { reconnect : true , request : true , bond : false } ,
36
- { reconnect : false , request : false , bond : true } ,
34
+ { reconnect : false , request : false } ,
35
+ { reconnect : false , request : true } ,
36
+ { reconnect : true , request : true } ,
37
37
] ;
38
38
}
39
39
@@ -53,18 +53,15 @@ class BLEWorkflow extends Workflow {
53
53
let p = this . connectDialog . open ( ) ;
54
54
let modal = this . connectDialog . getModal ( ) ;
55
55
btnRequestBluetoothDevice = modal . querySelector ( '#requestBluetoothDevice' ) ;
56
- btnBond = modal . querySelector ( '#promptBond' ) ;
57
56
btnReconnect = modal . querySelector ( '#bleReconnect' ) ;
58
57
59
58
// Map the button states to the buttons
60
59
this . connectButtons = {
61
60
reconnect : btnReconnect ,
62
- request : btnRequestBluetoothDevice ,
63
- bond : btnBond
61
+ request : btnRequestBluetoothDevice
64
62
} ;
65
63
66
64
btnRequestBluetoothDevice . addEventListener ( 'click' , this . onRequestBluetoothDeviceButtonClick . bind ( this ) ) ;
67
- btnBond . addEventListener ( 'click' , this . onBond . bind ( this ) ) ;
68
65
btnReconnect . addEventListener ( 'click' , this . reconnectButtonHandler . bind ( this ) ) ;
69
66
70
67
// Check if Web Bluetooth is available
@@ -74,7 +71,7 @@ class BLEWorkflow extends Workflow {
74
71
stepOne . classList . add ( "hidden" ) ;
75
72
}
76
73
try {
77
- const devices = await navigator . bluetooth . getDevices ( ) ;
74
+ const devices = await bluetooth . getDevices ( ) ;
78
75
console . log ( devices ) ;
79
76
this . connectionStep ( devices . length > 0 ? 2 : 1 ) ;
80
77
} catch ( e ) {
@@ -120,7 +117,7 @@ class BLEWorkflow extends Workflow {
120
117
if ( ! this . connectionStatus ( ) ) {
121
118
try {
122
119
console . log ( 'Getting existing permitted Bluetooth devices...' ) ;
123
- const devices = await navigator . bluetooth . getDevices ( ) ;
120
+ const devices = await bluetooth . getDevices ( ) ;
124
121
125
122
console . log ( '> Found ' + devices . length + ' Bluetooth device(s).' ) ;
126
123
// These devices may not be powered on or in range, so scan for
@@ -138,7 +135,7 @@ class BLEWorkflow extends Workflow {
138
135
139
136
// Bring up a dialog to request a device
140
137
async requestDevice ( ) {
141
- return navigator . bluetooth . requestDevice ( {
138
+ return bluetooth . requestDevice ( {
142
139
filters : [ { services : [ 0xfebb ] } , ] , // <- Prefer filters to save energy & show relevant devices.
143
140
optionalServices : [ 0xfebb , bleNusServiceUUID ]
144
141
} ) ;
@@ -153,11 +150,13 @@ class BLEWorkflow extends Workflow {
153
150
abortController . abort ( ) ;
154
151
console . log ( 'Connecting to GATT Server from "' + device . name + '"...' ) ;
155
152
try {
156
- await device . gatt . connect ( ) ;
153
+ this . bleServer = await device . gatt . connect ( ) ;
157
154
} catch ( error ) {
158
155
await this . _showMessage ( "Failed to connect to device. Try forgetting device from OS bluetooth devices and try again." ) ;
156
+ // Disable the reconnect button
157
+ this . connectionStep ( 1 ) ;
159
158
}
160
- if ( device . gatt . connected ) {
159
+ if ( this . bleServer && this . bleServer . connected ) {
161
160
console . log ( '> Bluetooth device "' + device . name + ' connected.' ) ;
162
161
await this . switchToDevice ( device ) ;
163
162
} else {
@@ -171,6 +170,7 @@ class BLEWorkflow extends Workflow {
171
170
this . debugLog ( "connecting to " + device . name ) ;
172
171
try {
173
172
console . log ( 'Watching advertisements from "' + device . name + '"...' ) ;
173
+ console . log ( 'If no advertisements are received, make sure the device is powered on and in range. You can also try resetting the device' ) ;
174
174
await device . watchAdvertisements ( { signal : abortController . signal } ) ;
175
175
}
176
176
catch ( error ) {
@@ -187,9 +187,7 @@ class BLEWorkflow extends Workflow {
187
187
let device = await this . requestDevice ( ) ;
188
188
189
189
console . log ( '> Requested ' + device . name ) ;
190
- await device . gatt . connect ( ) ;
191
-
192
- await this . switchToDevice ( device ) ;
190
+ await this . connectToBluetoothDevice ( device ) ;
193
191
/*}
194
192
catch (error) {
195
193
console.error(error);
@@ -203,7 +201,7 @@ class BLEWorkflow extends Workflow {
203
201
this . bleDevice = device ;
204
202
this . bleDevice . removeEventListener ( "gattserverdisconnected" , this . onDisconnected . bind ( this ) ) ;
205
203
this . bleDevice . addEventListener ( "gattserverdisconnected" , this . onDisconnected . bind ( this ) ) ;
206
- this . bleServer = this . bleDevice . gatt ;
204
+ // this.bleServer = this.bleDevice.gatt;
207
205
console . log ( "connected" , this . bleServer ) ;
208
206
let services ;
209
207
@@ -220,26 +218,11 @@ class BLEWorkflow extends Workflow {
220
218
await this . fileHelper . bond ( ) ;
221
219
await this . connectToSerial ( ) ;
222
220
223
- // Enable/Disable UI buttons
224
- this . connectionStep ( 3 ) ;
225
-
226
221
await this . onConnected ( ) ;
227
222
this . connectDialog . close ( ) ;
228
223
await this . loadEditor ( ) ;
229
224
}
230
225
231
- // Bond
232
- async onBond ( e ) {
233
- try {
234
- console . log ( "bond" ) ;
235
- await this . fileHelper . bond ( ) ;
236
- console . log ( "bond done" ) ;
237
- } catch ( e ) {
238
- console . log ( e , e . stack ) ;
239
- }
240
- await this . loadEditor ( ) ;
241
- }
242
-
243
226
async serialTransmit ( msg ) {
244
227
if ( this . rxCharacteristic ) {
245
228
let encoder = new TextEncoder ( ) ;
@@ -272,17 +255,11 @@ class BLEWorkflow extends Workflow {
272
255
}
273
256
// Is this a new connection?
274
257
if ( ! this . bleDevice ) {
275
- let devices = await navigator . bluetooth . getDevices ( ) ;
258
+ let devices = await bluetooth . getDevices ( ) ;
276
259
for ( const device of devices ) {
277
260
await this . connectToBluetoothDevice ( device ) ;
278
261
}
279
262
}
280
-
281
- // Do we have a connection now but still need to connect serial?
282
- if ( this . bleDevice && ! this . bleServer ) {
283
- await this . showBusy ( this . bleDevice . gatt . connect ( ) ) ;
284
- this . switchToDevice ( this . bleDevice ) ;
285
- }
286
263
}
287
264
288
265
updateConnected ( connectionState ) {
0 commit comments