Skip to content

Commit 2a4ffcf

Browse files
dugganbwp91
authored andcommitted
add post-write delay and fix debug log formatting
Restore the 100ms delay after BLE writes that was removed in 14f3bd8. With write-without-response mode, the BLE controller needs time to transmit the packet before the connection is torn down. Also fix logDebug calls that used printf-style %s placeholders, which don't work since logDebug only accepts a single string argument.
1 parent 6150c3d commit 2a4ffcf

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/connection/ble.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import process from 'node:process'
44
import btClient from '@stoprocent/noble'
55

66
import { decodeAny } from '../utils/decode.js'
7-
import { base64ToHex, generateCodeFromHexValues, hexToTwoItems } from '../utils/functions.js'
7+
import { base64ToHex, generateCodeFromHexValues, hexToTwoItems, sleep } from '../utils/functions.js'
88
import platformLang from '../utils/lang-en.js'
99
import { isValidPeripheral } from '../utils/validation.js'
1010

@@ -222,7 +222,7 @@ export default class BLEConnection {
222222
btClient.reset()
223223

224224
// Connect with timeout
225-
accessory.logDebug('connecting to device at %s', accessory.context.bleAddress)
225+
accessory.logDebug(`connecting to device at ${accessory.context.bleAddress}`)
226226
peripheral = await this.connectWithTimeout(accessory.context.bleAddress, CONNECTION_TIMEOUT)
227227
this.activeConnection = peripheral
228228
accessory.logDebug('connected successfully')
@@ -247,9 +247,10 @@ export default class BLEConnection {
247247
const finalBuffer = this.prepareCommandBuffer(params)
248248
accessory.logDebug(`sending command: ${finalBuffer.toString('hex')}`)
249249

250-
// Write with timeout
250+
// Write without response then allow time for the BLE controller to transmit before disconnecting
251251
await this.writeWithTimeout(characteristic, finalBuffer, WRITE_TIMEOUT)
252252
accessory.logDebug('command sent successfully')
253+
await sleep(100)
253254
} catch (err) {
254255
accessory.logWarn(`BLE update failed: ${err.message}`)
255256
throw err
@@ -265,7 +266,7 @@ export default class BLEConnection {
265266
await peripheral.disconnectAsync()
266267
accessory.logDebug('disconnected')
267268
} catch (err) {
268-
accessory.logDebug('disconnect error (non-critical): %s', err.message)
269+
accessory.logDebug(`disconnect error (non-critical): ${err.message}`)
269270
}
270271
}
271272

0 commit comments

Comments
 (0)