Enabling Mock Locations in Android on Chrome OS

On a Chromebook in Developer Mode, it appears to be possible to successfully enable mock locations for Android via the command prompt. I tested the method in Terminal Emulator (which requires root in Android), but it should also be possible to do in the Chrome OS shell via adb.

In Developer mode, with adb debugging switched on in the Android Developer options, if we connect to Android via adb in the Chrome OS shell
sudo su -
adb connect 100.115.92.2
We can then switch on mock locations via adb
adb shell settings put secure mock_location 1
If a suitable fake location Android app (e.g. https://play.google.com/store/apps/details?id=com.lexa.fakegps) is installed on the device, this can be set as the default mock locations app
adb shell appops set com.lexa.fakegps android:mock_location allow  
To set a different app as the mock locations app, change the package name/App ID (com.lexa.fakegps in the example above) to the appropriate package name (as shown on the app's Play Store URL).

After setting Fake GPS as the mock locations app, I was able to select my mock location in Fake GPS and e.g. Maps in Opera would successfully show the mock location, rather than my current location. I haven't really tested in many other apps at this time. 

I understand that some Android apps may specifically check if mock locations are being used. For those apps, I imagine further steps would probably be required, such as perhaps installing (possibly a different app such as GPS JoyStick) as a system app. This, however, would require remaking the Android rootfs container as a R/W image. If this is required, the rooting script can be run, which, as part of the rooting process, replaces the Android read-only rootfs with a R/W copy.

26 comments:

  1. I'm very excited about this - if I can get Mock Locations working properly on my Samsung Chromebook Pro, it will be the final piece of the puzzle for me. I'm trying to get it to replace an old Android tablet for in-vehicle navigation, using a Bluetooth GPS. Obviously this requires Mock Locations.
    On the command "adb shell settings put secure mock_location 1", I get the error "error: device unauthorized. Please check the confirmation dialog on your device".
    I have developer options, and ADB debugging enabled. OS is 63.0.3239.85 (Official Build) beta (64-bit).
    Any suggestions welcome, and thanks for sharing!

    ReplyDelete
    Replies
    1. So did the RSA authentication dialog pop up for you at all? If you do "adb devices", does it just say "unauthorized"?

      I have a CB running the same OS version and just attempted to connect to ARC++ via adb - the RSA popup didn't appear at all first of all.

      I'm on a secondary account so this might be different for you, but here's one way that I was able to get it connecting again.

      1. First, I exited root so as not to confuse matters. Then, as chronos@localhost, I checked if an .android dir was present in my home dir.

      cd ~
      ls -al

      2. The .android dir was not present in the home dir, so I (re)created it:

      mkdir ~.android

      3. Next I generated a new adb key

      adb keygen ~.android/adb_key

      4. Now, after killing the adb server and reconnecting, the Android RSA dialog correctly popped up in order for me to check the box to authorise the adb device connection.

      adb kill-server
      adb connect 100.115.92.2

      5. After ticking the box on the Android RSA popup, "adb devices" no longer shows "unauthorized", and I can proceed to change settings etc via adb.

      If this still doesn't help any, feel free to let me know. There are probably also other ways around it.

      Delete
  2. Sir, you have just given me a lovely Christmas present!

    The RSA authentication dialog popup didn't happen for me at all. Following your steps precisely worked, however - I'm now up to nominating a mock location provider. I'll be able to try this at home this afternoon (at work currently), and I'll let you know how I go with using BluetoothGPS as the mock location provider.

    Thank you very much for your original post, and very helpful reply.

    I'll reply back with my progress.

    Thanks again,
    Michael.

    ReplyDelete
    Replies
    1. Able to assign via adb "googoo.android.btgps" as the mock provider - now need to check that it works later today when I get home to the bluetooth GPS unit.

      Delete
    2. Confirming that this works perfectly with BluetoothGPS, using the instructions above. I was able to use Sygic (and Oruxmaps) offline, with the location provided via Bluetooth GPS (https://play.google.com/store/apps/details?id=googoo.android.btgps). I suspect that any of the other bluetooth GPS applications should work just as easily.

      Fantastic work - thanks again for sharing this information, and additional help in getting it working for me.

      Cheers!
      Michael.

      Delete
    3. I'm trying to get this working again after a few ChromeOS updates/powerwashes, and having some difficulty.
      Currently on Beta 68.0.3440.59 on the Chromebook Pro. Developer setting are enabled, and ADB debugging enabled through Android preference.

      I get as far as starting the ADB server, get the result "daemon not running. starting it now on port 5037. daemon started successfully".

      I the use "adb connect localhost:5037", and get a successful connection message "connected to localhost:5037".

      After that, however, no ADB commands work - all result in the error "error: device not found".

      When I run the "adb devices" command, I get an empty list - not even any unauthorised devices.

      I don't think I'm doing anything wrong, but suspect that something has changed in ChromeOS/ADB somewhere that I haven't been able to work out.

      Delete
    4. What happens if you do "adb connect 100.115.92.2"?

      Delete
    5. It works... of course.

      I had already tried that, but I may have stuffed up prior steps (ie. before I remembered to enable developer mode in my very fresh install).

      Working perfectly now.

      Thanks for your response - much appreciated!

      Cheers!
      Michael.

      Delete
  3. does anyone think they could make step by step guide on how to enable mock locations

    ReplyDelete
    Replies
    1. Well, that's kind of what this blog post is supposed to be. Perhaps it could be simplified further, if necessary. Feel free to post back if anything's particularly unclear, or if something doesn't seem to be working for you.

      I think the steps should work fairly universally, although I haven't tested it out on every device/CrOS version.

      There is one possible potential source of difficulty that comes to mind - which is if the Android "Allow USB Debugging?" RSA authentication pop up dialog doesn't appear immediately after entering the "adb connect" command for the first time (mentioned in comments #1 and #2 above).

      If the RSA pop-up doesn't appear when you try to connect via adb for the first time, it's probably worth restarting adb with "adb kill-server" then trying to connect again, or retrying after a reboot, before attempting anything else.

      Anyway, to recap, normally the steps are:

      1. Switch on adb debugging in Android's developer options.

      2. Become root in CrOS shell, then connect to Android with adb.

      sudo su -
      adb connect 100.115.92.2

      3. Enter the command to enable mock locations.

      adb shell settings put secure mock_location 1

      4. Set your mock location app as default.

      adb shell appops set YOURAPPID android:mock_location allow

      (change YOURAPPID to the App ID of the mock location app you want to use to spoof the location - e.g. in the example in the blog post I want to use the app "Fake GPS", so I use the app ID com.lexa.fakegps.

      Delete
  4. So as far as I get is to the Crosh command line page where it's just "crosh>". Tried commands but all I get is unknown command. Sorry for all the questions new to all this

    ReplyDelete
    Replies
    1. Oh, ok, well at the crosh command prompt you type

      shell

      to get into the proper command shell. You'll know when it's worked as the prompt will change from

      crosh>

      to

      chronos@localhost / $

      If the above doesn't happen when you try it, then the Chromebook's not in Developer mode, and so you won't be able to enable ADB debugging and connect to the Android container in the shell, etc etc.

      If your Chromebook isn't in Developer mode, you can put it in this mode with a few keypresses (as long as it's not enrolled in a school or organization).

      However, enabling Developer mode completely wipes all personal data from the device. Also, when the device is in Developer mode, an extra loading screen will show up every time you boot it, where you have to press Ctrl-D to continue.

      To put a Chromebook in Developer mode, normally you press both the ESC and Refresh (F3) keys, and then tap the Power button as well, which should bring you to Recovery mode, then you press Ctrl-D at the Recovery screen.

      I should say again, though, that this wipes the device completely, like a factory reset, and you have to set up the computer from fresh again (although most apps/extensions/settings etc. are automatically synced and restored.

      More info on this is available at various places online, e.g. at chromium.org:

      https://www.chromium.org/chromium-os/developer-information-for-chrome-os-devices/generic

      Delete
  5. If it's any use to you, here's my own list of steps, based on what I've learnt (mostly from here). I spent a good few hours trying to understand each of the steps, and learnt a lot along the way. I've just used this process this morning (revert to stable channel and powerwash from Canary, Samsung Chromebook Pro), so it works for me. Try and follow these steps, and see at which point it falls over for you.

    Step:. Source thread:
    https://nolirium.blogspot.com.au/2017/09/enabling-mock-locations-in-android-on.html?showComment=1513814676384#c1896119914600809341
    1. Enable ADB debugging, through Android preferences - same as an Android phone
    https://developer.android.com/topic/arc/
    2. Restart device
    Might not be necessary?
    3. Set password for SuperUser for Root access, Press ctl+alt+forward (by forward I mean the F2 equivalent, not the right arrow). It will pop up the proper developer console which allows you to logon as root with the password you set during setup. It also has instructions for setting the chronos password properly using chromeos-setdevpasswd.
    Source: https://github.com/dnschneid/crouton/issues/2050
    4. CTRL+ALT+T
    Enter crosh prompt.
    5. Input: shell
    Should then have "chronos@localhost / $" prompt
    6. Input: cd ~
    Exit root directory, should then have "chronos@localhost ~ $" prompt
    7. Input: sudo su ~
    Enables SuperUser for Root
    8. Input: ls -al
    Check to see if .android directory exists in the home directory - doesn't exist by default
    9. Input: mkdir ~.android
    Creates .android directory
    10. Input: adb keygen ~.android/adb_key
    Creates new adb key
    11. Input: adb kill-server
    Kill adb server
    12. Input: adb start-server
    Restarts the server - note number when "...starting it now on port xxxx"
    13. Input: adb connect 100.115.92.2
    Restart adb server - after this, a prompt should appear to allow the adb connection. Tick the "always allow" tick box before accepting. After this, any following adb devices/commands should now be authorised, allowing part 2 below
    14. Input: adb shell settings put secure mock_location 1
    Switches on Mock Locations via adb
    . adb shell appops set com.lexa.fakegps android:mock_location allow
    If a suitable fake location Android app (e.g. https://play.google.com/store/apps/details?id=com.lexa.fakegps) is installed on the device, this can be set as the default mock locations app. To set a different app as the mock locations app, change the package name/App ID (com.lexa.fakegps in the example above) to the appropriate package name (as shown on the app's Play Store URL). After setting Fake GPS as the mock locations app, I was able to select my mock location in Fake GPS and e.g. Maps in Opera would successfully show the mock location, rather than my current location. I haven't really tested in many other apps at this time.
    15. Input: adb shell appops set googoo.android.btgps android:mock_location allow
    For my example, BluetoothGPS website: https://play.google.com/store/apps/details?id=googoo.android.btgps

    ReplyDelete
    Replies
    1. where can i enable adb debugging?? i cant find it on the developer page

      Delete
  6. "adb shell appops set com.lexa.fakegps android:mock_location allow
    error: device unauthorized. Please check the confirmation dialog on your device."
    I didnt get the pop-up that tell to authorize the device !
    Is there anyway to authorize the device ?

    ReplyDelete
    Replies
    1. and no popup to authorize device. I have turned off adb turned it on again. killed adb started adb. still no prompt.

      Delete
    2. Yes, I know that getting adb connected and working can be quite finicky on certain devices/CrOS versions.

      Regarding the popup, if you haven't already, I might suggest trying the procedure mentioned in my comment above of 13 December 2017 at 07:49 (involving recreating the ~.android dir, and generating adbkeys therein).

      The latter method is also outlined, perhaps a little more clearly, in the comment above from
      Michael Forsythe of 16 August 2018 at 16:03.

      If still no joy after doing all of that, then another option might be to add a cli chroot, and try adbing in from there (I mention this as on some CrOS builds this is the only way I have been able to reliably connect to certain external Android devices).

      However, in terms of connecting to the container from within Cros directly, this definitely should be possible from the regular CrOS cli, but I am aware of the possibility that something could have changed with a recent OS update, especially since as the OS matures, features/bugs are periodically being tweaked/added/removed.

      When I get a chance, I'll have a look into this again, to see whether or not I can recreate the issue on a fresh build, and if so, what workaround/fix might be appropriate now.

      Delete
  7. it says no devices/emulators im trying to run fakegps on a chromebook

    ReplyDelete
  8. Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. Download mini militia mod apk

    ReplyDelete
  9. went through this before and worked flawlessly until cb got updated accidentally. bluetooth gps app disappeared and wont let me re-install. decided to try again instead using and app calledgps tether, but am currently stuck on step 9. keeps telling me cannot create directory, or command not found. any ideas or tips would be greatly appreciated.

    ReplyDelete
  10. Howdy just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Ie. I'm not sure if this is a format issue or something to do with browser compatibility but I figured I'd post to let you know. The style and design look great though! Hope you get the issue solved soon. Thanks

    ReplyDelete
  11. Have you ever thought about writing an e-book or guest authoring on other blogs? I have a blog centered on the same topics you discuss and would really like to have you share some stories/information. I know my viewers would enjoy your work. If you are even remotely interested, feel free to shoot me an e-mail.

    ReplyDelete
  12. whenever i do adb shell settings put secure mock_location 1
    it says that error: more than one device or emulator
    I need help

    ReplyDelete
    Replies
    1. Had the exact same issue - this worked for me:
      adb -s 100.115.92.2:5555 adb shell settings put secure mock_location 1

      adb -s 100.115.92.2:5555 adb shell appops set YOURAPPID android:mock_location allow

      Delete