dimanche 12 février 2017

How to Force ‘OK Google’ Hotword Detection to work when the Screen is Off on any Device [Root]

When the Moto X (2013) was first announced, one of the most exciting features (besides Ambient Display) was its ability to wake up to voice commands.

Later introduced in Google Nexus devices and now available in many flagships, hotword recognition capability is a pretty awesome feature for when you need to make a quick Google search without fumbling with the device in your hands. This can be exceptionally useful when you’ve docked your phone in your car and you need to start navigation to a certain destination. It is by far safer to use your voice to start Google Maps navigation than any other alternative.

Unfortunately, being able to trigger “OK Google” commands at all times requires a special, low power voice recognition chip in your device. While this hardware is present in many devices, it is not present in them all. Even powerful, expensive flagships like the Huawei Mate 9 do not offer this offer. For devices such as mine, Google offers the ability to trigger “OK Google” voice commands while the screen is on or when the device is plugged in to a charger.

A few years back, there was an application by the name of Open Mic+ for Google Now which had the option to enable a background service to listen for voice commands at all times. Unfortunately, Google requested that the developer stop using the service and the developer no longer had any time to support that app, so it fell by the wayside. The only existing option is to use AutoVoice, a popular Tasker plugin, but this requires you to have enough Tasker knowledge to set up profiles to respond to specific commands.

For those of you who would prefer to rely on Google’s voice recognition service, I’ve found a workaround that enables “OK Google” hotword detection on any rooted device, even if its hardware doesn’t support it. There are two caveats to this trick, which we will discuss in detail below.

“OK Google” Hotword Detection on any Device

The question I asked myself when thinking of a workaround to this problem was:

How can I trick my device into enabling the hotword recognition service on my command?

Since my current phone does not natively support hotword detection while the screen is off, the only conditions to make this work would be when my phone’s screen is on or when it is charging. Naturally, since my goal is to enable hotword detection at all times (even while the screen is off), having the screen be on would defeat that purpose. Having to leave my device plugged in would also be pointless in my endeavor, but what if I could trick my device into meeting either of these two conditions?

Luckily, with a boot of root magic and a clever use of debug commands, this is very much possible! Using a debug shell command meant for testing purposes, I am able to trick my device into thinking it is charging even though it is unplugged from any power source. The command we will be exploiting for our purposes is dumpsys battery, the parameters of which you can read about in this blog post by Roman Mazur.

Note: this trick is completely safe to perform. As is stated, this command only TRICKS your device into thinking it is charging. It is NOT actually charging, even if battery monitoring applications say otherwise (this is because these apps will be fed incorrect information.)

Of particular note is the dumpsys battery set command, which accepts parameters to set whether or not the device is currently being charged via AC, USB, or wireless charging. For instance, if we open a root shell in Android and enter the following command, the device will think it is being charged via AC power.

dumpsys battery set ac 1

By setting this command (or the command for USB/wireless charging), you will now be able to trigger OK Google commands while your screen is off. This is because for all intents and purposes, your device is “charging” – meeting the requirements for Google’s hotword recognition service to activate.

Since this is a command intended for debugging (mainly intended for developers to test functionality under different battery conditions), it has some drawbacks when used in this manner. Two drawbacks, in particular, which we will address next. The first drawback is rather trivial to work around, but the same can’t be said for the second drawback.

Drawback #1 – Frozen Battery Indicator

After entering any “dumpsys battery set” command, Android’s BatteryManager service will immediately stop collecting any further data on your battery state. This means your battery level, temperature, current, voltage, health will no longer be reported on by the Android system. They will instead be “frozen” in time at the moment you entered the command.

However, this data is still being collected by the system even if it is accessible by any application. If you want to get an update on what your current battery level is at, you will need to do some trickery. Fortunately, the data is quite easy to pull. If you have Tasker or another automation application, all you need to do is create a notification that updates on a tap with the current text stored in /sys/class/power_supply/battery/capacity.

In the above screenshot, my battery indicator (that is visible when I pull down the status bar) is stuck at 70%, but as you can see in the terminal, my actual battery level is 69%. It would be easy to regularly poll this file to post an accurate battery level in my notification bar, but there’s an even easier way to work around this setback.

Dealing with this particular drawback is actually incredibly trivial. If you think about it, this issue only presents itself when the command is enabled and when the screen is on. But the thing is, you don’t need to have this command enabled when the screen is on, because the default functionality allows you to access “OK Google” commands when the screen is on. Thus, all you need to do is simply disable this command when the screen is on. Using Tasker or another automation app would probably be the simplest way. Simply run this command to disable the trick:

dumpsys battery reset

And below you’ll find the description of the two Tasker profiles you would need to setup in order to run this command when the screen is off but disable it when the screen is on. In essence, you will be creating two “Event” contexts, one of which is the “Display Off” Event while the other is the “Display Unlocked” Event. The Task for the “Display Off” Event will have a single action in it, Code–>Run Shell with the command dumpsys battery set ac 1. The Task for the “Display Unlocked” Event will also have a single action in it, Code–>Run Shell with the command dumpsys battery reset. Thus, Tasker will run the command to trick your device into thinking it is charging while the screen is off (profile: Enable Hotword Detection) and run the command to disable this trick when the phone is unlocked (profile: Disable Hotword Detection).

Profile: Enable Hotword Detection (180)
        Event: Display Off
Enter: Anon (182)
        A1: Run Shell [ Command:dumpsys battery set ac 1 Timeout (Seconds):0 Use Root:On Store Output In: Store Errors In: Store Result In: ]
Profile: Disable Hotword Detection (191)
        Event: Display Unlocked
Enter: Anon (192)
        A1: Run Shell [ Command:dumpsys battery reset Timeout (Seconds):0 Use Root:On Store Output In: Store Errors In: Store Result In: ]

Drawback #2 – Increased Battery Drain

There’s a good reason why hotword detection at all times requires a custom co-processor, it’s because otherwise it would result in additional battery drain from the CPU having to stay awake. When you trick your device into thinking it is charging, this means that the device keeps the processor running and runs more background services because it is under the assumption that the increased power draw won’t matter due to the device having access to a power source.

But that isn’t the case here, so enabling this command will result in increased battery drain. In my experience leaving my device in this state overnight, my Mate 9 drained 12% battery over the course of 8 hours.

Hardly a deal-breaker in my opinion, but this may deter some of you out there. Unfortunately, there is no easy workaround to deal with this second drawback. Disabling the command while the screen is on, as per the last paragraph mentioned in the section above, will help mitigate the issue some, but that’s about all you can do.


I hope you find this trick useful. It’s certainly not for everybody, but if you’ve ever wanted to force OK Google hotword detection to work and your device doesn’t have the requisite hardware, this workaround is for you.

I’ve outlined the two potential drawbacks to this trick, the first of which can be solved by using an automation application to enable/disable the command when it makes sense to do so. The latter drawback is unfortunately a result of the fact that your hardware isn’t built for always-on hotword detection, but if you use this only when you really need it, then it shouldn’t be too much of an issue.

Give this trick a shot and let us know in the comments below if it works for you!

from xda-developers http://ift.tt/2lykWBk