1
\$\begingroup\$

I have two questions for you about my code :)

I'm creating an android to connect to ble device. I'm using ble-plx library.

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>
       )
     }

}

I have two question for you:

1) The monitoring give me values like:

  • For example in the case of "Pressure" the value: "ZABIAGYAZwBoAA==" I should receive a number not string, how it is possible?

2) I print the values using:

<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>

But If I wanted to access the "pressure" value directly, how could I do it? To be clear if I wanted to, for example, passing the value that remembers me pressure on another page? Thank you all for the support!

\$\endgroup\$

closed as off-topic by yuri, Mathias Ettinger, IEatBagels, Toby Speight, Pieter Witvoet May 29 at 9:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – yuri, Mathias Ettinger, IEatBagels, Toby Speight, Pieter Witvoet
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Welcome to Code Review! Since your code does not yet accomplish what you intend to do with it, it is not ready to be reviewed. Please fix the remaining issues first, then come back to ask for possible improvements. \$\endgroup\$ – AlexV May 28 at 13:33
  • \$\begingroup\$ @AlexV I think that I have write all :) \$\endgroup\$ – Mr Robinson May 28 at 13:41