我使用 react-native-ble-plx 库成功地从 BLE 设备接收和读取数据。
侦听器每 10 毫秒或 20 毫秒接收一次数据,但之后 UI 冻结约 1 分钟。
处理这种情况的正确方法是什么?
class DataReceiver extends React.Component {
...
characteristicListener = async (error, characteristic) => {
if (error) {
// handle error
} else {
// read data
//parse data/
// add new data to data object
}
};
startReadingData = async () => {
...
action.monitorCharacteristic({
serviceUUID: SERVICE_UUID,
characteristicUUID: CHAR_UUID,
listener: this.characteristicListener,
});
...
}
render() {
...
}
}
function mapStateToProps(state) {
return {
...
};
}
function mapDispatchToProps(dispatch) {
return {
action: bindActionCreators(
{
writeCharacteristic,
monitorCharacteristic,
readCharacteristic,
},
dispatch,
),
dispatch,
};
}
export default connect(mapStateToProps, mapDispatchToProps)(DataReceiver);