EASession、EAAccessoryDelegate 和“错误 - 打开会话失败”
我正在使用外部附件框架。我在应用程序进入后台然后返回前台后重新建立 EASession 时遇到问题。如果我终止我的应用程序并重新启动,那么蓝牙连接将按照人们的预期重新建立。我怀疑我遗漏了拆解的某些部分 - 或者没有暴露(??)。
[EAAccessoryManager共享AccessoryManager]connectedAccessories]]正在返回我连接的配件,我可以查询它以获取名称、型号等。但是,以下行将_session设置为nil。
_session = [[EASession alloc] initWithAccessory:_accessory forProtocol:_protocolString];
有没有办法诊断EASession初始化失败的原因?
有什么咒语可以清除旧的 EASession 吗?
这个问题与这个相关 - 但我并不是在寻求关于哪条路径的建议跟随。我想问为什么这条路有这么大的陷阱以及如何绕过它。
I am working with External Accessory framework. I am encountering problems reestablishing EASession after app goes into background and then returns to foreground. If I terminate my app and relaunch, then Bluetooth connection is reestablished as one would expect. I suspect that there is some part of the teardown that I am missing - or which is not exposed (??).
[EAAccessoryManager sharedAccessoryManager] connectedAccessories]] is returning my connected accessory, and I am able to query it to get name, modelNumber, etc. However, the following line sets _session to nil.
_session = [[EASession alloc] initWithAccessory:_accessory forProtocol:_protocolString];
Is there any way to diagnosis the reason for failed EASession initialization?
Is there some mantra for clearing out old EASession?
This question is related to this one - but I am not asking for advice on which path to follow. I am asking why this path has this large pitfall and how to navigate around it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现(在 iOS4.1 后的世界中)离开应用程序(后台或退出)将导致 DidDisconnectNotification 触发。在刚刚按下电源按钮或让设备休眠的情况下;我们没有看到连接断开。
现在,如果 BT 设备超出范围或自行进入睡眠状态。然后连接就会断开。
因此,除了 ConnectionNotifications 之外,我们不再依赖任何东西。我们甚至不信任
[[EAAccessoryManager sharedAccessoryManager]connectedAccessories]
列表,因为我们发现它有时可能包含“幽灵配件”,这些配件会说它们已连接并且具有您可以连接的流,并且即使在整个蓝牙系统关闭(BT 图标关闭)后,也可以获取写入可用事件。当您在后台时,ConnectionNotifications 会被缓存,因此当您重新进入应用程序时,您应该会获得新的状态。
当然是在第一次进入时;您要确保已正确设置所有侦听器(等)。
I've found (in the post iOS4.1 world) that leaving the app (Backgrounding or quitting) will cause a DidDisconnectNotification to fire off. In the case of just hitting the power button or having the device sleep; we don't see the connection go down.
Now if the BT device goes out of range or goes to sleep itself. Then the connection goes down.
As a result, we no longer depend on anything but the ConnectionNotifications. We don't even trust the
[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]
list because we've found it can sometimes contain 'ghost accessories' that will say they are connected and have streams that you can connect to and get write available events from even after the entire bluetooth system has gone down (BT icon off)ConnectionNotifications are cached when you are in the background, so you should get a fresh state when you re-enter the app.
Of course on first entry; you want to make sure that you've set up all your listeners (etc) properly.
据我所知,旧的 EASession 实例没有被清除。我可以猜测,但我真的不知道这是为什么。我怀疑已建立的 EASession 不会释放与其附件的关联 - 这会阻止后续 EASession 实例与同一附件成功关联。
当应用程序退出活动状态时,我选择保持 EASession 不变。这似乎有效。在测试中,我已连接到蓝牙配件、启动我的应用程序、访问配件、将我的应用程序发送到后台、断开/重新连接 BT 配件、将应用程序带到前台并能够再次访问配件。这是我所能希望的。
需要注意的是:在 iOS 4.0 中,可以拥有多个配件。例如,视频线和蓝牙设备都是配件。
As far as I can tell, there is no clearing out of old EASession instances. I can hypothesize, but I don't really know why this is. I suspect that the established EASession does not release its association with its accessory - and that this prevent subsequent EASession instances from being successfully associated with the same accessory.
I opted to leave EASession in tact when app resigns active. This seems to be working. In testing, I have connected to Bluetooth accessory, launched my app, accessed accessory, sent my app into background, disconnected/reconnected BT accessory, brought app to foreground and been able to access accessory again. This is as much as I could hope for.
One caveat: with iOS 4.0 it is possible to have more than one accessory. For example, a video cable and a Bluetooth device are both accessories.
我也遇到了同样的问题,并通过一个小技巧解决了它。我在打开会话时记录了附件的序列号,并在关闭会话时记录了该附件的序列号。
这就是我看到一些会话没有关闭的方式。因此,当我的应用程序进入后台模式时,我肯定会关闭设备所有打开的会话。
希望它对其他人有帮助。
I had the same problem and solved it with a little trick. I logged the serial number of the accessory when I opened a session and logged when I closed a session.
That's how I saw some sessions were not closed. So when my app go to background mode, I close definitely all the opened sessions by device.
Hope it helps someone else.
我也有同样的问题。我正在开发一个应用程序,它将按时间间隔打开外部蓝牙配件,读取一些数据,然后关闭流。这几天一直工作得很好,然后突然有一天它停止了解决这个确切的问题。控制台还记录了一条消息。
苹果文档很清楚,每个协议一次只允许打开一个实例,并且任何先前的实例都会自动释放。但是,为什么 iOS 突然无法释放之前的 EASession,而它已经工作了好几天了。
我很困惑,整整三天都在用头撞墙。我搜索了 Google,最终找到了这里,我阅读了 Apple 关于与外部配件通信的指南和 NSStream 编程指南。代码全部正确。
终于在第三天,我听到办公桌角落里传来一声嘟嘟声,我的安卓手机没电了。我脑子里的一个灯泡熄灭了。我关掉手机,中提琴,iOS 设备上的问题就消失了。
这实际上是一个 RF 问题,而不是编程问题,但我在这里包含了我的故事,以防其他人遇到这个问题,Google 会将您带到这里。关闭附近所有其他蓝牙无线电,因为它们是噪声源并可能导致此问题。
I had the same problem. I was developing an application would open an External Bluetooth Accessory on a timed interval, read some data, then close the streams. This had been working fine for a few days, then suddenly one day it stopped working with this exact problem. A message was also logged to the console.
The Apple documentation is clear, only one instance of each protocol is allowed to be open at a time and any previous instance is automatically deallocated. But why all of a sudden was iOS failing to dealloc the previous EASession when it had been working fine for days.
I was perplexed and I spent three days hitting my head against the wall. I searched Google and ended up here, I read Apple's guides on communicating with external accessories and the NSStream programming guide. The code was all correct.
Finally on the third day I heard a beep from the corner of my desk, my Android phone was running out of juice. A light bulb went off in my head. I turned the phone off and viola, the problem on the iOS device went away.
This is really an RF problem, not a programming problem, but I include my story here incase anyone else runs into this problem, Google will bring you here. Turn off all other Bluetooth radios in the vicinity because they are sources of noise and can lead to this problem.
我也遇到了同样的问题。就我而言,会话对象在创建后,retainCount = 3,因此在接收 DidDisconnectNotification 时,一次调用 [session release] 是不够的。确保您在收到 DidDisconnectNotification 通知后确实释放了会话对象。
I was heaving the same problem. In my case, the session object, just after creation, has retainCount = 3, so one call [session release] was not enough when receiving DidDisconnectNotification. Be sure you really released the session object after received DidDisconnectNotification notification.