Swift Core 蓝牙 - discoveryServices - 如果找不到服务会发生什么?

发布于 2025-01-15 02:09:02 字数 492 浏览 4 评论 0原文

我正在 Swift 中使用核心蓝牙,我想知道在调用 discoverServices([myServiceUUIDs]) 后会发生什么情况,以防找不到匹配的服务。 connect(_peripheral: CBPeripheral, options: [String: Any]? = nil)didConnectdidFailToConnect。所以我有一个案例,如果失败就会被调用。 但在 discoverServices() 上,文档仅说明

当外设发现一个或多个服务时,它会调用其委托对象的peripheral(_:didDiscoverServices:)方法。

因此,在“幸运的情况”下,它找到了我可以继续做事情的服务。但我怎么知道它找不到任何服务呢?是否还有一种方法或者我必须使用计时器并在定义的时间后取消并假设没有我正在寻找的服务?

I'm working with Core Bluetooth in Swift and I'm wondering what happens after I called discoverServices([myServiceUUIDs]) in case that no services that are matching can be found.
The connect(_ peripheral: CBPeripheral, options: [String: Any]? = nil) has didConnect and didFailToConnect. So I have a case that gets called if it fails.
But on discoverServices() the documentation says only

When the peripheral discovers one or more services, it calls the peripheral(_:didDiscoverServices:) method of its delegate object.

So in the "happy case" that it finds the services I can continue doing stuff. But how do I know that it can't find any services? Is there also a method for or do I have to use a Timer and just cancel after a defined time and assume that there is not the service I was looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2025-01-22 02:09:02

它会一直寻找,直到您将其关闭。您必须使用计时器或给用户一个停止搜索的按钮(或两者兼而有之)。

其他

在阅读了一些评论后,可能需要进行类比......想象一下,如果有人告诉您“找到所有蓝色鸡蛋” 。你什么时候停下来?

如果您回答“当我找到所有蓝色鸡蛋时我会停下来”,您怎么知道您已找到所有蓝色鸡蛋?你怎么知道在你寻找的时候有人不会藏更多的蓝蛋?

你不知道,所以你一直寻找,直到你被告知停止。

为什么会“出错”?只有当你由于某种原因无法寻找鸡蛋时,你才会出错,也许你在沙发底下寻找时被卡住了......

It keeps looking until you turn it off. You have to use a timer or give the user a button to stop searching (or both.)

Additional

After reading some of the comments, an analogy might be in order... Imagine if someone told you to "find all the blue eggs". When would you stop?

If you answer, "I would stop when I found all the blue eggs," how do you know you found them all? How do you know someone won't hide more blue eggs while you are looking?

You can't know, so you keep looking until your told to stop.

Why would you "error"? You would only error if you can't look for eggs for some reason, maybe you got stuck while looking under the couch...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文