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

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

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

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

mbed

mbed(エンベッド)は、Webサイト上でC++を使って開発を行う、ワンボードマイコンのプロトタイピングツールです。PCに開発環境をインストールする必要がなく、Webにアクセスできればどこにいても開発を行うことができます。

Q&A

解決済

1回答

2202閲覧

swift3→4のエラーが解決できません

debon18

総合スコア7

Bluetooth

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

mbed

mbed(エンベッド)は、Webサイト上でC++を使って開発を行う、ワンボードマイコンのプロトタイピングツールです。PCに開発環境をインストールする必要がなく、Webにアクセスできればどこにいても開発を行うことができます。

1グッド

0クリップ

投稿2017/11/24 06:42

###前提・実現したいこと
mbedとiphoneをBluetoothで接続しようとしています。
しかし、145行目(*で囲んでいる部分)に以下のようなエラーが出てしまい困っています。

###発生している問題・エラーメッセージ

Ambiguous reference to member 'peripheral(peripheral:didDiscoverServices:)'

###該当のソースコード

import UIKit import CoreBluetooth class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { var centralManager: CBCentralManager! private var peripheralArray = [CBPeripheral]() private var serviceArray = [CBService]() private var characteristicArray = [CBCharacteristic]() @IBOutlet weak var scanStatus: UILabel! @IBOutlet weak var scanStatusText: UITextView! @IBOutlet weak var connectStatus: UILabel! @IBOutlet weak var sendText: UITextField! //一番最初に呼び出される override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // ボタン押下でCoreBluetoothを起動 @IBAction func pushStartBtn(sender: AnyObject) { // 1-1. CentralManagerの起動 self.centralManager = CBCentralManager(delegate: self, queue: nil, options: nil) } // 1-2. CentralManager状態の受信 func centralManagerDidUpdateState(_ _central: CBCentralManager) { switch (_central.state) { case .poweredOff: print("BLE PoweredOff") case .poweredOn: print("BLE PoweredOn") // 2-1. Peripheral探索開始 _central.scanForPeripherals(withServices:nil, options: nil) /* ↑の第1引数はnilは非推奨。 該当サービスのCBUUIDオブジェクトの配列が望ましい */ case .resetting: print("BLE Resetting") case .unauthorized: print("BLE Unauthorized") case .unknown: print("BLE Unknown") case .unsupported: print("BLE Unsupported") } } // 2-2. Peripheral探索結果の受信(複数あれば複数回) private func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!){ print("name: (String(describing: peripheral.name))") print("UUID: (peripheral.identifier.uuidString)") print("advertisementData: (advertisementData)") print("RSSI: (RSSI)") // 配列に追加 self.peripheralArray.append(peripheral as CBPeripheral) } // リストから該当Peripheralを選択し接続を開始 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { // 省電力のためスキャンを停止 self.centralManager.stopScan() // 3-1. 指定したPeripheralへ接続開始 self.centralManager.connect(peripheralArray[indexPath.row], options: nil) } // 3-2. Peripheralへの接続結果の受信(成功時) func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) { print("connection success!") // デリゲートの設定 peripheral.delegate = self // 今回はサービスを"FFF0"に指定 let UUID = CBUUID(string: "FFF0") // 4-1. 利用可能Serviceの探索開始 peripheral.discoverServices([UUID]) /* ↑の引数はnilは非推奨。*/ } // 3-2. Peripheralへの接続結果の受信(失敗時) private func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: Error!) { print("connection failed") } // 4-2. Service探索結果の受信 private func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) { if (error != nil) { print("error: (error)") return } for service in peripheral.services! { self.serviceArray.append(service as CBService) // 5-1. 利用可能Characteristicの探索開始 peripheral.discoverCharacteristics(nil, for:service as CBService) /* ↑の第1引数はnilは非推奨。*/ } } // 5-2. Characterristic探索結果の受信 private func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) { if (error != nil) { print("error: (error)") return } for characteristic in service.characteristics! { self.characteristicArray.append(characteristic as CBCharacteristic) } } // ボタン押下で書き込み開始 @IBAction func pushWriteBtn(sender: AnyObject) { // 書き込むデータを仮で設定 var value: UInt = 0x0000FF80808080808000 let data: NSData = NSData(bytes: &value, length: 10) // 6-1. 指定データの書き込み ******************************************* [peripheral .writeValue(data!, for: characteristicArray[0], type: CBCharacteristicWriteType.WithResponse)] } ******************************************* // 6-2. 書き込み結果の受信 private func peripheral(peripheral: CBPeripheral!, didWriteValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) { if (error != nil) { print("Write...error: (error)") return } print("Write success!") } }

###試したこと
参考にしたコードはswift3であり、今はswift4を使っているためwriteValueの文法がおかしいのかなと思い調べてみたのですが結局わかりませんでした。

###補足情報(言語/FW/ツール等のバージョンなど)
swift4
Xcode Version 9.0.1

Omogo👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

  • peripheralが定義されていない。
  • 両端の[]は何?おそらく不要。
  • .WithResponse.withResponse

Swift3 → 4の問題ではないような。

投稿2017/11/24 07:14

編集2017/11/24 07:17
fuzzball

総合スコア16731

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

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

debon18

2017/11/30 09:13

初めてSwiftを使ってみたためサンプルコードの理解すらできていません。とりあえず、bluetoothの検出をしたいのですが、よろしければ検出の仕方を教えてくれませんか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問