gpt4 book ai didi

ios - 如何跨 View Controller 访问蓝牙数据?在 xcode 中

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:02 26 4
gpt4 key购买 nike

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

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

我发现一个链接说可以将 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)

}
}

最佳答案

您可以创建一个可以成为中央管理器属性全局类吗?

查看此答案如何操作:https://stackoverflow.com/a/6067515/1331332

然后您可以使用 NSNotificationCenter 在整个应用中发送数据,只需在每个 ViewController 中设置观察者

关于ios - 如何跨 View Controller 访问蓝牙数据?在 xcode 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43364207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com