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 ?
Can somebody help?
Also how do I clear the readValue buffer in rxCharacterstic before writing and reading new values?
Any help/comments appreciated.
Thanks
EDIT -> How my passing is done
From 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
}
}