swift - 在 swift 4 中使用 segue 传递连接的 ble 外围设备?

标签 swift bluetooth-lowenergy

我正在尝试将连接的 ble 外围设备作为对象传递到下一个 View Controller 场景,但它适用于两个 View ,然后我不断丢失对外围设备对象的引用? 有人可以指导我将外围设备作为对象从一个 View Controller 类传递到另一个 View Controller 类的正确方法吗?

//从 BLECentral 类到我的第一个 View Controller 类

peripheral.discoverServices([BLEService_UUID])

//连接后,移动到新的 View Controller 来管理传入和传出数据

        let storyboard = UIStoryboard(name: "Main", bundle: nil)


        let firstViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController


        firstViewController.peripheral = peripheral


        navigationController?.pushViewController(firstViewController, animated: true)

//第一个和第二个 View 使用带有 Segue 标识符“Second_View”的 Modal Segue 连接 //从第一个 View 到第二个 View

//First View Class
viewDidLoad(){
peripheral.discoverServices([BLEService_UUID])
}

//Under button click action

@IBAction bla bla bla(){
performSegue(withIdentifier: "Second_View", sender: self)
}

//I want to override the performSegue before its called

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
  if (segue.identifier == "Second_View") {
   
  let secondViewController = segue.destinationViewController as! SecondViewController

  secondViewController?.peripheral = bleperipheral
  }
}

//bleperipheral 来自 BLE Central 类

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,advertisementData: [String : Any], rssi RSSI: NSNumber) {


        blePeripheral = peripheral

        self.peripherals.append(peripheral)

        self.RSSIs.append(RSSI)

        peripheral.delegate = self

        self.baseTableView.reloadData()

        if blePeripheral == nil {

            print("Found new pheripheral devices with services")

            print("Peripheral name: \(String(describing: peripheral.name))")

            print("**********************************")

            print ("Advertisement Data : \(advertisementData)")

        }

    }

BLEService_UUID 是根据 UUID.swift 类中的 ble 字符串声明的 我可以从外围设备 writeValue 和 readValue 但有人可以指导我一种更稳定的方法来传递外围对象吗?

最佳答案

当您在整个应用程序中需要此对象(即作为数据服务)时,您可能需要考虑创建一个保存此外围设备的服务类。使用单例模式,只需记住单例会保留在内存中,直到您的应用程序退出。如果这个对象打算在某个时候被清除,那么单例可能不是你最好的选择,但如果你打算保留它,那么就像这样:

class BTService{
  static let instance = BTService()
  //set up your peripheral here you can have a setup function that you call early in your app}

您可以在第一个 viewDidLoad 中甚至在 appDelegate 中的 applicationFInishedLaunching 中调用该设置函数。然后,在任何 View Controller 上,您将此外围设备称为 BTService.instance.peripheral 或任何您所称的名称,以及您可以通过 instance.setupBT 访问的设置函数。

关于swift - 在 swift 4 中使用 segue 传递连接的 ble 外围设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809723/

相关文章:

swift - 如何更改 SwiftUI View @State **没有**动画

c - 使用 D-Bus API 读取时 Bluez 超时

swift - 使用 Swift 中的按钮通过蓝牙发送数据

android - 跳过 BLE 服务发现

math - CGPoint 标量乘法 Swift

ios - 我需要在收到通知后使用 Firebase 调用 API(应用程序处于打开状态、后台状态、事件状态)

ios - Swift 中的音频队列服务播放器不调用回调

ios - 如何使用 POST 方法调用带有表单数据的 REST API?

CBPeripheralManager AddService : 上的 iOS 崩溃断言失败

objective-c - 后台运行的 iOS 7 核心蓝牙外设