Skip to content

Commit

Permalink
fix |> fix ble disconnect error
Browse files Browse the repository at this point in the history
  • Loading branch information
htoooth committed May 16, 2024
1 parent 847b7d4 commit 4e37ca9
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/shared/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ export class MessageBuilder extends EventBus {
this.on('response', (resp) => {
this.onResponse(resp)
})

ble?.addListener((status) => {
DEBUG && logger.debug('ble change to=>', status)

if (!status) {
logger.error('ble disconnect')
this.shakeTask.reject(
new MessageError(MessageErrorCode.BLE_CLOSE, 'ble disconnect'),
)
this.shakeStatus = ShakeStatus.failure
}
})
}

fork(timeout = MessageShakeTimeOut) {
Expand All @@ -308,6 +320,17 @@ export class MessageBuilder extends EventBus {
)
}, timeout)

try {
this.errorIfBleDisconnect()
} catch (error) {
logger.error('error ble disconnect %j', error)
this.shakeTask.reject(
new MessageError(MessageErrorCode.BLE_CLOSE, 'ble disconnect'),
)
this.shakeStatus = ShakeStatus.failure
return this.waitingShakePromise
}

this.shakeStatus = ShakeStatus.pending
this.sendShake()

Expand Down Expand Up @@ -444,7 +467,7 @@ export class MessageBuilder extends EventBus {
}

sendShake() {
DEBUG && logger.info('shake send')
DEBUG && logger.debug('shake send')
const shake = this.buildShake()
this.sendMsg(shake)
}
Expand Down Expand Up @@ -967,6 +990,7 @@ export class MessageBuilder extends EventBus {
}

const isBleConnected = this.ble.connectStatus()
DEBUG && logger.debug('isBleConnected=>', isBleConnected)

if (!isBleConnected) {
throw new MessageError(MessageErrorCode.BLE_CLOSE, 'ble disconnect')
Expand Down Expand Up @@ -1057,13 +1081,16 @@ export class MessageBuilder extends EventBus {
try {
this.errorIfBleDisconnect()
} catch (error) {
logger.error('error ble disconnect %j', error)
return Promise.reject(error)
}

const requestTask = () => {
this.errorIfBleDisconnect()
this.errorIfSideServiceDisconnect()

DEBUG && logger.debug('request start %j', data)

let contentType = DataType.bin

if (typeof data === 'string') {
Expand Down

0 comments on commit 4e37ca9

Please sign in to comment.