BLEのcharacteristicの中のUUIDを、seugeで次の画面に受け渡したいのですが、上手く動作しません。チェックの為に途中でmyCharacteristicUUIDをprintしてみると、
myCharacteristicUUID : (
"Manufacturer Name String",
"Model Number String",
"Serial Number String",
"Hardware Revision String",
"Firmware Revision String",
"Software Revision String"
)
と、問題なく出力されますが、受け渡したいrelayedFoundUUIDについては、
relayedFoundUUID: Optional(<__NSArrayM 0x174056830>(
)
)
relayedFoudnUUID_count: 0
となり、値を読み取ることができません。 色々と試してみると、myCharacteristicUUIDもfuncの外に受け渡すことができていないことが分かりました。BLEから取り出した情報をそのfuncの外でも利用できる様にするには何か方法があるのでしょうか?
以下がコードの全文です。
// ViewController.swift import UIKit import CoreBluetooth class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource,CBCentralManagerDelegate,CBPeripheralDelegate { var myUuids: NSMutableArray = NSMutableArray() var myServiceUuids: NSMutableArray = NSMutableArray() var myCharacteristicUUID:NSMutableArray=NSMutableArray() var myService: NSMutableArray = NSMutableArray() var myIndexPath: NSMutableArray = NSMutableArray() var targetService:CBService! var relayedFoundUUID:NSMutableArray = NSMutableArray() var manager: CBCentralManager! var peripheral: CBPeripheral! @IBOutlet weak var tableViewOutlet: UITableView! override func viewDidLoad() { super.viewDidLoad() self.manager = CBCentralManager(delegate: self, queue: nil) } @IBAction func startButton(_ sender: AnyObject) { self.manager.scanForPeripherals(withServices: nil, options: nil) print("start scan peripherals") } @IBAction func stopButton(_ sender: AnyObject) { self.manager.stopScan() } func centralManagerDidUpdateState(_ central: CBCentralManager) { if central.state == CBManagerState.poweredOn { print("BLE Powered ON") } } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { self.peripheral = peripheral self.peripheral.delegate = self self.manager.connect(peripheral, options: nil) self.manager.stopScan() } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { print("connected to peripheral") self.peripheral.discoverServices(nil) } func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { if error != nil { print("Failed to discover services") } else{ for service in peripheral.services! { myServiceUuids.add(service.uuid) myService.add(service) } // DispatchQueue.main.async(execute: { self.tableViewOutlet.reloadData() }) } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { myIndexPath.add(indexPath.row) let targetService = myService[indexPath.row] print("target_service: \(targetService)") self.peripheral.discoverCharacteristics(nil, for:targetService as! CBService) performSegue(withIdentifier: "mySegue", sender: nil) } // targetService に対応する characteristicを探すメソッド func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor targetService: CBService, error: Error?) { if error != nil { print("Characteristic NOT found : \(error)") } let foundCharacteristics = targetService.characteristics for c in foundCharacteristics! as [CBCharacteristic] { self.myCharacteristicUUID.add(c.uuid) } } // prepare for segue override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if (segue.identifier == "mySegue") { print("myCharacteristicUUID: \(myCharacteristicUUID)") let mySecondViewController : SecondViewController = segue.destination as! SecondViewController mySecondViewController.relayedFoundCharacteristicUUID = myCharacteristicUUID } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myServiceUuids.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"firstTable" ) // Cellに値を設定. cell.textLabel?.sizeToFit() cell.textLabel?.textColor = UIColor.red cell.textLabel?.text = "\(myServiceUuids[indexPath.row])" cell.textLabel?.font = UIFont.systemFont(ofSize: 16) return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
// SecondViewController.swift import UIKit import CoreBluetooth class SecondViewController: UIViewController,CBPeripheralDelegate { var relayedFoundCharacteristicUUID:NSMutableArray! override func viewDidLoad() { super.viewDidLoad() print("relayedFoundCharacteristicUUID:\(relayedFoundCharacteristicUUID)") } @IBOutlet weak var secondTableOutlet: UITableView! }