How to continue BLE on the next view controller

I am developing an application that scans neighboring BLE devices, connects to one of them and communicates with the connected device. I tried to do this in 2 view controllers, controller-A and controller-B. Controller-A will scan neighboring devices and connect to one of them. Controller-B will exchange data to the connected device. Controller-A extends CBCentralManagerDelegate. My problem: when my application switches to view controller-B, it does not receive callbacks from CBCentralManager. I have to initialize the CBCentralManager in Controller-B again. I also have to disconnect the device from controller -A and reconnect to controller-B. Is there a better way to do this? Thanks.

+4
swift bluetooth bluetooth-lowenergy
source share
1 answer

Put your BLE related code in a centralized place, for example. BLEMaganer (better) or AppDelegate (so on). In this way, controller and controller B can share the same instance of centrolManager.

For example, you currently have the centralManager property in controller A and implements its delegate in controller A. You access centralManager using controllerA.centralManager .

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

 (UIApplication.sharedApplication().delegate as! AppDelegate).centralManager. 
+6
source share

All Articles