質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

1825閲覧

xcode, seuge間での値の受け渡し

sandalwalk

総合スコア77

Bluetooth

Bluetoothとは短距離の間でデータを交換するための無線通信規格である。固定・モバイル両方のデバイスから、短波の電波送信を行うことで、高いセキュリティをもつパーソナルエリアネットワーク(PAN)を構築する。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/11/07 00:22

編集2016/11/09 06:10

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! }

イメージ説明

イメージ説明

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fromageblanc

2016/11/07 03:11

prepaeメソッド中の、mySecondViewController.relayedFoundUUID = myCharacteristicUUID as NSMutableArrayの前に、print(myCharacteristicUUID)を入れた結果を見てみたいです。
sandalwalk

2016/11/07 07:19

試してみたところ、理由は不明ですがmyCharacteristicUUIDが空になっています。
fromageblanc

2016/11/07 07:32

その理由が掴めれば解決しますよ。どこかで不要な初期化があるんだと思います。
sandalwalk

2016/11/07 11:03

がんばってみます
fromageblanc

2016/11/09 03:20

myCharacteristicUUIDをclassの外側に書けばグローバルにはなりますが、提示のコードの場合、prepareで受け渡しできるはずなんですけどね。ViewControllerのソースを全部掲載してもらえないでしょうか。
sandalwalk

2016/11/09 05:21

ソースを全部掲載しました。
fuzzball

2016/11/09 05:56

SecondViewControllerの生成方法と、SecondViewControllerへの遷移方法を教えて下さい。
sandalwalk

2016/11/09 06:17

SecondViewControllerの生成方法は、スクリーンショットのファイルの一覧の部分で右クリックし、swiftファイルを新規作成しました。(生成方法の意味が違っていたらご指摘下さい)遷移方法は、Show(e.g. Push)としています。
fuzzball

2016/11/09 06:25

もしかして、「 vc.relayedFoundCharacteristicUUID = myCharacteristicUUID! 」にしたら渡せますか?
fromageblanc

2016/11/09 06:31

self.myCharacteristicUUID.add(c.uuid) の後に、テスト用で、performSegue(withIdentifier: "mySegue", sender: nil) を置いて遷移するか確認できますか?テーブルセルのタップでなく、myCharacteristicUUIDにデータが入ったときに遷移させて、次の画面で受け取れるかの確認です。このブロックに入れば受け取れると思うんです。予想ですが、現状myCharacteristicUUIDが空のままセルタップで遷移してるのではないかと。
sandalwalk

2016/11/09 06:46

すみません。。どの部分を変更するというこでしょうか。prepare( 内のmySecondViewController.relayedFoundCharacteristicUUID = myCharacteristicUUID部分を変更するというこででしたら、試した所、use of unresolved identifier 'vc'となってしまいます
fromageblanc

2016/11/09 06:49

一画面目です。「targetService に対応する characteristicを探すメソッド」中の、self.myCharacteristicUUID.add(c.uuid)の直後に、performSegue(withIdentifier: "mySegue", sender: nil) を置きます
sandalwalk

2016/11/09 06:56

今気づきましたが、一旦segueで遷移後、backで基のテーブルビュー画面に戻って、再度何かしらのセル(さっきとは異なるセルでも)を選択して、もう一度seugeさせると、一旦失敗した値の受け渡し(最初に選んだセルの中身)ができています。
fromageblanc

2016/11/09 07:05

失敗してるというか、セルタップ時にmyCharacteristicUUIDが取得されている保証はないロジックになってますよね。設計レベルの問題なのでは。
sandalwalk

2016/11/09 07:15

教えて頂いた方法を試した所、受け渡しに成功しました。ありがとうございました。セルタップ時に、目的の情報が取得できているかを確認するのが大事だと分かりました。
fuzzball

2016/11/09 07:24

んん?ViewControllerでのprintで出力されているものは何なんでしょうか?
fromageblanc

2016/11/09 09:00

そこが釈然としないのですが、おそらく出力された時はうまく渡ってたんだと思います。
sandalwalk

2016/11/09 20:36

fuzzball様 printの出力はその場での状況の把握が目的だけですので、気になさらないで下さい。アドバイス、ありがとうございました。
sandalwalk

2016/11/09 20:39

fromageblanc様 私は2度めのsegueで1度目の内容のデータが格納されるのを見て、もしや、segueの実行のタイミングがおかしい?と感じましたが、一人では解決できませんでした。プログラミング経験ゼロからswiftに挑戦してまだ1ヶ月ですので、またアドバイスよろしくお願いいたします。
guest

回答1

0

(deleted)

投稿2016/11/09 05:40

編集2016/11/09 05:45
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問