Android How to measure app power consumption when plugged - android

How to measure app power consumption when I connect my device with my pc?
I've read about batterystats from Android Studio. And I've followed the instructions and get a batterystats dumpsys. However, I can't not let my device consistently plugged on usb port because the result of dumpsys will have a missing part called "Estimated power use". Which is the most important part of the whole test because the app power consumption it contained.
I've tried using StopCharge app to interrupt phone charging. But the prerequisite is that the device need to get ROOT.
Other options like power tutor is outdated. Information out from the application can't quite fit my request(Android 6.0 or upper).
Is it possible to get the power consumption measurement of an application when it's charging or is my mind out of the roof?
Thank you in advance!
p.s the following is a sample of the method of Android Studio batterystats
Estimated power use (mAh)
Capacity: 2930, Computed drain: 43.9, actual drain: 58.6-87.9
Screen: 35.6
Unaccounted: 14.7 ( )
Uid u0a2618: 2.91 ( cpu=2.17 wake=0.657 wifi=0.00656 sensor=0.0732 )

I think when you plug, the battery stats are not very accurate.
There's two ways i usually do:
Connect the adb by wi-fi and run batterystats
In Android Studio go to preferences->Plugins and search for Wi-fi, this way you connect the device on USB, go to plugin and he do the wifi connection
Or if you don't wanna a new plugin
Run adb tcpip 5555 and adb connect 192.168.0.101:5555 if you already have usb access (More info.)
-
Use the Google Battery Historian:
In this link, download the project following the guidelines
In your adb, run adb shell dumpsys batterystats --reset to clear all the history
Use your App for some time
Go back on ADB an generate the log: adb bugreport > bugreport.txt
Then Upload the file to the Battery Historian site that you will host by following the instructions, and you are done.
Image of the Battery Historian:

Related

Logging when Android device is disconnected

Logcat works fine when device is connected to Android Studio. But how to log data when device is disconnected from Android Studio, but I still need to capture some information for later analysis?
This does NOT require rooting your device, NOT changing the code of your app, NOT subscribing to a third party service, NOT having your app in Google Play. If you can or are willing to do some of these things, there are other solutions.
However this solution DOES require to have the computer nearby, turned on and connected to the same network where the device is connected (not necessarily on the internet).
Start off by plugging the device into your computer via USB, then run
adb devices to make sure the device is there. If it finds more than one device (e.g. emulator and actual phone), disconnect/close everything but the one you want to debug to keep things simpler and not mistaken one device for another. Then, run adb tcpip 5555 to change the android to tcp mode, which will allow debugging over the network (instead of USB).
At that point you need to find the IP address of your phone, and you can do that in various ways. The easiest is running adb shell ip -f inet addr show wlan0 which will respond with something like:
11: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 192.168.x.x/yy brd 192.168.0.255 scope global wlan0
valid_lft forever preferred_lft forever
Disconnect the device from the computer's USB in the usual way. Connect it again "over the air" by typing adb connect 192.168.x.x, replacing the x with the IP address found above (right after inet). If the device and your computer are on the same network (e.g. same wifi, or mix of wifi and ethernet from the same router) it should connect. Otherwise, you might need to debug the networking, a whole different problem (but usually not necessary).
Sometimes you can connect to the device but it is shown as "offline". Most of the time just waiting one minute will automatically fix the issue. If in a minute or two the issue does not fix itself, you have to debug it, e.g. as described in this question
At this point the device should appear in the drop list of available devices in Android Studio and you should be able to debug it as if it were attached over the USB. However in some cases I have found that the logcat in Android Studio is not updated in this setup. If that happens, run adb logcat and you will see the logcat on the computer. That will have too much information, because includes everything is running on your device, not just your app -- so you will need to filter it, for example with adb shell logcat --pid NNNN where NNNN is the PID of your app. To find such a PID, run adb shell "ps -A | head -1; ps -A | grep -i com.example" (use the java package of your main Activity instead of com.example) which will respond with something like
USER PID PPID VSZ RSS WCHAN ADDR S NAME
u0_a105 NNNN 1511 4970004 182688 0 0 S com.example
When done, run adb usb to restore the phone to regular USB debugging.

Live capture of Android bluetooth traffic via Wireshark

As the title suggests, I would like to analyze the bluetooth traffic of my android device on wireshark, I was able to do that just a few hours ago by just connecting the Android device via usb and opening up wireshark, the android bluetooth interface would just shouw up and worked perfectly, after some time (I rebooted the phone and did a couple other things), the bluetooth interface was gone. I'm now able to only access the phone Logcat.
I think I disabled something, or have to do some steps in order to activate this feature, that I had activated before without knowing, but I can't seem to replicate that, and the only things I find online are about saving HCI log in a file and then analyzing them on wireshark, which is not ehat I'm looking for right now
I too was looking for a way to do this and I think the best way I've found so far is to use wireshark's androiddump tool: https://www.wireshark.org/docs/man-pages/androiddump.html
It's built in with wireshark and if your phone is plugged in with adb installed and configured correctly so that you can do an adb shell from your computer, then when you open wireshark your android's bluetooth interface should show up as possible source of capture as soon as you open wireshark.
You were probably using Wireshark extcap androiddump option which has three requirements:
rooted device
developer option Bluetooth HCI snoop enabled
adb server running
From your description, you simply opened wireshark and did not start Android server
Please do the following:
enable developer option Bluetooth HCI snoop - you can do so by
tapping your device serial number in your settings menu
From your terminal execute the following command: $adb start-server when you are finished you can use $adb kill-server

Command to clear the previous dumpsys info

Question is straight forward and simple. Is it possible to get the dumpsys information after certain point or certain user action. Not from the scratch.
There is no such thing.
Logs belong to the logcat facility. dumpsys is mostly for reporting the current state and some more advanced things. It is true that few of the Android service developers included some limited logging excerpts into their dumpsys output but virtually none of them implemented a way to clear those logs.
I realise this is a bit late but...
As per the documentation:
Connect your mobile device to your computer.
Open a terminal and type the following commands to reset the adb server:
adb kill-server
adb devices
This will list any connected devices (If you don't see any devices listed, make sure your phone is connected, and USB Debugging is turned on, and then kill and restart adb again).
Next you will need to reset battery data gathering. The device is always collecting batterystats and other debugging information in the background. Resetting erases old battery collection data. If you do not reset, the output will be huge.
In terminal use command:
adb shell dumpsys batterystats --reset
Disconnect your device from your computer so that you are only drawing current from the device's battery.
Play with your app and perform actions for which you would like data; for example, disconnect from WiFi and send data to the cloud.
Reconnect your phone and make sure it is recognized:
adb devices
Dump all battery data (This can take a while):
adb shell dumpsys batterystats > [path/]batterystats.txt
The batterystats.txt file is created in the directory you specify using the optional path argument. If you leave out [path/], the file is created in your home directory.
You can find the home directory in Android Studio by going to:
Tools > SDK Manager > Android SDK Location.
Navigate to that filepath then open the subdirectory 'platform-tools' and look for batterystats.txt.

how to run adb command on my phone without giving any current to the phone

I want to measure the current consumption of my device while running different test cases. (Have a tool to measure current consumption).
I have automated the test cases using UIAutomator. But for running the script I have to connect the device via USB or WiFi. In both cases current consumption of the device gets higher.
Is there any method by which I can stop the current coming from USB and run the script?
I have tried:
adb shell dumpsys battery set usb 0
This stops the charging through USB but the current reading is still more, because I think device is still getting the current it only stops the battery charging.
Connect with ADB over wifi. Start a test case that doesn't do anything and measure current increase (consider doing this multiple times). That's your baseline. When you run the real test, however much the current increases above that is due to the test.
My problem is solved. I used:
adb shell
uiautomator runtest projectname.jar -c pkg.cls &
Because of &, the script keeps running even when I disconnect the USB cable

How to disable battery charging during ADB connection?

Problem description:
Each time wen I connect USB cable between PC and my mobile phone the battery automatically is charged. I want to use ADB protocol but I don't want to charge my battery during ADB connection. Is it possible to to turn off this charging? And ofcourse how can I do this?
Environment:
Mobile phone with Android os 4 and higher
I need only mention that my referential device working on android 5 so there is no /sys/class/power_supply/battery/force_usb_charging file
BEWARE: adb shell dumpsys battery unplug only MOCKS the battery status. I checked with a current indicator on my usb cable and this does not change the amount of flowing current at all so the battery is still charging.
It only changes what applications think about the state, so for example google play would not start updating if you have configured that it can only update when charging...
Changing the value of a file like /sys/devices/battery.XX/power_supply/battery/hv_charger_set to 0 does really work but its a different file for every device. For example, for my yotaphone 2 with a snapdragon 805 there is a file: /sys/devices/qpnp-charger-14/power_supply/battery/charging_enabled
So you could make a script that you can use in tasker(if you have root):
echo $1 > /sys/devices/qpnp-charger-14/power_supply/battery/charging_enabled
if you write that to an executable file /data/setcharging.sh then you could call the script like /data/setcharging.sh 0 to disable or
/data/setcharging.sh 1 to enable.
This works, I checked with my current metering usb cable, but it does not show that it works in the battery status icon. So you could use a combination of this and adb shell dumpsys battery unplug / reset to make it look better, but the problem is that then apps like tasker don't know whether you have ac power or not, so you can not put triggers for that when you've set it to unplugged...
One more thing, I also tested what it does in recovery (twrp) and during boot. And it turns out that during boot and in recovery, the charging just works. So its not like your device will end up dead if you forget to turn on charging again. You can always charge it in recovery mode and then boot up and change the value again (or change it while in recovery if your recovery gives access to adb).
Lastly, there are apps on the play store that are made exactly for this, but I would recommend getting such a current indicating usb-cable and checking if they really do what they say and are not just mocking.
You can make the system think it is not loading th battery using
adb shell dumpsys battery unplug
Refer to this good article :
https://stanfy.com/blog/android-shell-part-1-mocking-battery-status/
Try this for rooted device:
To enable battery Charging:
adb shell dumpsys battery set ac 1
adb shell dumpsys battery set usb 1
adb shell dumpsys battery set wireless 1
To disable battery Charging:
adb shell dumpsys battery set ac 0
adb shell dumpsys battery set usb 0
adb shell dumpsys battery set wireless 0
Probably it can be done via (root privileges):
/sys/devices/battery.XX/power_supply/battery/hv_charger_set
0 - Disable USB Charging
1 - Enable USB Charging
if android os version is more than 6 use unplug and reset to revert back.
adb shell dumpsys battery unplug
adb shell dumpsys battery reset
Copied from perfetto.dev
On rooted phones the power management IC driver allows to disconnect the USB charging while keeping the USB data link active. This feature is SoC-specific, is undocumented and not exposed through any HAL. For instance on a Pixel 2 this can be achieved running, as root: echo 1 > /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998#2:qcom,qpnp-smb2/power_supply/battery/input_suspend. Note that in most devices the kernel USB driver holds a wakelock to keep the USB data link active, so the device will never fully suspend even when turning the screen off.

Categories

Resources