mirror of
https://github.com/thegecko/web-bluetooth-dfu.git
synced 2026-05-09 00:34:38 +08:00
Merge pull request #29 from fhaubold/delay
Optional delay between sending packets
This commit is contained in:
@@ -148,8 +148,11 @@ export class SecureDfu extends EventDispatcher {
|
||||
* Characteristic constructor
|
||||
* @param bluetooth A bluetooth instance
|
||||
* @param crc32 A CRC32 function
|
||||
* @param delay Milliseconds of delay between packets
|
||||
*/
|
||||
constructor(private crc32: (data: Array<number> | Uint8Array, seed?: number) => number, private bluetooth?: Bluetooth) {
|
||||
constructor(private crc32: (data: Array<number> | Uint8Array, seed?: number) => number,
|
||||
private bluetooth?: Bluetooth,
|
||||
private delay: number = 0) {
|
||||
super();
|
||||
|
||||
if (!this.bluetooth && window && window.navigator && window.navigator.bluetooth) {
|
||||
@@ -361,6 +364,7 @@ export class SecureDfu extends EventDispatcher {
|
||||
const packet = data.slice(start, end);
|
||||
|
||||
return this.packetChar.writeValue(packet)
|
||||
.then(() => this.delayPromise(this.delay))
|
||||
.then(() => {
|
||||
this.progress(offset + end);
|
||||
|
||||
@@ -379,6 +383,12 @@ export class SecureDfu extends EventDispatcher {
|
||||
return crc === this.crc32(new Uint8Array(buffer));
|
||||
}
|
||||
|
||||
private delayPromise(delay: number) {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(resolve, delay);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scans for a device to update
|
||||
* @param buttonLess Scans for all devices and will automatically call `setDfuMode`
|
||||
|
||||
Reference in New Issue
Block a user