如何知道 monitorCharacteristic 何时完成 react native ble plx?
How to know when monitorCharacteristic is done with react native ble plx?
我在我的项目中使用 react-native-ble-plx 来与我的工具进行通信,我正在尝试监控它的特性之一。
结果相当长,因此 monitoringCharacteristic 函数一直在循环,直到它向我发送所有内容,但我不知道如何确保循环完成。
这是我的监控功能:
const scanMyDevice = async (device) => {
const serviceUUID = '569a1-****'
const writeUUID = '569a2-****'
const readUUID = '569a2-****'
await device.discoverAllServicesAndCharacteristics()
await device.characteristicsForService(serviceUUID)
await device.writeCharacteristicWithResponseForService(serviceUUID, writeUUID, 'IzEwCg==')
var tempTrame = ''
const subscription = device.monitorCharacteristicForService(serviceUUID, readUUID, (error, a) => {
if (error) {
console.log(error)
console.log(error.message)
}
else {
tempTrame = tempTrame + base64.decode(a.value)
setTrameResult(tempTrame)
console.log('// INSIDE ///', tempTrame)
}
}, 'idMonitor')
return () => {
subscription.remove();
}
console.log('DONE')
}
在我的代码中,'DONE' 将在 'INSIDE trame' 之前打印。
我试图将 console.log('DONE')
放在 return
中,但它从未被打印出来。
我试图将 .then( console.log('DONE'))
放在 return
之前,但它向我发送了一条错误消息,说它与此处无关...
有人可以帮我吗?
最后,我通过 BLE 为我想要 send/read 的信息定义了一个结束模式,从而解决了这个问题,然后我编写了一个循环以在未遇到结束模式时继续阅读:
let tempTrame = tempTrame + base64.decode(a.value)
let finalTrame = ''
// check if my variable has the end symbols (to be sure that I have all the information) and if it is the first time I get this information
if (tempTrame.includes('#109F')) {
stop = true
// suppress end symbols
tempTrame = tempTrame.replace(endTrame, '')
finalTrame = tempTrame
}
console.log(finalTrame)
我在我的项目中使用 react-native-ble-plx 来与我的工具进行通信,我正在尝试监控它的特性之一。
结果相当长,因此 monitoringCharacteristic 函数一直在循环,直到它向我发送所有内容,但我不知道如何确保循环完成。
这是我的监控功能:
const scanMyDevice = async (device) => {
const serviceUUID = '569a1-****'
const writeUUID = '569a2-****'
const readUUID = '569a2-****'
await device.discoverAllServicesAndCharacteristics()
await device.characteristicsForService(serviceUUID)
await device.writeCharacteristicWithResponseForService(serviceUUID, writeUUID, 'IzEwCg==')
var tempTrame = ''
const subscription = device.monitorCharacteristicForService(serviceUUID, readUUID, (error, a) => {
if (error) {
console.log(error)
console.log(error.message)
}
else {
tempTrame = tempTrame + base64.decode(a.value)
setTrameResult(tempTrame)
console.log('// INSIDE ///', tempTrame)
}
}, 'idMonitor')
return () => {
subscription.remove();
}
console.log('DONE')
}
在我的代码中,'DONE' 将在 'INSIDE trame' 之前打印。
我试图将 console.log('DONE')
放在 return
中,但它从未被打印出来。
我试图将 .then( console.log('DONE'))
放在 return
之前,但它向我发送了一条错误消息,说它与此处无关...
有人可以帮我吗?
最后,我通过 BLE 为我想要 send/read 的信息定义了一个结束模式,从而解决了这个问题,然后我编写了一个循环以在未遇到结束模式时继续阅读:
let tempTrame = tempTrame + base64.decode(a.value)
let finalTrame = ''
// check if my variable has the end symbols (to be sure that I have all the information) and if it is the first time I get this information
if (tempTrame.includes('#109F')) {
stop = true
// suppress end symbols
tempTrame = tempTrame.replace(endTrame, '')
finalTrame = tempTrame
}
console.log(finalTrame)