1

I am trying read the array data sent by Peripheral and parse it into Swift array form.

Peripheral Device is sending data for Example: Array[1 To 15] Numbers.(For full scale purpose this integer values range between 1000 to 2000). but for now i am trying to make it work with 1 to 15 Integer values. I was able to get the result in string format in encoded format. My Question is how can i get array 1 to 15 from Characteristic.value from didUpdateValueFor Method in swift.

Here is the code snippet for reference.

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

    if characteristic == rxCharacteristic {
        count += 1
        let data = characteristic.value
        print("Count: \(count), data: \(data! as NSData)")


    }
}

In X-Code console Printed result is:

Count: 2, data: <00000100 02>
Count: 3, data: <00030004 00050006 00070008 0009000a 000b000c>
Count: 4, data: <000d00>
Count: 5, data: <0e000f00>

Mehul D
  • 140
  • 2
  • 10
  • 1
    Instead print `print("Count: \(count), data: \(data as NSData)")`, and tell us what's supposed to be the value for `data` when used. Because that's strange output. Else, just use an array, and append the value each time. – Larme Nov 20 '18 at 18:11
  • Thanks @Larme for comment.Here is the output i am getting. Count: 2, data: <00000100 02> Count: 3, data: <00030004 00050006 00070008 0009000a 000b000c> Count: 4, data: <000d00> Count: 5, data: <0e000f00> – Mehul D Nov 20 '18 at 18:15
  • 1
    And what's the target result? Is it chunked data? Are you supposed to concatenate all the value and then "transform it"? – Larme Nov 20 '18 at 18:17
  • Target result to be displayed is in array format [1,2,3,...,15]. Yes you are right concatenate all the value and then "transform it". – Mehul D Nov 20 '18 at 18:20
  • @MehulD have a look at this https://stackoverflow.com/a/53405270/10661965 – iOS_Maccus Nov 21 '18 at 12:41

1 Answers1

0

Use below code for retrieving the Int values in the form of array.

Don't forget to reset count = 0 and values.removeAll() for data request.

I am assuming your characteristics.value is Int value

var values:[Int] = []
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

        if characteristic == rxCharacteristic {
            count += 1
            if let data = characteristic.value {
                var num = 0
                //Convert Data to Int
                data.getBytes(&num, length: MemoryLayout<Int>.size)
                values.append(num)
            }
            print("Count: \(count), data: \(data! as NSData)")
          }
    }
iOS_Maccus
  • 377
  • 1
  • 11
  • i tried this and as per my understanding num value never going to change it will remain 0. it's not calculating dynamically in your logic. – Mehul D Nov 21 '18 at 19:03
  • @MehulD, In my code I am reading the characteristic.value which is coming from peripheral. – iOS_Maccus Nov 22 '18 at 05:16