77import json
88import logging
99import os
10+ import platform
1011import struct
1112import sys
1213from typing import Dict , Tuple
@@ -275,6 +276,7 @@ class BootloaderConnection(BLERequestsConnection):
275276 def __init__ (self ):
276277 """Initialize the BLE Connection for Bootloader service."""
277278 super ().__init__ ('00001626-1212-efde-1623-785feabcd123' )
279+ self .ignore_erase_reply = False
278280
279281 async def bootloader_request (self , request , payload = None , timeout = None ):
280282 """Sends a message to the bootloader and awaits corresponding reply."""
@@ -292,6 +294,9 @@ async def bootloader_request(self, request, payload=None, timeout=None):
292294 if request .reply_len > 0 :
293295 logger .debug ("Awaiting reply" )
294296 reply = await self .wait_for_reply (timeout )
297+ # Windows may receive reply from erase command at the wrong time
298+ if self .ignore_erase_reply and reply [0 ] == 0x11 :
299+ reply = await self .wait_for_reply (timeout )
295300 return request .parse_reply (reply )
296301
297302 async def flash (self , firmware , metadata ):
@@ -320,14 +325,19 @@ async def flash(self, firmware, metadata):
320325 # Erase existing firmware
321326 logger .debug ("Erasing flash." )
322327 try :
328+ # Windows sometimes doesn't receive the reply to this command at all
329+ # or until another command is sent (buggy Bluetooth drivers?) so we
330+ # have a few hacks to special case this. City hub further complicates
331+ # things by having a buggy Bluetooth implementation in its bootloader.
323332 response = await self .bootloader_request (
324333 self .ERASE_FLASH_CITY_HUB
325- if info .type_id == HubTypeId .CITY_HUB
334+ if info .type_id == HubTypeId .CITY_HUB and not platform . system () == "Windows"
326335 else self .ERASE_FLASH ,
327336 timeout = 5
328337 )
329338 logger .debug (response )
330339 except asyncio .TimeoutError :
340+ self .ignore_erase_reply = True
331341 logger .info ("did not receive erase reply, continuing anyway" )
332342
333343 # Get the bootloader ready to accept the firmware
0 commit comments