繁体   English   中英

React-Native的监控服务和特性

[英]Monitoring Services And Characteristics on React-Native

关于我的代码,我有两个问题:)

我正在创建一个连接到ble设备的android。 我正在使用ble-plx库。

import React, {Component} from 'react';
import { Platform, View, Text } from 'react-native';
import { BleManager } from 'react-native-ble-plx';

export default class Main extends Component {
  constructor() {
    super();
    this.manager = new BleManager()
    this.state = {info: "", values: {}}
    this.deviceprefix = "Device";
    this.devicesuffix_dx = "DX";
    this.sensors = {
      "0000f0fd-0001-0008-0000-0805f9b34fb0" : "Acc+Gyr+Mg",
      "0000f0f4-0000-1000-8000-00805f9b34fb" : "Pressure"
    }

    }

  serviceUUID() {
      return "0000f0f0-0000-1000-8000-00805f9b34fb"
    }

  notifyUUID(num) {
      return num 
    }

  model_dx (model) {
      return this.deviceprefix + model + this.devicesuffix_dx
    }

  info(message) {
      this.setState({info: message})
    }

  error(message) {
      this.setState({info: "ERROR: " + message})
    }

  updateValue(key, value) {
      this.setState({values: {...this.state.values, [key]: value}})
    }

  componentWillMount(){
    const subscription = this.manager.onStateChange((state) => {
      if (state === 'PoweredOn') {
        this.scanAndConnect();
        subscription.remove();
      }
    }, true);
  }
  async requestPermission() {
    try {
      const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);

      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        this.setState({permissionStatus:'granted'});
      }else if(granted === PermissionsAndroid.RESULTS.DENIED) {
        this.setState({permissionStatus:'denied'});
      }else{

      }
    } catch (err) {
      console.error(err)
    }
  }

    scanAndConnect(){
      this.manager.startDeviceScan(null, null, (error, device) => {
        if (error) {

          return
        }

        let model = '0030';
        if (device.name == this.model_dx(model)  ) {
          this.manager.stopDeviceScan();
          device.connect()
            .then((device) => {
              this.info("Discovering services and characteristics ")
              return device.discoverAllServicesAndCharacteristics()
            })
           .then((device) => {
             this.info("SetupNotification")
             return this.setupNotifications(device)
           })
            .catch((error) => {
              this.error(error.message)
            });
        }

      });
    }


    async setupNotifications(device) {
        for (const id in this.sensors) {

          const service = this.serviceUUID(id)
          const characteristicN = this.notifyUUID(id)

          device.monitorCharacteristicForService(service, characteristicN, (error, characteristic) => {
            if (error) {
              this.error(error.message)
              return
            }
            this.updateValue(characteristic.uuid, characteristic.value)
          })
        }
      }

    render() {
       return (
         <View>
           <Text>{this.state.info}</Text>
           {Object.keys(this.sensors).map((key) => {
          return <Text key={key}>
                   {this.sensors[key] + ": " + (this.state.values[this.notifyUUID(key)] || "-")}
                 </Text>
        })}
         </View>
       )
     }

}

我有两个问题要问你:

1)监控给我的价值如下:

  • 例如,在“压力”的情况下,值:“ZABIAGYAZwBoAA ==”我应该收到一个数字而不是字符串,我试图将文本转换为二进制,然后二进制转换为十进制。 在您认为应用中,我该怎么办?

2)我使用以下方法打印值:

<Text>{this.state.info}</Text>
               {Object.keys(this.sensors).map((key) => {
              return <Text key={key}>
                       {this.sensors[key] + ": " + (this.state.values[this.notifyUUID(key)] || "-")}
                     </Text>

但如果我想直接获取“压力”值,我该怎么办呢? 要清楚,如果我想,例如,将记住我的压力传递给另一页? 谢谢大家的支持!

安装这个库:

npm install --save base-64

在您的Main类中导入它:

从'base-64'导入base64

然后在你的updateValue方法中:

updateValue(key, value) {
    const readebleData = base64.decode(value);

    this.setState({values: {...this.state.values, [key]: readebleData}})
}

如果你想将readableData传递给另一个类,你应该使用像react-redux这样的库,或者在转换到新类时传递变量,例如使用react-native-router-flux

Actions.pageKey({data:readebleData})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM