首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS将BLE数据快速传递给一个单独的类函数

iOS将BLE数据快速传递给一个单独的类函数
EN

Stack Overflow用户
提问于 2022-01-30 14:46:33
回答 2查看 142关注 0票数 0

我确信这个问题已经在某个地方得到了回答,但我不知道该寻找什么。我正处于一个简单的BLE iOS应用程序的早期阶段,并试图研究如何将传入的BLE数据转移到一个单独的函数中。从BLE传入的数据正在转换为数组,但我认为不可能将其发送到不同的函数。我试图将其作为字符串发送,但似乎不可能将该字符串拆分为数组。

有人能建议什么是将传入的BLE数据转移到单独的函数中最有效的方法吗?

这是func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)中的代码

代码语言:javascript
复制
// 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")
    
}
代码语言:javascript
复制
// 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 ??)
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-30 20:49:55

“来自BLE的数据正在转换为数组,但我认为不可能将其发送到不同的函数。”

不,将数据数组传递给另一个函数很简单:

代码语言:javascript
复制
// 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)

接收数据的类/函数可以简单地如下所示:

代码语言:javascript
复制
class OtherClass {
    func processData(data: [UInt8]) {
        // Do something with your data
    }
}
票数 1
EN

Stack Overflow用户

发布于 2022-01-30 19:33:31

我想要将characteristic.value (它有Data?类型)转换为String类型的变量。

您提到您想要String,但是在代码中您也使用了[UInt8],所以我不确定我是否理解您想要做什么。

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

转换示例(如果characteristic.value为零,或转换错误,则结果为空字符串):

代码语言:javascript
复制
let data = characteristic.value ?? Data()
let stringValue = String(data: data, encoding: .utf8) ?? ""
// pass stringValue where you need it

此外,您还可以使用Data?类型将数据传递给目标函数“原样”,因此决定如何处理它是目的地函数的工作:

代码语言:javascript
复制
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放入Text字段的示例:https://github.com/alexanderlavrushko/BLEProof-collection/blob/5cbb089dbfed42cfb8aad0db236a376ff1cce620/iOS/BLEProofCentral/BLEProofCentral/BLECentralViewController.swift#L295

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70915747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档