Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to connect with xamarin form between a webapi en an android device?

When I start in visual studio 2022 an webapi and a xamerinform(.NET MAUI) I cannot create a connection between de fysical andriod device with de .NET MAUI app and the webapi on my local machine.
Do I need to open some ports(redirect?)
I can’t get further with testing my application now on Android.

When working on .NET MAUI on my windows machine, it’s ok, but debug on a physical android device, I expected that there would be some kind of tunnel so testing would be easy. Can’t find a way out.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This scenario is always a bit complicated for newcomers. Here is the gist. Where I say .NET MAUI, you can also replace it with Xamarin.Forms, or even any client technology basically.

The Problem

If you run your .NET MAUI Windows app on the same machine as your backend, you can simply connect to localhost and that will work just fine. Now, when you run your app on Android (or iOS) if you try to connect to localhost, then it tries to find your backend on the Android or iOS device. Doesn’t matter if that is an emulator or a physical device. Even though an emulator runs on the same machine, it acts like a separate device.

Hard Solution

So the first thing you want to do is figure out the IP address that you backend server is on and connect to that. Then depending on your OS and a lot of other things, you might need to open a port etc.

But then, there is the issue of a secure connection. Luckily, but a bit annoying, Android and iOS now force us to use secure connections. And typically your backend development service doesn’t have it’s own SSL cert. Now you suddenly need to add all kinds of disabling of security features in your app to still be able to connect from iOS/Android to your development backend service.

All of this is described in great detail here.

Easy Solution: Dev Tunnels

With Dev Tunnels this becomes much easier! Dev Tunnels allows you to run a little CLI tool on your machine that can open up your local development machine to the world (including your iOS/Android device) and provides a secure connection.

A reference for the Dev Tunnels CLI can be found here.

Dev Tunnels Quick Start

  1. Install the CLI tool

On Windows run: Invoke-WebRequest -Uri https://aka.ms/TunnelsCliDownload/win-x64 -OutFile devtunnel.exe .\devtunnel.exe -h

On macOS/Linux run: curl -sL https://aka.ms/DevTunnelCliInstall | bash

  1. Run devtunnel user login to login with your Azure account

  2. Host a tunnel with devtunnel host -p 8080 where 8080 is the port that your development service is running on. You will presented with a (secure) URL that you can use to connect to.

You might want to add the -a switch which allows for anonymous connections. Else for each connection you need to login with your Azure account. Regardless of using the -a switch, the first time you go to the URL there will be a webpage that you need to acknowledge, so make sure you go there first and click the button and then connect from your app.

Additionally, make sure that your development server is accepting connections from outside (usually make it listen to 0.0.0.0 for ASP.NET projects) and let it run on http (so not https) so that a certificate that might be in place is not conflicting with the one from Dev Tunnels and causing disconnects.

I have a video walking you through all of this here.

Alternatives

There has been an alternative for all this for a long time already called Ngrok. Functionality is basically the same but works a bit different.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading