0

I am trying to figure out how to parse the value from a Bluetooth device with a characteristic which has more than 20 bytes in its value.

I have the code below which basically fires each time the characteristic value is updated. However, because the total amount of values is long, it seems that the didUpdateValueFor function seems to put out 4 or 5 notifications before I get the total amount of data. At the moment, I am looping through the value and appending to an array which I will use to graph some data. Is there a way to get all of the data into one array without having to do this loop each time? At the moment the data I am getting is as follows:

[89, 0, 44, 0, 84, 0, 112, 0, 125, 0, 125, 0, 134, 0, 144, 0, 153, 0, 162, 0]

[89, 1, 163, 0, 163, 0, 162, 0, 167, 0, 169, 0, 172, 0, 176, 0, 176, 0, 177, 0]

[89, 2, 182, 0, 181, 0, 181, 0, 182, 0, 181, 0, 169, 0, 164, 0, 157, 0, 139, 0]

[89, 3, 135, 0, 119, 0, 117, 0, 101, 0, 101, 0, 84, 0, 85, 0, 70, 0, 70, 0]

[84, 4, 55, 0, 46, 0, 26, 0, 21, 0]

The first value in each of the arrays is not important. However, the second value is a MS value and should be zero. However, each time the loop runs it seems to increment by one each time. Because I need to multiply this number by 256 (should the value of the LS value exceed 256), I am going to run into trouble if I simply ignore it (which is what my code does now as published below)

if characteristic.uuid == IDENTIFIER {

                let v = characteristic.value;
                print("this is what we have: \(value)")

                //let numDataPoints = v![0] & 0x0f
                let numDataPoints = v![0]

                print("Number of data points:\(numDataPoints)")

                for i in 1...v!.count-2 {

                    intIndex = Int(i)
                    let datapointnew = Int(v![intIndex!])
                    if datapointnew > 5 {
                         datapointArray.append(datapointnew)
                    } else {
                         datapointArray.append(0)
                    }


                }

                intIndex = 0  
        }

The second value in each of the array should be 0 with this set of data. Thanks in advance for any help you can offer.

5
  • Just for clarification: v is an example of one of the 5 arrays mentioned in your question? Commented Apr 14, 2019 at 1:25
  • Yes, that is correct. Commented Apr 14, 2019 at 1:57
  • Got it figured out now. Thank you all. Commented Apr 14, 2019 at 2:16
  • Maybe you could post your own answer in order to benefit others who come across this problem. Commented Apr 14, 2019 at 14:42
  • Certainly, the issue was that the first value in each array was an MS Nib and an LS Nib. I was reading the manufacturer's reference guide incorrectly. The second value was a sequence number. After that, the remaining values were LS, MS repeating in that order. Once I figured this out (the developer helped me), I was good. Hope this helps someone. Commented Apr 15, 2019 at 2:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.