9
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

Organization

BLEスキャンコード

iOS x CoreBluetooth本のコードを試してみました。
※ ライブラリにCoreBlutoothを入れてからコンパイルしてください。

import UIKit
import CoreBluetooth


class ViewController: UIViewController, CBCentralManagerDelegate{

    // 初期化していないのでオプショナル型を使う
    var centralManager:CBCentralManager?
    var peripheral:CBPeripheral?

    override func viewDidLoad() {
        super.viewDidLoad()

        // CBCentralManagerを初期化
        self.centralManager = CBCentralManager(delegate: self, queue: nil)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // CBCentralManagerの状態変化を取得
    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("state: \(central.state.rawValue)")

        switch(central.state){
            case CBCentralManagerState.PoweredOn:
                println("スキャン開始")
                // スキャン開始
                self.centralManager!.scanForPeripheralsWithServices(nil, options: nil)
                break

            case CBCentralManagerState.PoweredOff:
                println("電源が入っていないようです。")
                break

            case CBCentralManagerState.Unknown:
                println("unknown")
                break

            case CBCentralManagerState.Unauthorized:
                println("Unauthorized")
                break

            case CBCentralManagerState.Unsupported:
                println("Unsupported")
                break

            default:
                break
        }
    }

    // スキャン結果を受け取る
    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
        println("peripheral: \(peripheral)")
    }

}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
9
Help us understand the problem. What are the problem?