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
I suppose you want to convert
characteristic.value
(it has typeData?
) to a variable of typeString
.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 fromcharacteristic.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):
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: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"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:
The class / function receiving the data could simply look like this: