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.
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
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.
For example, currently you have a property
centralManager
in controllerA, and implement its delegate in controllerA. You access centralManager bycontrollerA.centralManager
.Move the
centralManager
property toAppDelegate
, as well other related code. Then you can access centrolManager instance by