I need to stop and then start (restart) my MainService when the user plugs in the phone's charger. To do this, I have a so-called GodService that registers a receiver for the ACTION_BATTERY_CHANGED intent, and then I restart MainService within that BroadcastReceiver.
I can post the code to that if necessary, but that seems to work fine, since, as soon as I start debugging I'm charging, and I can step through and see MainService get stopped and restarted.
My question is, how can I test this multiple times in a row (which I need to do), since I'll have to unplug the device, thus stopping my debugging session. I'm sure there must be a way to debug the charging port removal, but I must not be searching with the correct terms.
You can actually simulate the phone disconnect and reconnect via ADB. Here is the relevant code:
Prior to Android 6+:
Disconnect USB
$ adb shell dumpsys battery set usb 0
Re-connect USB
$ adb shell dumpsys battery set usb 1
For Android 6+:
Disconnect USB
$ adb shell dumpsys battery unplug
Re-connect USB
$ adb shell dumpsys battery reset
EDIT:
Found a nice tuturial on how to mock different battery statuses. https://stanfy.com/blog/android-shell-part-1-mocking-battery-status/
Related
I am playing around with the Android doze mode in my app and just wants to examine my apps behavior in doze mode.
My application enqueues some tasks in a job scheduler while in doze mode. I want to check if any event, that wakes up the device, triggers the jobs that my app had put in the job scheduler while in doze.
I programmatically put the device in doze mode by firing the below commands-
adb shell dumpsys battery unplug
adb shell dumpsys deviceidle enable
adb shell dumpsys deviceidle force-idle
I also tried the other set of commands to put it in the doze mode
adb shell dumpsys deviceidle step deep
The above commands put the device in deep idle mode and I check for it with the command-
adb shell dumpsys deviceidle get deep
output: IDLE
But even if I give a phone call, which turns the device screen on and vibrates the device, the device status doesn't change. The above command continuous to give me IDLE status.
Question:
Can I emulate the scenario where I put the device in doze(by firing some commands) and then the device would move out of doze by some events that are stated by the android doc (moving it, turning on the screen, or connecting a charger).
EDIT
My observations after doing a lot of research-
When we program a device to go in the deep idle mode, the device doesn't come out of doze mode unless explicitly done using the command-
adb shell dumpsys deviceidle disable
So, even if you a send a text message or call the device which would cause
- the device screen to turn on
- the device to vibrate
but because if is programmatically set to idle mode, it continuous to remain in the same state.
I believe the problem is that you are using force-idle.
When you do that the device enters IDLE mode until you "unforce" it with:
adb shell dumpsys deviceidle unforce
You can check for alternative ways of entering IDLE mode from adb in the docs or in other question, like here:
How to shift device in Doze Mode (Android Preview M / Marshmallow)?
Currently I'm working on an app which does Bluetooth scanning in background service.
I realized that the phones behaviour is different if I force the device to sleep mode by
adb shell dumpsys battery unplug
adb shell dumpsys deviceidle force-idle
or unplug the physical cable from the device.
I can't find information about that there is a difference. But I recognised a difference already on different devices.
Is there a way to put the device in a I-physical-unplug-and-put-screen-off-mode and still be able to debug with logcat?
Thank you very much.
I am seeing an issue with my android images where the command adb reboot bootloader simply reboots back the android, instead of going to bootloader mode.
In order to fix the issue, I did some study and find that there are acually two things, adb and adbd and the host and target devices communicate using the TCP protocol over sockets.
So, the interesting thing is commands like adb shell and adb devices are working but not the reboot bootloader. I want to understand what the adbd on receiving the reboot bootloader. Does it change the boor order, sets some flag, changes EFI vars....?
Can you please point to some good links or understanding you can share?
PS : I am working on embedded device environ, similar to raspberry pi...
This is how adb reboot bootloader works on a standard Android device connected via USB (the only transport supported by the standard Android bootloader in the fastboot mode):
adb client sends the reboot bootloader command to the adb server (over TCP)
adb server forwards the reboot bootloader command to the adbd on the device (over USB)
adbd sets the sys.powerctl property to reboot,bootloader
sys.powerctl change triggers the init.rc rule which runs powerctl init's built-in
which does _NR_reboot syscall
which sets the reboot to bootloader flag and reboots the device
On the next power up the bootloader would see the flag and go to the fastboot mode. But only if USB is connected.
I'm using Android tablet communicating with a USB device through USB port, the tablet only has one usb port, so I use adb wifi as my debug tool. However, when I connect adb through WIFI, the connection won't keep long(only a few minutes us usually), how to keep the adb wifi connection online all the time?
Another question, is it possible to use USB HUB to connect the tablet with both USB flash-drive and my computer, and keep adb running?
Many thanks!!!
After lots of unsuccessful attempts,I have noticed that turning Allow ADB debugging in charge only mode specification on , solved the problem . It makes the device online and accessable over wifi to ADB.
You can find it under Developer options section.
Here is what I found out from researching everywhere and developing on my own for more than a week.
Q1. adb service often crashes if it is not doing any executions for a long long time.
A1. calling adb commands time to time keeps adb server online.
Q2. wifi connected device sometimes disconnect.
A2. Before you check your wifi on the connected device, check the wifi that your ADB server is running on. Ping to google and if it is well connected, ping to the device.
Wireless can disconnect on its own and even wired connection is sometimes disconnected. so calling "adb connect ..." when the device is not already connected is necessary. You could do this easily by saving "adb devices > device.txt" and query out whether your expected wifi is on the list. If not, connect again..(do this in another thread).
Q3. wifi connected device is sometimes offline.
A3. This is because by TCP/IP connection your ADB server is now on Time_Wait stance. The last connection was interrupted that the next connection is now offline because the last connection is not completely shut down. You can either manually disconnect/connect your device's wifi or restart TCPIP connection by ex) "adb tcpip 5555"
If you want to do this automatically, you are going to have to disconnect/connect your device's wifi programmatically.
Leave any other questions and I can answer on the comments.
When connection is lost and adb devices shows device is offline, like this:
prompt> adb devices
List of devices attached
192.168.1.1:5555 offline
then adb disconnect IP followed by adb connect IP will often work:
prompt> adb disconnect 192.168.1.1
disconnected 192.168.1.1
prompt> adb devices
List of devices attached
prompt> adb connect 192.168.1.1
connected to 192.168.1.1
prompt> adb devices
List of devices attached
192.168.1.1:5555 device
Use adb over wi-fi without any third-party apps. Follow the steps.
Connect device via USB
Open your terminal and check your device is listed with adb devices command
Type adb tcpip 5555 and enter (or use another port instead of 5555)
Plug out your USB cable. You don't need it anymore.
Take your Android phone and go Settings/About/Status
Check the IP address part. Let's say your IP address is 192.168.x.y
In your terminal, write the command adb connect 192.168.x.y
Now, you have connected your device via wi-fi.
You can see your device in adb devices
Enjoy it! :)
This seems like an issue with your wifi. As a precaution, just keep the terminal open and whenever you need to test just press the 'UP arrow' key to get to previously ran adb connect "<ip>" command.
I tried many ways but this one worked out for me:
force the display on and of in a loop would keep the device online
adb shell input keyevent 224
timeout 2
adb shell input keyevent 26
timeout 58
this turns the display on every minute for 2 seconds
So, I'm using Scrcpy and Adb to wirelessly connect to my Phone(for developing Flutter Apps) and since the screen wasn't staying awake even by Scrcpy --stay-awake command.
I had to change Lock Settings=>Lock Screen => Sleep, and settings to Never Sleep.
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.