如何将 BLE 活动继续到下一个视图控制器 [英] How to continue BLE activities onto next view controller

查看:23
本文介绍了如何将 BLE 活动继续到下一个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款应用程序,该应用程序可扫描附近的 BLE 设备、连接到其中一个设备并与连接的设备通信.我尝试在 2 个视图控制器中执行此操作,即控制器 A 和控制器 B.控制器-A 将扫描附近的设备并连接到其中之一.控制器 B 将与连接的设备交换数据.控制器-A 扩展了 CBCentralManagerDelegate.我的问题是:当我的应用程序切换到视图控制器 B 时,它没有从 CBCentralManager 获得回调.我必须在控制器 B 中再次初始化 CBCentralManager.我还必须将设备与控制器 A 断开连接,然后在控制器 B 中重新连接.有没有更好的方法来做到这一点?谢谢.

I am developing an app which scans for nearby BLE devices, connect to one of them, and communicate to the connected device. I tried to do it in 2 view controllers, controller-A and controller-B. Controller-A would scan for nearby devices and connect to one of them. Controller-B would exchange data to the connected device. Controller-A extends CBCentralManagerDelegate. My problem is: when my app switch to view controller-B, it does not get the call-backs from CBCentralManager. I have to initialize CBCentralManager again in controller-B. I also have to disconnected the device from controller-A, and reconnect in controller-B. Is there a better way to do it? Thanks.

推荐答案

把你的 BLE 相关代码放在一个集中的地方,例如BLEMaganer(更好)或 AppDelegate(如此).这样控制器A和控制器B就可以共享同一个centrolManager实例.

Put your BLE related code into a centralized place, e.g. BLEMaganer (better) or AppDelegate (so so). So that controllerA and controllerB can share the same centrolManager instance.

例如,当前您在 controllerA 中有一个属性 centralManager,并在 controllerA 中实现其委托.您可以通过 controllerA.centralManager 访问 centralManager.

For example, currently you have a property centralManager in controllerA, and implement its delegate in controllerA. You access centralManager by controllerA.centralManager.

centralManager 属性移动到 AppDelegate,以及其他相关代码.然后你可以通过

Move the centralManager property to AppDelegate, as well other related code. Then you can access centrolManager instance by

(UIApplication.sharedApplication().delegate as! AppDelegate).centralManager.

这篇关于如何将 BLE 活动继续到下一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆