Disconnect network in Android emulator while keeping ADB alive - android

I have made a mistake somewhere in my code that my Android app crashes when I switch off the network in the emulator while the app's HTTPClient is making some request.
But I cannot debug it, because when I switched off the network, I also lost the ADB connection. Thus I cannot see what exception is causing the crash.
I have tried switching off the network by:
F8
Set 'Data' to 'Unregistered' or 'Denied' in DDMS Emulator Control
I can put my Mac offline and ADB will stay up, but unfortunately I cannot reproduce the crash that way.
Is there a way to turn off network in the emulator, while keeping ADB alive? I basically need access to the LogCat entries.
Thanks!

You can run logcat from the device instead of from ADB and redirect it to a file for offline reading.
adb shell logcat [-v threadtime] > /sdcard/mylogs/logcat.log
Instead of
adb logcat [-v threadtime]
Then you can get the file when you reconnect to the device.

On linux this is pretty simple with iptables firewall. I suppose you trying to debug api calls so just block api host:
iptables -I OUTPUT -d api.example.com -j DROP
Sure you can do the same thing with Mac or Windows firewall.

You could always put the device into airplane mode, that will still allow you to connect over adb but not give the emulator a connection to the internet. You can also turn off data through eclipse via the emulator control section in ddms by putting in unregistered/denied to the data drop down.

I know this is old question but someone might use find for this.
My setup
Eclipse Kepler with emulated android 4.4
If you want to keep your adb alive and network turned off, you can try this.
Start emulated device
Activate flight/airplane mode
Restart Eclipse
It should work! Check DDMS perspective for attached devices or use adb devices command

Related

error: more than one device/emulator - cant remove unity emulator

Im trying to get debug info through logcat when developing for android. Logcat does not show any info but only an error
error: more than one device/emulator
When i run command
adb devices
i get
List of devices attached
R3CT20DP74P device
emulator-5562 offline
I guess the emulator is preventing my phone from connecting with adb? Im also guessing thats what causing catlog from logging from my phone. Do i remove the emulator -5562 somehow? Ive tried adb kill-server but the emulator reapears.
Ive also tried
adb -s emulator-5562 emu kill
returns:
error: could not connect to TCP port 5562: cannot connect to 127.0.0.1:5562: No connection could be made because the target machine actively refused it. (10061)
Ive tried:
adb.exe -s R3CT20DP74P forward tcp:7201 tcp:7201
which returns
7201
but does nothing else. Emulator is still there.
First of all, there are 2 separate errors that you're getting, and they actually have nothing to do with each other. You'll need to deal with both of them:
error: more than one device/emulator:
you need to run adb kill-server to clear the connected devices, and then restart ADB. It'll auto-connect any devices that are physically connected via USB, but it won't connect any emulators or devices that are connected via wireless debugging
error: could not connect to TCP port: it looks like you're trying to connect via wireless debugging, and it's a bit finicky. It times out after a while, and the device starts rejecting your connection requests. To fix it, you have to physically connect your device to the PC using a USB cable, and as soon as you do that, it should give you a confirmation prompt on the device to allow the USB debugging connection. After you approve that request, you'll need to turn off wireless debugging, and then turn it right back on. It'll generate a new TCP port for you to use, and you will now be able to connect to wireless debugging using that new port

Android Studio - how to use my wifi in order to connect to internet

I searched on how to get an Internet connection from my emulator in Android Studio and it was suggested I start up the emulator from the command line as follows: emulator -avd Pixel_3a_XL_API_29 -dns-server 8.8.8.8. This indeed has provided an Internet connection when I do from the command line, after adb shell, then run-as com.example.myapplication, then ping abcnews.go.com. this returns packets from the server "...r.cloudfront.net". Looking up this server, it appears to be for Amazon AWS, and is "free", up to some data limit I haven't looked up yet. This is OK short-term, but I would prefer to use my home wifi rather than go thru a different server. What's the best way to have my Android Studio emulator be able to connect to the Internet via my home wifi? (do I need a plugin?) TIA, Steve
First of all, emulator below API 25 doesn't support wifi.
Second, you can't connect the emulator directly to your WiFi.
The WiFi in the emulator provides a virtual hotspot to which the emulator automatically connects.
So, best practice would be that you connect your PC to your home WiFi and then run the emulator on your PC.

Android ADB can't ALWAYS see the phone device

I have read all the relative threads about ADB not recognizing devices. My case though is different: ADB sometimes "sees" my phone (Samsung Galaxy) and other times NOT. No matter how many times I kill and restart the ADB service or I disconnect and reconnect the device from/to the PC. Quite an unstable situation here. You know, it's one of these things that drives you crazy. That's why I resorted to your help.
Has anyone had this exact problem and resolved it once for all?
More info:
The PC has never a problem recognizing the device.
I try with both the standard SDK adb.exe and the Universal ADB driver adb.exe.
Either of the above have no problem recognizing the emulator.
Couple things.
1) it's a dumb suggestion but sometimes its as simple as this. Did you try more than one cable?
2) If yes, then try this. try as many times as you have to get it connected since u said "ALWAYS" so it does work sometimes. When it does work, open terminal or command line and run the following commands.
(make sure device is connected when you do this AND you are on the same wifi network)
adb tcpip 5555
then disconnect your phone and run the following command:
adb connect YOUR_LOCAL_IP
do this many times, this is wireless adb connection and if this works, its something with ADB or your computer and not the phone.
Let me know if it works. I might have more ideas depending on what result you get
============ EDIT ============
I believe it's offline because it says 8080. I just did the same with my phone and I get the following:
Try this: disconnect phone physically (if connected) and all other devices and make sure by doing: adb disconnect.
then, do adb kill-server
then, on your phone, go to Developer Options and uncheck USB debugging.
then, right underneath that options, there is Revoke USB debugging authorizations options. select and revoke all devices
then, check USB debugging
then, back on your computer, run adb start-server
then, try reconnecting with the steps I said before. make sure port is 5555
when you run the adb connect command, make sure to add the port at the end.
example: adb connect 10.0.0.15:5555
Hope this works.

Virtual device running in Genymotion periodically goes offline in ADB

I have an Android device (with Lollipop image, if important) running in Genymotion 2.4 and connect to it via ADB (version 1.0.32) from another developer PC with Eclipse in local network without connectivity problems. All is fine except for periodical drops of ADB connection (other protocols keep working fine), that is ADB loses the device, and when I try to reconnect it says that device is offline. From my experience, this offline status does basically mean that the receiving end (the device) still thinks it's connected and declines new connection. The only method I know to remedy this problem is to restart the device and then ADB connects to it as expected.
If someone is interested, ADB is not running on the remote host machine with Genymotion - otherwise the remove connections from other PCs would be impossible.
The question: how can I refresh "softly" the virtual device to accept new ADB connection without restarting entire device?
P.S. If I leave the "offline" device for its own for some time (quite a long time, for example, for an hour) it seems refreshes its state without external interaction, so ADB is again able to properly connect to and device is reported as "online". But, it makes no sense to wait so long for this to happen. This is just a new piece of information for consideration.
This may not work for you, but has worked for me in the situation where my device simply goes ADB offline. One of the comments on your question mentions that their emulator typically goes offline when their device sleeps. Mine does the same and also goes offline after long periods of inactivity, despite not actually sleeping. Anyway, my fix is plain and simple:
In terminal, navigate to where you have the SDK installed. Then, navigate to the directory platform-tools within your SDK, and issue the following command:
adb kill-server
After that finishes, type this one:
adb devices
That almost always fixes my problem, and I hope it fixes yours as well. Let me know how that works for you.
If you use MacOS Sierra - there is a known bug in it.
28.11.2016 There is no workaround yet except restarting adb which is suggested in #lustig answer.
Google gonna fix it in Android Studio 2.3. Issues to track 82425, 216214
There is no info about will Apple fix it on their side or not.

Launch Android Emulator with No Data via Command Line

I'm trying to figure out how to launch an emulator with no data connection or airplane mode, either one will work. Im testing my application against data loss, however I need this to be atomized. Therefore I'm trying either to launch the emulator with no data via command line, or write bash script to launch emulator, turn data off, then run Unit Test.
RESOLVED - Per Recommendations
#! /usr/bin/expect
set timeout 10
spawn telnet localhost 5554
expect "OK"
send "gsm data off\n"
send "quit\n"
The quick and dirty way to do it could be to make an app that starts at boot and turns data off, then run the emulator and install the app. This does have potential for inconsistency however.
The other thing you can do to leave just the emulator without a network is to telnet into the emulator and turn data off. To automate it, I made a simple (and crude) example using a batch file and vbscript to feed telnet the commands since my work machine is running Windows.
The batch file:
#echo off
emulator -avd YOURAVDNAMEHERE
timeout /t 10
telnet_turn_data_off.vbs
telnet_turn_data_off.vbs:
set oShell=CreateObject("WScript.Shell")
oShell.Run "telnet localhost 5554", 9
WScript.sleep 500
oShell.Sendkeys "gsm data off{ENTER}"
WScript.sleep 500
oShell.Sendkeys "gsm voice off{ENTER}"
WScript.sleep 500
oShell.Sendkeys "quit{ENTER}"
WScript.sleep 500
oShell.Sendkeys "quit{ENTER}"
WScript.sleep 500
If running on Linux or Mac OS, expect should be able to script telnet (as seen by the edit to the question).
It is also worth noting that on older emulators (eg 2.2), turning data off might not do anything. My above example was tested with a 4.2 emulator and it appeared to cut off data properly.
I typically use physical hardware for testing instead of emulators, but if you can't find an option to enable airplane mode you could simply disconnect your computer from the internet before launching the emulator. No internet for your computer = no internet for your emulator.
Use This Following steps..
Go to Eclipse
Window->show view->other->android->Devices
now select your running emulator and Again
Go to,
Window->show view->other->android->Emulator Control
Now in that..set Data to Unregister from Home
From Device and Emulator
In Device or Emulator Go to,
Setting -> Wireless & networks -> Airplane Mode -> ok

Categories

Resources