hi i hvae a bluetooth singleton class, i want to pass my ble connect status to my viewcontroller when updatevalue. i think need some listener.
my Bluetooth class is here
class Bluetooth:NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
static var instance = Bluetooth()
func initBluetoothSingleton(){}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic.uuid == BLE_WIFI_EVENT_CHARACTERISTIC_CBUUID {
let wifiEvent = ReadWiFiEvent(using: characteristic)
if(wifiEvent == "connect"){
// i want to pass data to my viewcontroller to show alert
}
}
}
func handlerBleStatus(completion: (status:String) -> ()) {
completion(status: "connect")
}
and my viewcontroller is like this in viewdidload or somewhere
Bluetooth.instance.handlerBleStatus { (status) in
print(status) //how to set a listener here
}
i want set a listener when my ble updatevalue then show alert let user know the status is this situation need user protocol?
thanks
You can send values in multiple ways. Using protocols, closures, or notification center.
I believe using a closure would work fine for your case.
Define a variable in the Bluetooth
class
static var instance = Bluetooth()
var handleBleStatus: ((String) -> Void)?
Set the closure in your view controller/model.
Bluetooth.instance.handleBleStatus = { value in
print(value)
// TODO: Display alert
}
Then you can send values from the Bluetooth
class using the closure. The view you set the closure variable will get the update.
if(wifiEvent == "connect"){
// i want to pass data to my viewcontroller to show alert
handleBleStatus?("Status: Connected")
}