When I try to ping a device from my MAUI App built for Android I get a SocketException(Permission Denied). When i build for Windows it works just fine.
I can not find any Permission other than the ones I already have that could be required. Does anyone know what could be causing this?
Tested with Android 11 and Android 12 on physical devices.
EDIT:
Network-connection works fine through HttpClient.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<application android:allowBackup="true" android:icon="#mipmap/appicon" android:roundIcon="#mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Code:
var sender = new System.Net.NetworkInformation.Ping();
var options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "testdata";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 100;
PingReply reply = sender.Send(address, timeout, buffer, options);
Exception:
System.Net.Sockets.SocketException 'Permission denied'
Related
I'm running into a very odd issue in my Xamarin Forms app. I am trying to take a picture in my app, then use OCR to read the text, but I'm struggling to get permissions to be granted for WRITE_EXTERNAL_STORAGE. I now have to declare WRITE_EXTERNAL_STORAGE twice in my AndroidManifest in order for the app to allow me to request storage permissions or to access storage, once as a self-closing tag and once with explicit tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
If either is removed (leaving just one version of WRITE_EXTERNAL_STORAGE), I get the following exception when trying to request permissions for external storage or when trying to capture a picture: "You need to declare using the permission: android.permission.WRITE_EXTERNAL_STORAGE in your AndroidManifest.xml"
This ONLY affects WRITE_EXTERNAL_STORAGE...all other declared permissions (using self-closing tags) in the manifest work appropriately. It's just the write storage permission that needs this "hack."
This issue occurs on emulated devices (debug mode) AND on physical devices (via Play Store alpha track) when only one instance of the permission is listed. When debugging with both lines in the manifest, the app is able to obtain permissions to storage successfully and I can take the picture as expected. The Play Store will not accept submissions with duplicate lines in the manifest, so I am unable to submit to the store using this "hack."
This is a full copy of my AndroidManifest (without PII), including the duplicate lines I have to include in order for storage permissions to be granted successfully when debugging:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="myapp" android:versionName="3.2.2" android:installLocation="auto" android:versionCode="59">
<application android:theme="#android:style/Theme.Material.Light" android:icon="#drawable/Icon120" android:label="MyApp">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/file_paths"></meta-data>
</provider>
</application>
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
</manifest>
I also have the following line in my AssemblyInfo.cs file:
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
When I try to request permissions via Xamarin.Essentials or to access the camera to tke a picture via Xamarin.Essentials or CrossMedia, the app blows up with the above exception, claiming I am missing the WRITE_EXTERNAL_STORAGE permission, despite it being in the manifest once (either self-closed or with explicit tag). Any of these lines executing will result in the exception and all lines work when both entries for WRITE_EXTERNAL_STORAGE are present:
var permissionStatus = await Xamarin.Essentials.Permissions.RequestAsync<Xamarin.Essentials.Permissions.StorageWrite>();
var photo = await Xamarin.Essentials.MediaPicker.CapturePhotoAsync(new Xamarin.Essentials.MediaPickerOptions { Title = DateTime.Now.ToString("G") + ".jpg" });
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions { Name = DateTime.Now.ToString("G") + ".jpg" });
I have tried updating all of my NuGet packages and am now on the latest versions of frameworks for the app (Xamarin Forms v5 and Xamarin.Essentials 1.6.1) but this issue still persists. I also tried completely deleting the manifest and restarting from scratch, but the same "hack" is still needed.
Has anyone run into this or have any idea of how this can be fixed?
Thank you in advance!
To anyone who has this issue, the solution was to remove all references to and uninstall the NuGet package for HockeySDK.Xamarin. As soon as I removed everything related to this, the issue resolved itself and I no longer had the problem with the manifest
I am getting this error as th manifest file contain 2 application element and I know that.
Every time I remove the other application element it gets back again. The element item is the network_security_config.xml file I have set the Build Action = AndroidResource Do not Copy. How can I make sure application containing network_security_config gets created in the existing application element.
<?xml version="1.0" encoding="UTF-8"?>
-<manifest android:installLocation="auto" package="Me.Scheduler" android:versionName="1.0" android:versionCode="1" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:targetSdkVersion="28" android:minSdkVersion="21"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
-<application android:name="android.app.Application" android:debuggable="true" android:icon="#mipmap/icon" android:allowBackup="true" android:label="Scheduler.Android">
-<activity android:name="crc64c2f66de974599db5.MainActivity" android:icon="#mipmap/icon" android:label="Scheduler" android:theme="#style/MainTheme" android:configChanges="orientation|screenSize">
-<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name="crc64a98abb514ffad9f1.KeepAliveService"/>
<receiver android:name="crc64a0e0a82d0db9a07d.BatteryBroadcastReceiver" android:label="Essentials Battery Broadcast Receiver" android:exported="false" android:enabled="true"/>
<receiver android:name="crc64a0e0a82d0db9a07d.EnergySaverBroadcastReceiver" android:label="Essentials Energy Saver Broadcast Receiver" android:exported="false" android:enabled="true"/>
<receiver android:name="crc64a0e0a82d0db9a07d.ConnectivityBroadcastReceiver" android:label="Essentials Connectivity Broadcast Receiver" android:exported="false" android:enabled="true"/>
-<provider android:name="xamarin.essentials.fileProvider" android:exported="false" android:grantUriPermissions="true" android:authorities="Me.Scheduler.fileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/xamarin_essentials_fileprovider_file_paths"/>
</provider>
<receiver android:name="crc643f46942d9dd1fff9.PowerSaveModeBroadcastReceiver" android:exported="false" android:enabled="true"/>
<provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:authorities="Me.Scheduler.mono.MonoRuntimeProvider.__mono_init__" android:initOrder="1999999999"/>
<!--suppress ExportedReceiver-->
-<receiver android:name="mono.android.Seppuku">
-<intent-filter>
<action android:name="mono.android.intent.action.SEPPUKU"/>
<category android:name="mono.android.intent.category.SEPPUKU.Me.Scheduler"/>
</intent-filter>
</receiver>
</application>
<application android:networkSecurityConfig="#xml/network_security_config"/>
</manifest>
I would assume you are actually changing the Android Manifest in bin folder. I guess you access it by double tapping on error you getting? I was making the same mistake. That Manifest is auto generated from Manifest in Properties folder.
Make that change in an actual android manifest in Properties folder, and see if problem persists.
I'm getting the UnauthorizedAccessException when starting up my emulator with my application, it tells me that "Access to /storage/emulated/0/Download/MenuPlayer is denied".
This only occurs when I try to create a file or open up a folder to download files into it.
My AndroidManifest.xml code is:
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
My Code that is trying to access this is:
try {
/// Get the path to the downloads folder
var donwloadDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
path = Path.Combine(donwloadDir.AbsolutePath, dirName);
dirInfo = new DirectoryInfo(path);
///Create a path to the config file folder.
configFilePath = Android.OS.Environment.GetExternalStoragePublicDirectory("").AbsolutePath + "/";
} catch(Exception e) {
Android.Util.Log.Debug(TAG, "STUFF HERE..." + configFilePath);
Android.Util.Log.Debug(TAG, e.Message);
System.IO.File.WriteAllText(#"C:\logs.txt", e.Message);
}
System.IO.File.WriteAllText(#"C:\logs.txt", Android.OS.Environment.DirectoryDownloads);
Android.Util.Log.Debug(TAG, configFilePath);
Android.Util.Log.Debug(TAG, "END | PlayerReader");
the error is:
Unhandled Exception:
System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Download/MenuPlayer" is denied. occurred
Even if you have enabled READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission in manifest, you might need to do few things explicitly in code. Are you doing that already?
See post below for details
Xamarin-System.UnauthorizedAccessException: Access to the path is denied
Hope this helps
I am currently developing an app to use a barcode scanner, one of the requirements is that app autostart with the device.
so, google on that I found some interest thinks, but when I implemented it fails epicly, the app does not starts.
this is mainfest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AppConsulta.Droid" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="AppConsulta.Android" android:icon="#drawable/Logo_Jumbo_Cencosud">
<activity android:name="MainActivity" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
and this is my receiver
namespace AppConsulta.Droid
{
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent serviceStart = new Intent(context, typeof(MainActivity));
serviceStart.AddFlags(ActivityFlags.NewTask);
context.StartActivity(serviceStart);
}
}
}
using adb.exe I manage to extract the log of the device
and it shows this
10-22 16:33:00.412 2007 2007 E AndroidRuntime: FATAL EXCEPTION: main
10-22 16:33:00.412 2007 2007 E AndroidRuntime: Process: AppConsulta.Droid, PID: 2007
10-22 16:33:00.412 2007 2007 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate receiver AppConsulta.Droid.BootReceiver: java.lang.ClassNotFoundException: Didn't find class "AppConsulta.Droid.BootReceiver" on path: DexPathList[[zip file "/data/app/AppConsulta.Droid-1/base.apk"],nativeLibraryDirectories=[/data/app/AppConsulta.Droid-1/lib/arm, /data/app/AppConsulta.Droid-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
I'm having a bit of an issue with the Xamarin.Forms.Maps and the exceptions are not propagated up into the debugger in a meaningful way to help me diagnose the issue.
I can get it to work, everytime I reinstall Visual Studio. It will work a couple times then eventually it will crash 100% of the time as long as the Map item is in the xaml. I am not remaking the project between running, just reinstalling Visual Studio. I'm running Visual Studio 17 Community.
The error just says:
'Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).'
I have a valid API Key, and even if I comment out ALL code relating to the Map it eventually happens other than the Xaml that will create it. If I reinstall visual studio I can manipulate the map the first few times I invoke the debugger before it fails.
I wish I could give more information, but I'm pretty new to Xamarin development so I'm a bit unsure why reinstalling Visual Studio fixes the issue, temporarily. Any insight would be much appreciated, as I've spent many hours trying to diagnose and reinstall, but have no idea what the issue could be given the error doesn't really say much of anything. I'd love to provide more information if someone has more ideas for me to check out.
In general, it also fails to deploy eventually also : / which is why I tried an install originally. I'm not sure if this is related.
MainPage.xaml.cs
using System;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Plugin.Geolocator;
using System.Threading.Tasks;
using System.Collections.Generic;
using Plugin.Geolocator.Abstractions;
namespace LawnDroneCompanion
{
public partial class MainPage : ContentPage
{
private List<Position> RecordedPositions = new List<Position>();
public MainPage()
{
InitializeComponent();
}
private void StopGeoLocatorListening()
{
BeginTrackingRoute.Text = "Begin Tracking";
BeginTrackingRoute.BackgroundColor = Color.Green;
CrossGeolocator.Current.StopListeningAsync();
}
private async void StartGeoLocatorListening(object obj, EventArgs e)
{
if (CrossGeolocator.Current.IsListening)
{
StopGeoLocatorListening();
return;
}
BeginTrackingRoute.Text = "Stop Tracking";
BeginTrackingRoute.BackgroundColor = Color.Red;
CrossGeolocator.Current.DesiredAccuracy = 10;
///This logic will run on the background automatically on iOS, however for Android and UWP you must put logic in background services. Else if your app is killed the location updates will be killed.
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 1, true, new Plugin.Geolocator.Abstractions.ListenerSettings
{
ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Other,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = true,
PauseLocationUpdatesAutomatically = false
});
Position StartLocation = await CrossGeolocator.Current.GetLastKnownLocationAsync();
//MyMap.MoveToRegion(Xamarin.Forms.Maps.MapSpan.FromCenterAndRadius(new Xamarin.Forms.Maps.Position(StartLocation.Latitude, StartLocation.Longitude), Xamarin.Forms.Maps.Distance.FromMiles(0.2)));
CrossGeolocator.Current.PositionChanged += GeoLocateHandler;
}
private void DrawNewLocation(Plugin.Geolocator.Abstractions.Position position)
{
if(RecordedPositions.Count > 1000)
{
return;
}
RecordedPositions.Add(position);
//MyMap.MoveToRegion(Xamarin.Forms.Maps.MapSpan.FromCenterAndRadius(new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude), MyMap.VisibleRegion.Radius));
}
private void GeoLocateHandler(object sender, PositionEventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
DrawNewLocation(e.Position);
});
}
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:LawnDroneCompanion"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
x:Class="LawnDroneCompanion.MainPage">
<ContentPage.Content>
<StackLayout>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<!-- If I comment this out it works, this comment isn't in my real code -->
<maps:Map x:Name="MyMap" IsShowingUser="True" MapType="Hybrid"/>
</StackLayout>
<Button x:Name="BeginTrackingRoute" HorizontalOptions="FillAndExpand" Text="Begin Tracking" Clicked="StartGeoLocatorListening" BackgroundColor="Green" TextColor="WhiteSmoke" BorderRadius="0"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.LawnDroneCompanion" android:installLocation="auto">
<uses-sdk android:minSdkVersion="25" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Allow the application to access Google web-based services. -->
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<application android:label="LawnDroneCompanion.Android">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="{IMAGINE-MY-KEY-HERE}" />
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
</application>
</manifest>