@@ -7,6 +7,7 @@ import "./demo.css";
77import  {  MicrobitWebUSBConnection  }  from  "../lib/usb" ; 
88import  {  HexFlashDataSource  }  from  "../lib/hex-flash-data-source" ; 
99import  { 
10+   BackgroundErrorEvent , 
1011  ConnectionStatus , 
1112  ConnectionStatusEvent , 
1213  DeviceConnection , 
@@ -79,29 +80,35 @@ const displayStatus = (status: ConnectionStatus) => {
7980const  handleDisplayStatusChange  =  ( event : ConnectionStatusEvent )  =>  { 
8081  displayStatus ( event . status ) ; 
8182} ; 
82- const  initConnectionStatusDisplay  =  ( )  =>  { 
83+ const  backgroundErrorListener  =  ( event : BackgroundErrorEvent )  =>  { 
84+   console . error ( "Handled error:" ,  event . errorMessage ) ; 
85+ } ; 
86+ 
87+ const  initConnectionListeners  =  ( )  =>  { 
8388  displayStatus ( connection . status ) ; 
8489  connection . addEventListener ( "status" ,  handleDisplayStatusChange ) ; 
90+   connection . addEventListener ( "backgrounderror" ,  backgroundErrorListener ) ; 
8591} ; 
8692
8793let  connection : DeviceConnection  =  new  MicrobitWebUSBConnection ( ) ; 
8894
89- initConnectionStatusDisplay ( ) ; 
95+ initConnectionListeners ( ) ; 
9096
9197const  switchTransport  =  async  ( )  =>  { 
9298  await  connection . disconnect ( ) ; 
9399  connection . dispose ( ) ; 
94100  connection . removeEventListener ( "status" ,  handleDisplayStatusChange ) ; 
101+   connection . removeEventListener ( "backgrounderror" ,  backgroundErrorListener ) ; 
95102
96103  switch  ( transport . value )  { 
97104    case  "bluetooth" : { 
98105      connection  =  new  MicrobitWebBluetoothConnection ( ) ; 
99-       initConnectionStatusDisplay ( ) ; 
106+       initConnectionListeners ( ) ; 
100107      break ; 
101108    } 
102109    case  "usb" : { 
103110      connection  =  new  MicrobitWebUSBConnection ( ) ; 
104-       initConnectionStatusDisplay ( ) ; 
111+       initConnectionListeners ( ) ; 
105112      break ; 
106113    } 
107114  } 
@@ -120,23 +127,14 @@ flash.addEventListener("click", async () => {
120127  const  file  =  fileInput . files ?. item ( 0 ) ; 
121128  if  ( file )  { 
122129    const  text  =  await  file . text ( ) ; 
123-     await  connection . flash ( new  HexFlashDataSource ( text ) ,  { 
124-       partial : true , 
125-       progress : ( percentage : number  |  undefined )  =>  { 
126-         console . log ( percentage ) ; 
127-       } , 
128-     } ) ; 
129-   } 
130- } ) ; 
131- 
132- accDataGet . addEventListener ( "click" ,  async  ( )  =>  { 
133-   if  ( connection  instanceof  MicrobitWebBluetoothConnection )  { 
134-     const  data  =  await  connection . getAccelerometerData ( ) ; 
135-     console . log ( "Get accelerometer data" ,  data ) ; 
136-   }  else  { 
137-     throw  new  Error ( 
138-       "`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`" , 
139-     ) ; 
130+     if  ( connection . flash )  { 
131+       await  connection . flash ( new  HexFlashDataSource ( text ) ,  { 
132+         partial : true , 
133+         progress : ( percentage : number  |  undefined )  =>  { 
134+           console . log ( percentage ) ; 
135+         } , 
136+       } ) ; 
137+     } 
140138  } 
141139} ) ; 
142140
@@ -170,10 +168,29 @@ accDataStop.addEventListener("click", async () => {
170168  } 
171169} ) ; 
172170
171+ accDataGet . addEventListener ( "click" ,  async  ( )  =>  { 
172+   if  ( connection  instanceof  MicrobitWebBluetoothConnection )  { 
173+     try  { 
174+       const  data  =  await  connection . getAccelerometerData ( ) ; 
175+       console . log ( "Get accelerometer data" ,  data ) ; 
176+     }  catch  ( err )  { 
177+       console . error ( "Handled error:" ,  err ) ; 
178+     } 
179+   }  else  { 
180+     throw  new  Error ( 
181+       "`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`" , 
182+     ) ; 
183+   } 
184+ } ) ; 
185+ 
173186accPeriodGet . addEventListener ( "click" ,  async  ( )  =>  { 
174187  if  ( connection  instanceof  MicrobitWebBluetoothConnection )  { 
175-     const  period  =  await  connection . getAccelerometerPeriod ( ) ; 
176-     console . log ( "Get accelerometer period" ,  period ) ; 
188+     try  { 
189+       const  period  =  await  connection . getAccelerometerPeriod ( ) ; 
190+       console . log ( "Get accelerometer period" ,  period ) ; 
191+     }  catch  ( err )  { 
192+       console . error ( "Handled error:" ,  err ) ; 
193+     } 
177194  }  else  { 
178195    throw  new  Error ( 
179196      "`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`" , 
@@ -183,8 +200,12 @@ accPeriodGet.addEventListener("click", async () => {
183200
184201accPeriodSet . addEventListener ( "click" ,  async  ( )  =>  { 
185202  if  ( connection  instanceof  MicrobitWebBluetoothConnection )  { 
186-     const  period  =  parseInt ( accPeriodInput . value ) ; 
187-     await  connection . setAccelerometerPeriod ( period ) ; 
203+     try  { 
204+       const  period  =  parseInt ( accPeriodInput . value ) ; 
205+       await  connection . setAccelerometerPeriod ( period ) ; 
206+     }  catch  ( err )  { 
207+       console . error ( "Handled error:" ,  err ) ; 
208+     } 
188209  }  else  { 
189210    throw  new  Error ( 
190211      "`getAccelerometerData` is not supported on `MicrobitWebUSBConnection`" , 
0 commit comments