如何在NativeAppEventEmitter中设置状态?

时间:2019-05-24 05:40:39

标签: reactjs react-native

我正在从BLE中检索某些数据,但无法将其设置为状态以将其显示在组件上。我使用了注册通知功能来发送请求,而我正在使用NativeAppEventEmitter检索返回的数据。我使用该数据,对其进行处理,然后尝试将其设置为一种状态,以便将其加载到组件上。

BleHelper.registerNotification(peripheral.id, xxxx, xxxx, this._onCharValueUpdate)
                .then(handle => {
                      NativeAppEventEmitter.addListener('BleManagerDidUpdateValueForCharacteristic', function(data){ 
                        console.log(data.value);
                       // let x = new Uint8Array([-2])
                        let buffer = Buffer.from([254]);
                        let exponent = buffer.readInt8(0);
                        console.log("before");

                        let temp= data.value[3] << (16)| data.value[2]<<(8)|data.value[1];

                        console.log("after");
                        console.log(temp*(Math.pow(10,exponent)));
                       let temperature = temp*(Math.pow(10,exponent));

                       this.setState({
                        temperature:temperature
                    });                    });


                    this.notifyHandle = handle;
                    this.forceUpdate();
                }
                )
                .catch(err => {
                    console.warn(err)
                    //ErrorRegistry.putError('GATT Register Notification', err);
                });
        } ```

0 个答案:

没有答案
相关问题