简体   繁体   English

如何快速正确读取外围值?

[英]How to read ble peripheral value correctly in swift?

I am trying to read a byte array from my one view controller to another, please find my code below. 我正在尝试从一个视图控制器向另一个视图控制器读取字节数组,请在下面找到我的代码。

From my First View 从我的第一眼看

class First: UIViewController {

var myByteArray = [UInt8](repeating:0, count: 20)

viewDidLoad(){
......}

Few statements later hers where I read my data in a function 后来她的几句话我在函数中读取数据的地方

func passThis(){

let ReceiveData = rxCharacteristic?.value
        if let ReceiveData = ReceiveData {
            let ReceivedNoOfBytes = ReceiveData.count
            myByteArray = [UInt8](repeating: 0, count: ReceivedNoOfBytes)
            (ReceiveData as NSData).getBytes(&myByteArray, length: ReceivedNoOfBytes)
            print("Data Received ",myByteArray)
               }

This is my Second View that I'm trying to read my array from First View 这是我的第二视图,我正在尝试从第一视图读取数组

class Second: UIViewController {

var myByteArray2 = [UInt8](repeating: 0, count: 20)

viewDidLoad(){
super.viewDidLoad()
let fvc = First()
myByteArray2 = fvc.myByteArray

print(myByteArray2)
}

Now I have [11,12,13,14,15,16,17,18,19,20] from myByteArray but have [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] from myByteArray2 ? 现在我有myByteArray的[11,12,13,14,15,16,17,18,19,20]但有[0,0,0,0,0,0,0,0,0,0,0 ,my,0,0,0,0,0,0,0,0,0,0]来自myByteArray2吗?

Can somebody help? 有人可以帮忙吗?

Also how do I clear the readValue buffer in rxCharacterstic before writing and reading new values? 另外,在写入和读取新值之前,如何清除rxCharacterstic中的readValue缓冲区?

Any help/comments appreciated. 任何帮助/评论表示赞赏。

Thanks 谢谢

EDIT -> How my passing is done 编辑->如何通过我的

From BLECentral 从BLECentral

class BLECentral: ...

var centralManager: CBCentralManager!

//After Scanning and connecting

func centralManager(_central: CBCentralManager, didConnect peripheral: CBPeripheral){

peripheral.delegate = self

peripheral.discoverServices([BLEUUID])



//Once connected, move to new view controller to manager incoming and outgoing data
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let firstVC = storyboard.instantiateViewController(withIdentifier: "First") as! First

        firstVC.peripheral = peripheral

        navigationController?.pushViewController(firstVC, animated: true)
    }

Now in my First under prepare for segue block I'm passing the peripheral like this 现在,在我的第一个准备隔离块的条件下,我像这样通过外围设备

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if segue.destination is Second
{
let vc2 = segue.destination as? Second
vc2.periperal = blePeripheral
}
}

You are creating a new First view controller from your Second view controller, instead of accessing the one already created. 您正在从Second视图控制器中创建一个新的First视图控制器,而不是访问已创建的控制器。

//Create a new View Controller
let fvc = First()

So you have two First that are made now. 因此,您现在有两个First What you want to do is access the already created First view controller. 您要执行的操作是访问已创建的“ First视图控制器。 Assuming there is no other way, you want to have a "Singleton". 假设没有其他方法,您想要一个“ Singleton”。 This is a very bad way to handle this, as I'll explain later, and there is most likely a better way, but I'm going to give a solution to access First from Second if they never communicate, but First is already created. 正如我将在后面解释的那样,这是处理此问题的一种非常糟糕的方法,并且最有可能是一种更好的方法,但是如果他们从不进行交流,但我将给出一种解决方案,以便他们从Second访问First,但是First已经创建。

If you declare in First a piece of code like: 如果您在First声明一段代码,例如:

static let shared = First()

Then that singleton can be accessed via 然后可以通过访问该单例

let first = First.shared

That being said, that's a really bad way of handling communication between view controllers. 话虽如此,这是处理视图控制器之间通信的一种非常糟糕的方法。 If you call Second from First, you should pass the data from First to Second (or you could pass a reference of First to Second so Second can access First). 如果从第一个调用第二个,则应将数据从第一个传递到第二个(或者可以将第一个传递给第二个引用,以便第二个可以访问第一个)。

There is generally good ways to pass data between view controllers in the 通常,有很好的方法可以在视图控制器之间的视图控制器之间传递数据

func prepare(for segue: UIStoryboardSegue, sender: Any?) 

method before you navigate. 导航之前的方法。 Whenever, whatever, makes the Second view controller should pass it the data it needs. 无论何时,无论如何,第二个视图控制器都应该向其传递所需的数据。

Lastly, another reason the Singleton view controller is a terrible idea is that it gets away from the MVC concept. 最后,Singleton视图控制器是一个糟糕想法的另一个原因是它摆脱了MVC概念。 So if you can't pass the proper data to Second, you probably need a new class that handles the data for you that both First and Second can work with which is the Model in MVC. 因此,如果您无法将正确的数据传递给Second,则可能需要一个新的类来为您处理可以同时与First和Second一起使用的数据,这就是MVC中的Model。

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

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