簡體   English   中英

iOS swift 將 BLE 數據傳遞給單獨的 class ZC1C425268E68385D1AB5074C17A94

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

我確信這已經在某個地方得到了回答,但我不確定要搜索什么。 我正處於一個簡單的 BLE iOS 應用程序的早期階段,並試圖弄清楚如何將傳入的 BLE 數據傳輸到單獨的 function。 從 BLE 傳入的數據正在轉換為數組,但我相信不可能將其發送到不同的 function。 我嘗試將其作為字符串發送,但似乎不可能將字符串分解為數組。

有人可以建議將傳入的 BLE 數據傳輸到單獨的函數的最有效方法是什么?

這是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 ??)
        }
    }
}

我想您想將characteristic.value (它的類型為Data? )轉換為String類型的變量。

你提到你想要String ,但在代碼中你也可以使用[UInt8] ,所以我不確定我是否理解你想要做什么。

您已經使用func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)實現了偵聽更新,並從characteristic.value讀取傳入數據,因此仍然需要將數據轉換為所需的類型.

轉換示例(如果特征值為零,或轉換錯誤,結果將為空字符串):

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

您也可以使用數據類型“按原樣”將數據傳遞到目標 function Data? ,因此目的地 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)'")
    }
}

這是將接收到的characteristic.value值放入文本字段的示例: https://github.com/alexanderlavrushko/BLEProof-collection/blob/5cbb089dbfed42cfb8aad0db236a376ff1cce620/iOS/BLEProofCentral/BLEProofCentral/BLECentralViewController。

“從 BLE 傳入的數據正在轉換為一個數組,但我認為不可能將其發送到不同的 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)

接收數據的 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