简体   繁体   English

iOS swift 将 BLE 数据传递给单独的 class ZC1C425268E68385D1AB5074C17A94

[英]iOS swift passing BLE data to a separate class function

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.我正处于一个简单的 BLE iOS 应用程序的早期阶段,并试图弄清楚如何将传入的 BLE 数据传输到单独的 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.从 BLE 传入的数据正在转换为数组,但我相信不可能将其发送到不同的 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?有人可以建议将传入的 BLE 数据传输到单独的函数的最有效方法是什么?

This is the code in the func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) :这是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 ??)
        }
    }
}

I suppose you want to convert characteristic.value (it has type Data? ) to a variable of type String .我想您想将characteristic.value (它的类型为Data? )转换为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.你提到你想要String ,但在代码中你也可以使用[UInt8] ,所以我不确定我是否理解你想要做什么。

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.您已经使用func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)实现了侦听更新,并从characteristic.value读取传入数据,因此仍然需要将数据转换为所需的类型.

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?您也可以使用数据类型“按原样”将数据传递到目标 function Data? , so it will be the job of destination function to decide what to do with it: ,因此目的地 function 的工作将决定如何处理它:

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这是将接收到的characteristic.value值放入文本字段的示例: https://github.com/alexanderlavrushko/BLEProof-collection/blob/5cbb089dbfed42cfb8aad0db236a376ff1cce620/iOS/BLEProofCentral/BLEProofCentral/BLECentralViewController。

"The data incoming from BLE is converting into an array, but I believe it's not possible to send this to a different function." “从 BLE 传入的数据正在转换为一个数组,但我认为不可能将其发送到不同的 function。”

No, it's trivial to pass your data array to another function:不,将数据数组传递给另一个 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 / function 可能看起来像这样:

class OtherClass {
    func processData(data: [UInt8]) {
        // Do something with your data
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM