iOS 獲取電池電量,屏幕亮度,藍(lán)牙狀態(tài)

1、電池電量

    func openBatteryListening(){
        UIDevice.current.isBatteryMonitoringEnabled = true
        NotificationCenter.default.addObserver(self, selector: #selector(BatteryChange), name:UIDevice.batteryLevelDidChangeNotification, object: nil)
    }
   @objc func BatteryChange(notification:NSNotification){
        print("電池電量:\(UIDevice.current.batteryLevel*100) %")
    }

2、屏幕亮度

  func openBrightnessListening(){
       NotificationCenter.default.addObserver(self, selector: #selector(brightnessChange), name:UIScreen.brightnessDidChangeNotification, object: nil)
   }
   
   @objc func brightnessChange(){
       print("屏幕亮度:\(String(format: "%.2f",UIScreen.main.brightness*100.0)) %")
   }

3、藍(lán)牙狀態(tài)

var _CBlueToothCentralManager:CBCentralManager?

func openBlueToothStateChangeListening(){
        _CBlueToothCentralManager = CBCentralManager(delegate: self, queue: nil)
 }    
func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if(central.state == CBManagerState.poweredOff){
            print("藍(lán)牙狀態(tài):關(guān)閉")
        }else{
            print("藍(lán)牙狀態(tài):開啟")
       }
}

4、獲取磁盤空間

func openFreeDiskspace(){
    var totalSpace = 0.0
    var totalFreeSpace = 0.0
    var totalUsedSpace = 0.0
        
   if let path:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last{
     do{
           let dictionary = try FileManager.default.attributesOfFileSystem(forPath: path)
           let fileSystemSizeInBytes = dictionary[FileAttributeKey.systemSize] as! NSNumber
           let freeFileSystemSizeInBytes = dictionary[FileAttributeKey.systemFreeSize] as! NSNumber
            totalSpace = Double(truncating: fileSystemSizeInBytes)
            totalFreeSpace = Double(truncating: freeFileSystemSizeInBytes)
            totalUsedSpace = totalSpace - totalFreeSpace;
                //
            let freePercent = totalFreeSpace/totalSpace;
            let usedPercent = totalUsedSpace/totalSpace;
                
            print("iphone磁盤未使用率 =\(String(format: "%.2f",freePercent*100.0))%")
            print("iphone磁盤使用率  =\(String(format: "%.2f",usedPercent*100.0)) %")
            print("iphone磁盤總大小  =\(((totalSpace/1000.0)/1000.0/1000.0)) GB")
            print("iphone磁盤已使用大小    =\(((totalUsedSpace/1000.0)/1000.0/1000.0)) GB")
            print("iphone磁盤剩余大小    =\(((totalFreeSpace/1000.0)/1000.0/1000.0)) GB")
                
            print("Memory Capacity of \(((totalSpace/1000.0)/1000.0/1000.0)) GB with \( ((totalFreeSpace/1000.0)/1000.0)/1000.0)) GB Free memory available.")
                
       }catch{
           print(error)
        }
     }  
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容