如何跨视图控制器访问蓝牙数据?在 xcode

How to access bluetooth data across view controllers? in xcode

我正在尝试通过 BLE 访问运动传感器(IMU 传感器)。结构是 connect/pair 一个视图控制器中的传感器并修改其设置,然后在另一个视图控制器中访问和分析其输出数据(它们不是 segue 连接)。

如何继续访问已连接到另一个视图控制器的传感器的数据? Coredata 并不理想,因为它是实时呈现的,不需要记录原始数据。我也不能通过 segue 传递数据,因为它们没有 segue 连接(它们是通过不同的标签栏视图控制器访问的)。

我发现一个 link 说可以将 CBCentralManager 等放入 AppDelegate,然后它成为中央管理器 属性 ()。这是正确的方法吗?如果是这样,应该将中央管理器的哪一部分放入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)

    }
}

您是否可以创建一个 全局 class 来成为 中央管理器 属性

查看此答案如何操作:

然后您可以使用 NSNotificationCenter 在整个应用程序中发送数据,只需在每个 ViewController[ 中设置 observers =22=]