3

everyone. I tried to access file "/sys/class/power_supply/Battery/current_now" to retrieve the battery current in Huawei P20.

f =  File("/sys/class/power_supply/Battery/current_now");
if (f.exists()) {
  return OneLineReader.getValue(f, true)

However i get the error of

java.io.FileNotFoundException: /sys/class/power_supply/Battery/current_now: open failed: EACCES (Permission denied)

i already grant permission of read and write to storage.

private val PERMISSIONS_STORAGE = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)

fun verifyStoragePermissions(activity: Activity) {
    // Check if we have write permission
    val permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        )
    }
}

Anyone has the hint for this problem?

Minipika
  • 31
  • 1
  • 2
  • current_now is file or folder??? – Learning Always Aug 23 '18 at 06:57
  • current_now is file – Minipika Aug 23 '18 at 06:58
  • what are the extension?? – Learning Always Aug 23 '18 at 07:00
  • i wonder that file is system file and need access through "root" or other way to read the file. – Minipika Aug 23 '18 at 07:00
  • i use the method in this page. https://stackoverflow.com/questions/2439619/getting-the-battery-current-values-for-the-android-phone – Minipika Aug 23 '18 at 07:01
  • You are trying to access system files, even if you grant external read and write permission, you probably won't be able to access it on non-rooted device. Also its part of the internal storage and not the external one. – Nakul Aug 23 '18 at 07:03
  • Other model of phone that use this method does not have any problem, such as XiaoMi. However, problem arises when using Huawei P20. I am not sure whether new version of android has new permission to this directory. – Minipika Aug 23 '18 at 07:03
  • 1
    Looks like targetSdk affects too. When I target on api 21 i can read the file even with enforced selinux and no root. But now i'm updating my app to targetSdk=28 and experiencing same issue. The working code doesn't work anymore. – deviant May 26 '20 at 16:15

1 Answers1

0

You need turn off selinux. Use adb shell "setenforce 0" and then you can read it from your app

gamarjoba
  • 55
  • 6