0

I am a little confused on how Data object works in swift. I am attempting to write values using bluetooth and one of the values I need to write is 500. In order to write you need to convert the value to a data object before sending. Here's the code I am using.

if(characteristic != nil){
   var byteCount = 1
   if(sensitivity > 255){
      byteCount = 2
   }
   let data = Data.init(bytes: &sensitivity, count: byteCount)
   peripheral.writeValue(data, for: sensCharacteristic!, type: CBCharacteristicWriteType.withResponse)
   peripheral.readValue(for: sensCharacteristic!)
}else{
   print("No bluetooth Connection")
}

From what I understood if I try to send a value that takes more than a byte to represent then it will overflow, thus when I send 500 the value it writes is actually 244. Because of this I tried writing and sending 2 bytes but the value I am getting is 244101. I am unsure where that value is coming from. What is the correct way to covert 500?

This function is called whenever a characteristic is read and then placed in a label. All it does is loop through the data and adds each byte to a string.

func readCharacteristic(data: Data) -> String {
    var characterString = ""
    for byte in data {
        let c = String(byte)
        characterString.append(c)
    }
    return characterString
}
shallowThought
  • 19,212
  • 9
  • 65
  • 112
Ubarjohade
  • 471
  • 6
  • 21
  • Where are you seeing `244101`? What data type is `sensitivity`? Is your peripheral expecting just a single byte of data, or can it accept multiple bytes? – DonMag Jun 23 '17 at 15:43
  • Right after the writeValue I have a readValue of the same characteristic, I'll add the code for that. Sensitivity is just an int that can have the values 4, 13, 23 ... 230, 500. Each characteristic has a maximum size of 20 bytes on this peripheral, do devices typically require you to only write 1 byte at a time? – Ubarjohade Jun 23 '17 at 15:49
  • 1
    prpbably you can find answers [here](https://stackoverflow.com/questions/38023838/round-trip-swift-number-types-to-from-data) – Willjay Jun 23 '17 at 15:49
  • Read that thread, might of missed something though – Ubarjohade Jun 23 '17 at 15:58
  • OK - the problem is almost certainly how you are converting the data you read back... Are you expecting a string? – DonMag Jun 23 '17 at 16:06
  • Typically the data I read back from another characteristic is a Hex value similar to BF314F64641, I just loop through each byte and then treat it as a string as I have to do some manipulation to format it correctly and when I made it I couldn't find the correct way to parse the value. – Ubarjohade Jun 23 '17 at 17:23

1 Answers1

1

Based on this SO post: round trip Swift number types to/from Data (pointed to by Wei Jay)...

Try this:

func readCharacteristic(data: Data) -> Int {
    return data.withUnsafeBytes { $0.pointee }
}

Note: that post provides various ways to convert the Data to different data types - Int, Float, Double, etc - so this could be used if you know you'll only be reading an Int value. Otherwise, just build on this + the other post.

DonMag
  • 69,424
  • 5
  • 50
  • 86