如何通过功能将数据发送到BLE并快速读取同一功能中的响应

时间:2018-08-22 09:59:25

标签: swift bluetooth-lowenergy

我能够通过一个函数将数据发送到BLE,并且我也从BLE接收数据,但是在循环完成后我能够看到Rx。
循环完成后,Didupdatevalueforcharacteristics被调用了,有谁能帮我找到解决方法

这是我的代码:

用于将数据发送到BLE的功能:

func SendRawData(data: [UInt8])
    { 
        let Transmitdata = Data(bytes:data)

        print("bleperipheral is ", blePeripheral)

        print("tx char is ",txCharacteristic!)

        blePeripheral?.writeValue(Transmitdata , for: txCharacteristic!, type: CBCharacteristicWriteType.withoutResponse)

    }

didupdatevalueforcharacteristics函数为:

public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
    {       
        var ReceivedByteArray = [UInt8]()

        let ReceivedData = rxCharacteristic?.value

        if let ReceivedData = ReceivedData
        {        
            let ReceivedNoOfBytes = ReceivedData.count

            ReceivedByteArray = [UInt8](repeating: 0, count: ReceivedNoOfBytes)

            (ReceivedData as NSData).getBytes(&ReceivedByteArray, length: ReceivedNoOfBytes)

            RxByteArray.append(ReceivedByteArray)

            print("Received byte array is ", RxByteArray)        
        }
    }

0 个答案:

没有答案