【问题标题】:iOS peripheral service still empty after discovering发现后iOS外围服务仍然为空
【发布时间】:2016-10-03 21:28:10
【问题描述】:

我正在尝试使用蓝牙 我有这样的事情:

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    Connected = true
    TableView.reloadData()
    Activityview.stopAnimating()
    TabBar.topItem!.rightBarButtonItem = UIBarButtonItem(title: "Suivant", style: UIBarButtonItemStyle.Done, target: nil, action: #selector(Connexion.next))
    CM.stopScan()
    Scanning = false
    Refresh_Button.setTitle("Deconnecté", forState: UIControlState.Normal)
    let AlertMessage = UIAlertController(title: "Connecté", message: "Le module est bien connecté", preferredStyle: .Alert)
    let ActionAlert = UIAlertAction(title: "Ok", style: .Default) { (action) in }
    AlertMessage.addAction(ActionAlert)
    self.presentViewController(AlertMessage, animated: true, completion: nil)
    peripheral.delegate = self
    let servicetoFind = CBUUID(string: "19B10000-E8F2-537E-4F6C-D104768A1214")
    peripheral.discoverServices([servicetoFind])
}

我的服务功能:

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
    print("found service")
    print(peripheral)
    print(peripheral.services)
    print(error)
    for service in peripheral.services! {
        print("Searching for charac...")
        peripheral.discoverCharacteristics(nil, forService: service)
    }
}

找到我的服务并进入控制台:

found service
<CBPeripheral: 0x13ce1c430, identifier = F39F9D5D-98FE-A350-17BD-21C1185E2089, name = GENUINO 101-1E2D, state = connected>
Optional([])
nil

为什么我的外围设备的服务在发现后仍然为空但没有错误?

有什么想法吗?

谢谢!

【问题讨论】:

  • 您是否已将您的peripheral 保存在属性中,也许保存在此处未显示的didDiscover,, 中?
  • 我将找到的所有外围设备记录到一个 CBPeripheral 数组中,如下所示:Peripherals.append(peripheral)。但我还没有使用它们。

标签: ios swift bluetooth swift2 cbperipheral


【解决方案1】:

好吧,我重新启动了我的 iPhone,现在一切正常...谢谢大家!

【讨论】:

  • 我只是在这上面浪费了几个小时!使用我的服务 UUID 时发现并连接了我的后台外围设备,但 didDiscoverServices 没有返回任何服务。重新启动修复它。
  • 看来我说得太早了。它昨天在后台工作了一整天。今天我使用 Xcode 在两台设备上运行了新版本的应用程序,我又回到了无法获得后台服务的状态,即使它发现了它。我已经多次重启了这两个设备:(
  • 也许禁用/启用BT?
【解决方案2】:

它可以为零。您应该只检查您正在使用的 for 循环下的服务。由于您没有获得任何服务,因此您也可以尝试 peripheral.discoverServices(nil)。它将列出所有服务。

【讨论】:

  • 我已经尝试过使用 nil 参数,而 iOS 只是找到 0 服务。但是如果我在参数中使用我的服务,iOS 会找到我的外设中的服务。我了解 iOS 在 peripheral.services 中为找到的所有服务添加服务,并且可以直接在此处理程序中使用:“didDiscoverservice”
  • @LoganGallois 如果您没有获得任何服务,那么就没有服务,您应该创建一个服务并将其添加到您的外围设备中。
  • 我得到了一个,因为我在“didDiscoverservice”中收到了“找到服务”的消息
  • 调用 discoverServices 方法时会调用它,但您应该只使用外围设备检查服务。
  • 哦,好的。那么我如何只使用外围设备呢?使用discoverService(nil) ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-10
  • 2023-03-24
  • 1970-01-01
  • 2014-08-27
  • 2021-01-04
  • 1970-01-01
  • 2015-01-26
相关资源
最近更新 更多