From 0a50ebddd7a9b637cf78da8b538586496fa4d5f9 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 1 May 2020 21:11:01 -0500 Subject: [PATCH] bootloader: add withResponse option This takes advantage of new (not implemented yet) Web Bluetooth APIs to write with or without response --- src/actions/bootloader.ts | 8 ++++++-- src/services/bootloader.ts | 12 +++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/actions/bootloader.ts b/src/actions/bootloader.ts index 0823c22b..9ef6747a 100644 --- a/src/actions/bootloader.ts +++ b/src/actions/bootloader.ts @@ -75,10 +75,14 @@ export function didError(err: Error): BootloaderConnectionDidErrorAction { export interface BootloaderConnectionSendAction extends Action { readonly data: ArrayBuffer; + readonly withResponse: boolean; } -export function send(data: ArrayBuffer): BootloaderConnectionSendAction { - return { type: BootloaderConnectionActionType.Send, data }; +export function send( + data: ArrayBuffer, + withResponse = false, +): BootloaderConnectionSendAction { + return { type: BootloaderConnectionActionType.Send, data, withResponse }; } export interface BootloaderConnectionDidSendAction diff --git a/src/services/bootloader.ts b/src/services/bootloader.ts index 674e5f74..3cf994d5 100644 --- a/src/services/bootloader.ts +++ b/src/services/bootloader.ts @@ -81,7 +81,17 @@ async function send(action: Action, dispatch: Dispatch): Promise { if (!char) { throw Error('Not connected'); } - await char.writeValue((action as BootloaderConnectionSendAction).data); + const sendAction = action as BootloaderConnectionSendAction; + // Fall back to legacy WebBluetooth writeValue if new methods are not + // available. + const writeValue = sendAction.withResponse + ? // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // @ts-ignore + char.writeValueWithResponse || char.writeValue + : // eslint-disable-next-line @typescript-eslint/ban-ts-ignore + // @ts-ignore + char.writeValueWithoutResponse || char.writeValue; + await writeValue(sendAction.data); dispatch(didSend()); } catch (err) { dispatch(didSend(err));