###前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/30 09:13