8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BLEスキャンコード

Posted at

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)")
    }

}

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?