我試圖通過BLE訪問運動傳感器(IMU傳感器)。該結構是在一個視圖控制器中連接/配對傳感器,並修改其設置,然後在另一個視圖控制器(它們不連續)訪問和分析其輸出數據。如何通過視圖控制器訪問藍牙數據?在xcode
如何繼續訪問已連接到另一個視圖控制器的傳感器的數據? Coredata不會很理想,因爲它是實時呈現的,並且不需要原始數據來記錄。我不能通過segue傳遞數據,因爲它們不是連續的(它們通過不同的標籤欄視圖控制器訪問)。
我發現一個鏈接,說可以將CBCentralManager等放入AppDelegate,然後它變成中央管理器屬性(How to continue BLE activities onto next view controller)。這是做到這一點的正確方法嗎?如果是這樣,那麼中央經理的哪一部分應該放入AppDelegate?
這是我的代碼,包括搜索和建立藍牙連接。
var cManager = CBCentralManager()
var peripheralManager = CBPeripheralManager()
func centralManagerDidUpdateState(central: CBCentralManager!) {
var message: String = ""
switch cManager.state {
case .PoweredOff:
println("CoreBluetooth BLE hardware is powered off")
let alertView = UIAlertController(title: "", message: "Please enable Bluetooth to start the measurement", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
break
case .PoweredOn:
println("CoreBluetooth BLE hardware is powered on and ready")
self.scanForWAX9(self)
break
case .Resetting:
println("CoreBluetooth BLE hardware is resetting")
break
case .Unauthorized:
println("CoreBluetooth BLE state is unauthorized")
break
case .Unknown:
println("CoreBluetooth BLE state is unknown")
break
case .Unsupported:
println("CoreBluetooth BLE hardware is unsupported on this platform")
break
default:
break
}
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println(peripheral.name);
//************************************************************************************
// Add some specification for bluetooth device
//************************************************************************************
if (peripheral.name != nil) && (peripheral.name.rangeOfString("WAX9") != nil) {
central.connectPeripheral(peripheral, options: nil)
self.connectedPeripheral = peripheral
println("PERIPHERAL NAME: \(peripheral.name)\n AdvertisementData: \(advertisementData)\n RSSI: \(RSSI)\n")
println("UUID DESCRIPTION: \(peripheral.identifier.UUIDString)\n")
println("IDENTIFIER: \(peripheral.identifier)\n")
cManager.stopScan()
}
}
@IBOutlet var connectNotice: UILabel!
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
peripheral.delegate = self
peripheral.discoverServices(nil)
// self.connectedPeripheral = peripheral
connectNotice.text = "\(peripheral.name) connected."
connectNotice.textColor = UIColor.whiteColor()
connectNotice.backgroundColor = UIColor(red:0.03, green:0.37, blue:0.5, alpha:0.5)
cManager.stopScan()
println("Scanning stopped")
println("Connected to peripheral")
}
// Alert message when fail to connect, e.g. when sensor goes out of range
func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
println("FAILED TO CONNECT \(error)")
let alertView = UIAlertController(title: "", message: "Failed to connect.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
self.disconnect()
}
// Start to scan for sensor when disconnected
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
println("Disconnected from peropheral")
let alertView = UIAlertController(title: "", message: "The connection is stopped.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
self.connectedPeripheral = nil
if scanAfterDisconnecting {
scanForWAX9(self)
}
}
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
switch peripheralManager.state {
case .PoweredOff:
println("Peripheral - CoreBluetooth BLE hardware is powered off")
break
case .PoweredOn:
println("Peripheral - CoreBluetooth BLE hardware is powered on and ready")
break
case .Resetting:
println("Peripheral - CoreBluetooth BLE hardware is resetting")
break
case .Unauthorized:
println("Peripheral - CoreBluetooth BLE state is unauthorized")
break
case .Unknown:
println("Peripheral - CoreBluetooth BLE state is unknown")
break
case .Unsupported:
println("Peripheral - CoreBluetooth BLE hardware is unsupported on this platform")
break
default:
break
}
}
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
if (error != nil) {
println("ERROR: \(error)")
disconnect()
return
}
for service in peripheral.services
{
NSLog("Discovered service: \(service.UUID)")
peripheral.discoverCharacteristics(nil, forService: service as CBService)
}
}
背後的想法把CBCentralManager作爲AppDelegate中可訪問的屬性,是使用AppDelegate是sharedInstance(單例模式)的事實。 但是這給了AppDelegate太多無關的工作。讓AppDelegate完成「AppDelegating」的工作。 爲你自己創建一個Singleton(看看「Singleton + Swift」),它將管理你所有的BLE的東西。 – Larme
非常感謝! – Mushroomcloud