@@ -7,14 +7,15 @@ import {CONNTYPE} from '../constants.js';
77import { Workflow } from './workflow.js' ;
88import { GenericModal , DeviceInfoModal } from '../common/dialogs.js' ;
99import { sleep } from '../common/utilities.js' ;
10+ import { bluetooth } from 'webbluetooth' ;
1011
1112const bleNusServiceUUID = 'adaf0001-4369-7263-7569-74507974686e' ;
1213const bleNusCharRXUUID = 'adaf0002-4369-7263-7569-74507974686e' ;
1314const bleNusCharTXUUID = 'adaf0003-4369-7263-7569-74507974686e' ;
1415
1516const BYTES_PER_WRITE = 20 ;
1617
17- let btnRequestBluetoothDevice , btnBond , btnReconnect ;
18+ let btnRequestBluetoothDevice , btnReconnect ;
1819
1920class BLEWorkflow extends Workflow {
2021 constructor ( ) {
@@ -30,10 +31,9 @@ class BLEWorkflow extends Workflow {
3031 this . partialWrites = true ;
3132 this . type = CONNTYPE . Ble ;
3233 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 } ,
3737 ] ;
3838 }
3939
@@ -53,18 +53,15 @@ class BLEWorkflow extends Workflow {
5353 let p = this . connectDialog . open ( ) ;
5454 let modal = this . connectDialog . getModal ( ) ;
5555 btnRequestBluetoothDevice = modal . querySelector ( '#requestBluetoothDevice' ) ;
56- btnBond = modal . querySelector ( '#promptBond' ) ;
5756 btnReconnect = modal . querySelector ( '#bleReconnect' ) ;
5857
5958 // Map the button states to the buttons
6059 this . connectButtons = {
6160 reconnect : btnReconnect ,
62- request : btnRequestBluetoothDevice ,
63- bond : btnBond
61+ request : btnRequestBluetoothDevice
6462 } ;
6563
6664 btnRequestBluetoothDevice . addEventListener ( 'click' , this . onRequestBluetoothDeviceButtonClick . bind ( this ) ) ;
67- btnBond . addEventListener ( 'click' , this . onBond . bind ( this ) ) ;
6865 btnReconnect . addEventListener ( 'click' , this . reconnectButtonHandler . bind ( this ) ) ;
6966
7067 // Check if Web Bluetooth is available
@@ -74,7 +71,7 @@ class BLEWorkflow extends Workflow {
7471 stepOne . classList . add ( "hidden" ) ;
7572 }
7673 try {
77- const devices = await navigator . bluetooth . getDevices ( ) ;
74+ const devices = await bluetooth . getDevices ( ) ;
7875 console . log ( devices ) ;
7976 this . connectionStep ( devices . length > 0 ? 2 : 1 ) ;
8077 } catch ( e ) {
@@ -120,7 +117,7 @@ class BLEWorkflow extends Workflow {
120117 if ( ! this . connectionStatus ( ) ) {
121118 try {
122119 console . log ( 'Getting existing permitted Bluetooth devices...' ) ;
123- const devices = await navigator . bluetooth . getDevices ( ) ;
120+ const devices = await bluetooth . getDevices ( ) ;
124121
125122 console . log ( '> Found ' + devices . length + ' Bluetooth device(s).' ) ;
126123 // These devices may not be powered on or in range, so scan for
@@ -138,7 +135,7 @@ class BLEWorkflow extends Workflow {
138135
139136 // Bring up a dialog to request a device
140137 async requestDevice ( ) {
141- return navigator . bluetooth . requestDevice ( {
138+ return bluetooth . requestDevice ( {
142139 filters : [ { services : [ 0xfebb ] } , ] , // <- Prefer filters to save energy & show relevant devices.
143140 optionalServices : [ 0xfebb , bleNusServiceUUID ]
144141 } ) ;
@@ -153,11 +150,13 @@ class BLEWorkflow extends Workflow {
153150 abortController . abort ( ) ;
154151 console . log ( 'Connecting to GATT Server from "' + device . name + '"...' ) ;
155152 try {
156- await device . gatt . connect ( ) ;
153+ this . bleServer = await device . gatt . connect ( ) ;
157154 } catch ( error ) {
158155 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 ) ;
159158 }
160- if ( device . gatt . connected ) {
159+ if ( this . bleServer && this . bleServer . connected ) {
161160 console . log ( '> Bluetooth device "' + device . name + ' connected.' ) ;
162161 await this . switchToDevice ( device ) ;
163162 } else {
@@ -171,6 +170,7 @@ class BLEWorkflow extends Workflow {
171170 this . debugLog ( "connecting to " + device . name ) ;
172171 try {
173172 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' ) ;
174174 await device . watchAdvertisements ( { signal : abortController . signal } ) ;
175175 }
176176 catch ( error ) {
@@ -187,9 +187,7 @@ class BLEWorkflow extends Workflow {
187187 let device = await this . requestDevice ( ) ;
188188
189189 console . log ( '> Requested ' + device . name ) ;
190- await device . gatt . connect ( ) ;
191-
192- await this . switchToDevice ( device ) ;
190+ await this . connectToBluetoothDevice ( device ) ;
193191 /*}
194192 catch (error) {
195193 console.error(error);
@@ -203,7 +201,7 @@ class BLEWorkflow extends Workflow {
203201 this . bleDevice = device ;
204202 this . bleDevice . removeEventListener ( "gattserverdisconnected" , this . onDisconnected . bind ( this ) ) ;
205203 this . bleDevice . addEventListener ( "gattserverdisconnected" , this . onDisconnected . bind ( this ) ) ;
206- this . bleServer = this . bleDevice . gatt ;
204+ // this.bleServer = this.bleDevice.gatt;
207205 console . log ( "connected" , this . bleServer ) ;
208206 let services ;
209207
@@ -220,26 +218,11 @@ class BLEWorkflow extends Workflow {
220218 await this . fileHelper . bond ( ) ;
221219 await this . connectToSerial ( ) ;
222220
223- // Enable/Disable UI buttons
224- this . connectionStep ( 3 ) ;
225-
226221 await this . onConnected ( ) ;
227222 this . connectDialog . close ( ) ;
228223 await this . loadEditor ( ) ;
229224 }
230225
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-
243226 async serialTransmit ( msg ) {
244227 if ( this . rxCharacteristic ) {
245228 let encoder = new TextEncoder ( ) ;
@@ -272,17 +255,11 @@ class BLEWorkflow extends Workflow {
272255 }
273256 // Is this a new connection?
274257 if ( ! this . bleDevice ) {
275- let devices = await navigator . bluetooth . getDevices ( ) ;
258+ let devices = await bluetooth . getDevices ( ) ;
276259 for ( const device of devices ) {
277260 await this . connectToBluetoothDevice ( device ) ;
278261 }
279262 }
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- }
286263 }
287264
288265 updateConnected ( connectionState ) {
0 commit comments