Battery level

In xCode for iOS, an older example to read the battery level is

    device.readBatteryLifeAsync().success { result in
        self.batteryLevelLabel.text = result.stringValue
    }

I cannot seem to find this function in the latest MetaWear API.

Please advise.

Comments

  • Hi Eric,
    Thanks for that. Is there a Swift equivalent to the C++ code ?

  • pls share the swift code

  • @gowtham, you need to write the swift code yourself.

  • @Laura said:
    @gowtham, you need to write the swift code yourself.

    can you pls share with me please in urgent

  • If you need help to write code, we do offer services: www.mbientlab.com/services.
    Otherwise maybe you have a developer friend or colleague that can help you?

  • can you please share code how to check battery level in swift

  • let battery_signal = mbl_mw_settings_get_battery_state_data_signal(DiagnosticVC.deviceBoard.board)

        mbl_mw_datasignal_subscribe(battery_signal!, nil) { (context, data) in
             let state = MblMwBatteryState()
            print(state.charge)
        }
    
        mbl_mw_datasignal_read(battery_signal)
    
  • @sanjeev said:
    let battery_signal = mbl_mw_settings_get_battery_state_data_signal(DiagnosticVC.deviceBoard.board)

        mbl_mw_datasignal_subscribe(battery_signal!, nil) { (context, data) in
             let state = MblMwBatteryState()
            print(state.charge)
        }
        
        mbl_mw_datasignal_read(battery_signal)
    

    I used this code

  • Thanks sanjeev

  • edited January 2020
            guard let board = device?.board else {return}
            guard mbl_mw_metawearboard_lookup_module(board, MBL_MW_MODULE_SETTINGS) != MODULE_TYPE_NA else {  return }
            let signal = mbl_mw_settings_get_battery_state_data_signal(board)
            mbl_mw_datasignal_subscribe(signal, bridge(obj: self)) {(context, data) in
                        let _self: viewController = bridge(ptr: context!)
                        let state = data!.pointee.valueAs() as MblMwBatteryState
                        print("Battery charge : \(state.charge)%")
    /*
    // if you want to pass this value (state.charge) to other swift function
                DispatchQueue.main.async {
                        _self.updateBatteryChargeLabel(Int(state.charge))
                  }
    */
                    }
            mbl_mw_datasignal_read(signal)
    
Sign In or Register to comment.