I have implemented http server on raspberry device which has it own network without access to internet. Now, I am developing Xamarin.Android application which needs to download files stored on server. When I connect to raspberry network there is no internet connection. When I try to download any file from server via http using Android DownloadManager instance nothing happens. There is no notification and no error at all. But when I change network in my phone to my local wifi I have internet access and then notification about downloading failure shows up.
It seems that DownloadManager do not work offline, but customer don't want internet connection on raspberry device.
I created my download manager instance based on:
Android: How to use download manager class?
Here is the code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
LoadApplication(new App());
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted || ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
{
// this will request for permission when user has not granted permission for the app
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 1);
}
else
{
//Download Script
DownloadManager downloadManager = (DownloadManager)GetSystemService(Context.DownloadService);
Android.Net.Uri uri = Android.Net.Uri.Parse("http://192.168.43.147:8080/data/22f13b77-d3cc-4788-96b3-cf454d4c07b6_IMG_20191008_122456.jpg");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);
request.SetVisibleInDownloadsUi(true); request.SetNotificationVisibility(Android.App.DownloadVisibility.Visible);
request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, uri.Path.Replace('\\', '/').Split('/').Last());
downloadManager.Enqueue(request);
}
}
}
Server works and correctly serve files because I can download it via web browser on my laptop.
How can I achieve this without download manager, or how to force download manager to work offline?
request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);
Why Mobile also? That's internet. – Selig/data/apname/Downloads
you mean/data/data/apname/Downloads
? – Selighttp://192.168.43.147
Are you trying to download from an Android device acting as a hotspot? Id the Android device where your app is running connected to the same hotspot? – SeligBut when I change network in my phone to my local wifi I have internet access
You can let your local wifi on but detach the cable from the wall to the router. So no internet but wifi yes. Does it work? – Selig