OGeek|极客世界-中国程序员成长平台

标题: ios - 外设和中央同时在同一个应用iOS11上 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:39
标题: ios - 外设和中央同时在同一个应用iOS11上

我正在尝试制作一款用作蓝牙 watch (例如健身手环、智能 watch )主控件的应用。我已经对此进行了研究,尽管有些人设法做到了,但他们并没有提供有关该过程的很多细节。以下是我找到的一些“解决方案”:

Is it possible to switch central and peripheral each other on iOS?

Can iOS do central and peripheral work on same app at same time?

Peripheral and central at the same time on iOS

所有这些都在 Objective-C 中,虽然我对它很熟悉,但这些帖子已经 3 年多了,所以关于代码的事情已经发生了变化。另一个问题是我需要将应用程序与另一个蓝牙设备一起使用,而不是像上面那样使用 iOS 设备,并且目前连接请求只能来自 iPhone,而不是来自蓝牙设备。

问题是是否有可能达到预期的结果,如果可以,最好的方法是什么?到目前为止,建议的解决方案之一是连接到设备,获取 UUID,然后将 iPhone 切换到外围模式,以便它可以宣传其服务。这是不可能的(在我看来),至少在现阶段是这样。

iOS 已经有一个预定义的服务,当它们 2 连接时,设备可以发现和访问该服务(当前时间服务),无需我进行任何修改,因此应该有办法实现这一点。

我希望我对这个问题足够清楚,如果你相信我可以添加更多细节来澄清上下文,请告诉我。谢谢你的时间。

我在下面发布了一些我发现外围设备的关键代码:

 override func viewDidAppear(_ animated: Bool) {

    manager = CBCentralManager(delegate: self, queue: nil)
    peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
    peripherals = []

    if (manager?.state == CBManagerState.poweredOn) {
       scanBLEDevices()
    self.tableView.reloadData()
    }
}
 func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    switch(peripheral.state)
    {
    case.unsupported:
        print("eripheral is not supported")
    case.unauthorized:
        print("eripheral is unauthorized")
    case.unknown:
        print("eripheral is Unknown")
    case.resetting:
        print("eripheral is Resetting")
    case.poweredOff:
        print("eripheral service is powered off")
    case.poweredOn:
        print("eripheral service is powered on")
        print("Start advertising.")

        let serviceUUID:CBUUID = CBUUID(string: self.service_uuid_string)
        let locationUUID:CBUUID = CBUUID(string: self.location_and_speed)

        // Start with the CBMutableCharacteristic
        self.locationCharacteristic = CBMutableCharacteristic(type: locationUUID, properties: .notify , value: nil, permissions: .readable)

        // Then the service
        let locationService = CBMutableService(type: serviceUUID, primary: true)

        // Add the characteristic to the service
        locationService.characteristics?.append(locationCharacteristic!)

        // And add it to the peripheral manager
        self.peripheralManager?.add(locationService)
        peripheralManager?.startAdvertising([CBAdvertisementDataServiceUUIDsKey : serviceUUID])
    }

}



Best Answer-推荐答案


我正在以正确的方式实现所需功能。初始化 peripheralManager 后,创建一个 CBMutableService 并持有对它的引用(在类的顶部声明)。

var globalService:CBMutableService? = 无

下一步是检查 peripheralManager 的状态,并在收到 powerOn 状态后执行所有必需的工作:

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    switch(peripheral.state)

case.poweredOn:
        print("eripheral service is powered on")

        createServiceWithCharacteristics()
}


func createServiceWithCharacteristics(){

    let serviceUUID:CBUUID = CBUUID(string: self.service_uuid_string)
    let featureCharacteristicUUID:CBUUID = CBUUID(string: self.feature_characteristic_uuid_string)

    // Start with the CBMutableCharacteristic
    let permissions: CBAttributePermissions = [.readable, .writeable]
    let properties: CBCharacteristicProperties = [.notify, .read, .write]

    self.featureCharacteristic = CBMutableCharacteristic(type: featureCharacteristicUUID, properties: properties , value: nil, permissions: permissions)

    // Then the service
    let localService = CBMutableService(type: serviceUUID, primary: true)

    // Add the characteristic to the service
    localService.characteristics = [featureCharacteristic!]
    globalService = localService

    // And add it to the peripheral manager
    self.peripheralManager?.add(globalService!)
    print("Start advertising.")
    peripheralManager?.startAdvertising([CBAdvertisementDataLocalNameKey:"Name"])
}

关于ios - 外设和中央同时在同一个应用iOS11上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48405759/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4