2017-04-12 56 views
2

我试图通过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) 

    } 
} 
+0

背后的想法把CBCentralManager作为AppDelegate中可访问的属性,是使用AppDelegate是sharedInstance(单例模式)的事实。 但是这给了AppDelegate太多无关的工作。让AppDelegate完成“AppDelegating”的工作。 为你自​​己创建一个Singleton(看看“Singleton + Swift”),它将管理你所有的BLE的东西。 – Larme

+0

非常感谢! – Mushroomcloud

回答

1

你可能创建一个全局类能够成为中央管理器属性

看到这个答案如何做到这一点:https://stackoverflow.com/a/6067515/1331332

然后,您可以使用NSNotificationCenter到整个应用程序发送的数据,刚刚成立的观察家每个的ViewController