Base solution for your next web application
Open Closed

Xamarin - USB connected Android cannot connect to internet #4520


User avatar
0
jcompagnon created

Hello,

I am trying to run a .Mobile solution following this part of the documentation <a class="postlink" href="https://aspnetzero.com/Documents/Development-Guide-Xamarin#solution-structure-layers">https://aspnetzero.com/Documents/Develo ... ure-layers</a>.

I have set up the environment by setting the proper local IP to LocalhostIp in DebugServerIpAdresses.cs, downloaded the Xamarin Android SDK Manager and API up to 26 installed.

When debugging with the emulator Android_Accelerated_x86_Nougat (Android 7.1 - API 25), it brings up the emulator and runs the application correctly. I am able to sign in.

I've also hooked up my Android device via USB (Samsung Galaxy S7 - listed as Android 7.0 - API 24) and enabled USB debugging via Developer options. However, when I run the application again, it opens the splash screen on my phone but never moves from there, eventually getting an error saying "cannot connect to server".

I've confirmed that the AndroidManifest has INTERNET enabled as a Required permission.

Is there a setting I missed or some other add-on I need to download to make it work? Is it something to do with the solution or my Samsung device's settings?

Documentation I've already looked at: <a class="postlink" href="https://developer.xamarin.com/guides/android/getting_started/installation/windows/">https://developer.xamarin.com/guides/an ... n/windows/</a> <a class="postlink" href="https://developer.xamarin.com/guides/android/getting_started/installation/set_up_device_for_development/">https://developer.xamarin.com/guides/an ... velopment/</a> <a class="postlink" href="https://developer.xamarin.com/guides/android/troubleshooting/questions/android-internet/">https://developer.xamarin.com/guides/an ... -internet/</a>

Thanks!

Jonathan


10 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Hi @JCompagnoni it seems everything is OK. I suggest you to make sure your phone and computer the host project is running on. Some times firewall or virus program block the connection.

    You can test whether your phone can connect to host by browsing host IP. Open a browser in your phone and type Local IP that you set in DebugServerIpAdresses.cs.

  • User Avatar
    0
    jcompagnon created

    Hi alirizaadiyahsi,

    While running application with my device connected via USB, I tried opening a browser and going to the LocalhostIp set (by itself and with the port of the host :22742) but it was not able to connect.

    I confirmed that there is nothing in place blocking the USB from extending connections to external devices and turned off all settings of the Windows Defender Firewall to make sure it wasn't that firewall blocking either.

  • User Avatar
    0
    alirizaadiyahsi created

    Are your phone and computer are the same network?

    For example your host IP 192.168.1.5:22742 Can you connect this IP from your computer browser? Can you connect this IP from your phone browser?

  • User Avatar
    0
    jcompagnon created

    No, my computer is hooked up via Ethernet and my phone is on LTE.

    At first, I was under the impression that hooking up the smartphone via USB would connect it to the same network as the computer, especially since the emulator is running on LTE but is able to connect just fine. Based on your question and my recent tests though, that doesn't seem to be the case.

    I've tried looking up the public IP of my network and putting that as the LocalhostIp, but that doesn't work either. Perhaps the easiest way around this would be to hook up my phone and computer to the same WiFi, but my office has a WiFi network that I cannot access. Otherwise I was looking into reverse usb tethering, but that seems to be quite a bit of work to accomplish.

    Is this scenario something that should work or am I hitting my head against a wall for nothing?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Are you running your *.Web.Host project via visual studio or as explained in here <a class="postlink" href="https://aspnetzero.com/Documents/Development-Guide-Xamarin#debugging">https://aspnetzero.com/Documents/Develo ... #debugging</a> ?

  • User Avatar
    0
    jcompagnon created

    @ismcagdas,

    I downloaded the Start-Host.zip file included in that documentation and changed the destination for the .Web.Host to where the files are stored locally on my computer C:\Users[username]\Documents\CoreTest\CoreTest\src\CoreTest.Web.Host.

    Therefore I run .bat file to start the host

  • User Avatar
    0
    jcompagnon created

    Stupid question, but I'm assuming in DebugServerIpAdresses.cs,

    public static string Current => LocalhostIp;
    
    //You can configure your computer's IP adress for external access (if Current = LocalhostIp)
    private const string LocalhostIp
    

    should be set to the IPv4 of the computer I'm working on, correct?

  • User Avatar
    0
    alper created
    Support Team

    hi,

    Connecting to a Web API from mobile can be tedious (apart from connecting to a Xamarin App)... I tried to explain how to make connection between the Xamarin App and the Web.Host Checkout the screenshot!

  • User Avatar
    0
    jcompagnon created

    Thanks @alper!

    I've been suspecting for a while that both the device and computer need to be on the same WiFi network, but it isn't explicitly stated anywhere in the documentation.

    Otherwise, I think I've been doing everything else properly.

  • User Avatar
    0
    alper created
    Support Team

    updated the docs! added the information: >Be aware that connecting over WiFi needs both the device and the computer to be on the same WiFi network! (To avoid confusions to you can completely turn off your mobile data connection)

    <a class="postlink" href="https://aspnetzero.com/Documents/Development-Guide-Xamarin#debugging-xamarin-app">https://aspnetzero.com/Documents/Develo ... amarin-app</a>

    for the newcomers to this topic; there's an official Xamarin setup guide for development. It clarifies things explicitly See <a class="postlink" href="https://developer.xamarin.com/guides/android/getting_started/installation/set_up_device_for_development/">https://developer.xamarin.com/guides/an ... velopment/</a>

    From the Xamarin official doc [attachment=0:bt4utdy1]Screenshot_1.jpg[/attachment:bt4utdy1]