Mr Robinson
Mr Robinson

Reputation: 71

Covert Values to Int

I'm creating an Android App that takes data from a ble device. I have only a problem, the data that my ble device give to me are like:

Accelerometer + Gyroscope: "zLAi/wD/SpwiAOv/VwBnAML/xQE="

Pressure: "bgBvAHAAcQByAA="

Battery: "ChYKARoA"

While should have number, how can I convert these values??

For Example I have this function to manageCharacteristics

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

      const service = this.serviceUUID(id)
      const characteristicAccGyrMg = this.notifyUUIDAccGyrMg(id)


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

And this is updateValue:

updateValue(key, value) {
  //        const readebleData = base64.decode(value);
    this.setState({ values: { ...this.state.values, [key]: value } })
  }

I have try to use base64.decode(value) but the values that I receive are not number :/

Upvotes: 1

Views: 69

Answers (1)

Leftium
Leftium

Reputation: 17903

Apparently those values are hex encoded into base64. I could not find any documentation, but it's based on this Github issue: monitorCharacteristicForService value is fragmented

I used this tool to convert your base64 to hex:

Accelerometer + Gyroscope: "zLAi/wD/SpwiAOv/VwBnAML/xQE="

cc b0 22 ff 00 ff 4a 9c 22 00 eb ff 57 00 67 00 c2 ff c5 01

Pressure: "bgBvAHAAcQByAA="

6e 00 6f 00 70 00 71 00 72 00

Battery: "ChYKARoA"

0a 16 0a 01 1a 00

Unfortunately, without knowing the specifications for this device, it is impossible to convert these hex values to numbers. The manufacturer could have chosen from an infinite number of methods to map numbers to hex values. You will either have to find the protocol documentation or reverse engineer it.

Here are samples of how another BLE device converted numbers to hex (search for "notification consists"). You will find sections like this:

The notification consists of 6 byte:

byte 0: 02
byte 1: 01
byte 2: XY (see below)
byte 3: valve open state in % (from 0x00 to 0x64)
byte 4: undefined (battery level?)
byte 5: (temperature * 2)

Upvotes: 1

Related Questions