2015-01-08 39 views
0

我是swift的新手。我沒能獲得一個回調centralManagerDidUpdateState :: W /操場以下(即:我想初始化會回調到centralManagerDidUpdateState):無法讓Core Bluetooth在Swift iOS操作系統中工作

import CoreBluetooth 
class BTDiscovery:NSObject, 
CBCentralManagerDelegate { 

    func centralManagerDidUpdateState(central: CBCentralManager!) { 
     println("here") 
    } 
} 

var bt = BTDiscovery() 

是核心藍牙在iOS版雨燕操場支持? 我試過這個OSX操場和IOBluetooth。這也沒有奏效。我究竟做錯了什麼?

謝謝。

回答

2

我認爲你運行的是操場本質上是同步的,而藍牙發現是異步的。爲了讓它工作,你需要一些事情添加到您的操場允許異步操作:

import XCPlayground 
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true) 

還要注意,由於iOS的操場上的模擬器上運行,我不一定會想到CB在那裏工作所有。

您還有更多的根本性問題,因爲您沒有做任何事情來實際觸發發現。您需要創建的CBCentralManager一個實例,並用它來驅動的發現過程:

import Cocoa 
import XCPlayground 
import CoreBluetooth 

class BTDiscovery:NSObject, CBCentralManagerDelegate { 

    func centralManagerDidUpdateState(central: CBCentralManager!) { 
     println("here") 
    } 

} 

var bt = BTDiscovery() 
var central = CBCentralManager(delegate: bt, queue: dispatch_get_main_queue()) 

XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true) 
+0

非常感謝。對你的評論唯一的修正 - 沒有發現,因爲我想看看是否初始化(var bt = BTDiscovery()得到了一個DidUpdateState。當它沒有,我意識到有東西無法正常工作。在OSX上使用IOBluetooth,我現在再試一次,我同意你的觀點是:模擬器/沒有核心藍牙支持,再次感謝你, – user1255603

+0

didUpdateState不會在你的例子中被調用,因爲沒有CBCentralManager,沒有什麼可以調用的它(除了異步問題) –

1

您只能在實際的iOS設備上使用藍牙核心;它在模擬器中不受支持,並且通過擴展,它在Playground中不受支持,因爲它也在Mac上執行,而不是在iOS設備上執行。