とりあえず、サンプルを参考にして、コードを書いて普段使っているBluetooth接続のイヤホンで試しているのですがperipheral.nameを出力したいのですが何も表示されません。
最終的にはMbedのLEDを点灯させることが目的で、まずはBluetoothを検出できるようにしたいです。
import UIKit import CoreBluetooth class ViewController: UIViewController,CBCentralManagerDelegate { var centralManager:CBCentralManager! var BLEPeripheral:CBPeripheral! var count = 0 //=========================================================================== // MARK: -- 初期化 & ビューライフサイクル -- //=========================================================================== override func viewDidLoad() { super.viewDidLoad() //CBCentralManagerを初期化 //centralManagerDidUpdateStateで状態変化が取得できます centralManager = CBCentralManager(delegate: self, queue: nil) } //=========================================================================== // MARK: -- BLE -- //=========================================================================== //セントラルマネージャーの状態変化を取得 func centralManagerDidUpdateState(_ central: CBCentralManager) { switch (central.state) { case .poweredOff: print("Bluetoothの電源がOff") case .poweredOn: print("Bluetoothの電源はOn") //ペリフェラルのスキャン開始 centralManager.scanForPeripherals(withServices: nil, options:nil) case .resetting: print("レスティング状態") case .unauthorized: print("非認証状態") case .unknown: print("不明") case .unsupported: print("非対応") } } @IBAction func startButton(){ count += 1 print("(count)") } //スキャン結果を受け取る private func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { //スキャンが完了しない場合は、peripheral.nameが違っているかもしれません。 //ここでperipheral.nameを確認してください。 //print("(peripheral.name)") // 変更start if(count != 0){ print("peripheral.name(String(describing: peripheral.name))") } //end BLEPeripheral = peripheral centralManager.stopScan() print("(String(describing: peripheral.name))をスキャン完了!") } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/30 14:05
2017/11/30 14:26
2017/11/30 15:29