skip to Main Content

I am sure this has been answered somewhere, but I am not sure what to search for. I am in the early stages of a simple BLE iOS app and trying to work out how to transfer the incoming BLE data to a separate function. The data incoming from BLE is converting into an array, but I believe it’s not possible to send this to a different function. I try to send it as a string, but then it seems impossible to break the string into an array.

Could someone advice what is the most efficient way to transfer the incoming BLE data to a separate functions?

This is the code in the func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?):

// Decode the incoming BLE into bytes
let data2 = characteristic.value
let count = (data2?.count)! / MemoryLayout<UInt8>.size
var array = [UInt8](repeating: 0, count: count)
data?.copyBytes(to: &array, count:count * MemoryLayout<UInt8>.size)
// Transfer the incoming BLE data to a sepurate class to decode
var stringFromData = String(data: characteristic.value!, encoding: String.Encoding.ascii)
// How to transfer the incoming BLE data to a sepurate class?
BLE_Element_One_DataExtract_class.DummyFunc(_IncomingElementOneData: stringFromData!, _IncomingUUID: characteristic.uuid)

print("BLE incoming data in an array = ")
let x=0
for x in array
{
    print("(array) BLE data n")
    
}
// Separate class and function to decode the BLE data and store in the MVVM
// This code does not work, as it does not treat the incoming data as a string 

class BLE_Element_One_DataExtract
{
    func DummyFunc(_IncomingElementOneData:String, _IncomingUUID:CBUUID)
    {
        let data
        let data2 = _IncomingElementOneData
        let count = (data2?.count)! / MemoryLayout<UInt8>.size
        var array = [UInt8](repeating: 0, count: count)
        data?.copyBytes(to: &array, count:count * MemoryLayout<UInt8>.size)
        
        print("data is -> : ", terminator : "")
        
        if(_IncomingUUID.isEqual(CBUUIDs.ECHO_DISPLAY_ELEMENT_STATUS_ONE_UUID_CHAR))
        {
            print ("Found ECHO_DISPLAY_ELEMENT_STATUS_ONE_UUID_CHAR inner func")
            // BLE_Element_One_DataExtract(?? send data string ??)
        }
    }
}

2

Answers


  1. I suppose you want to convert characteristic.value (it has type Data?) to a variable of type String.

    You mentioned you want String, but in code you also work with [UInt8], so I’m not sure if I understand what you want to do.

    You’ve already implemented listening for the updates with func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?), and reading the incoming data from characteristic.value, so it remains to convert the data to the wanted type.

    Conversion example (if characteristic.value is nil, or conversion error, result will be an empty string):

    let data = characteristic.value ?? Data()
    let stringValue = String(data: data, encoding: .utf8) ?? ""
    // pass stringValue where you need it
    

    Also you can pass data to the destination function "as is" using type Data?, so it will be the job of destination function to decide what to do with it:

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        DataDestinationClass.doSomething(data: characteristic.value, uuid: characteristic.uuid)
    }
    
    class DataDestinationClass {
        static func doSomething(data: Data?, uuid: CBUUID) {
            let stringValue = String(data: data ?? Data(), encoding: .utf8) ?? ""
            print("MYLOG: stringValue = '(stringValue)'")
        }
    }
    

    Here is an example of putting the received characteristic.value to the Text Field: https://github.com/alexanderlavrushko/BLEProof-collection/blob/5cbb089dbfed42cfb8aad0db236a376ff1cce620/iOS/BLEProofCentral/BLEProofCentral/BLECentralViewController.swift#L295

    Login or Signup to reply.
  2. "The data incoming from BLE is converting into an array, but I believe it’s not possible to send this to a different function."

    No, it’s trivial to pass your data array to another function:

    // Your code
    var array = [UInt8](repeating: 0, count: count)
    data?.copyBytes(to: &array, count:count * MemoryLayout<UInt8>.size)
    
    // At this point you can simply pass your array to another
    // function (which could be in a different class)
    otherClass.processData(data: array)
    
    

    The class / function receiving the data could simply look like this:

    class OtherClass {
        func processData(data: [UInt8]) {
            // Do something with your data
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search