Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing permissions on Android, code not working #857

Open
ygoe opened this issue Nov 30, 2024 · 1 comment
Open

Missing permissions on Android, code not working #857

ygoe opened this issue Nov 30, 2024 · 1 comment

Comments

@ygoe
Copy link

ygoe commented Nov 30, 2024

Edit: Sorry, I noticed that this repo is for all samples. I'm talking about this one: https://platform.uno/blog/building-a-bluetooth-explore-with-uno-platform/

So this thing works on Windows 11 (in some form or the other, I made a few modifications to get that 32feet thing working at all). But it fails on Android 12 on a Samsung S10e. I get an exception:

Java.Lang.SecurityException: 'Need android.permission.BLUETOOTH_SCAN permission for AttributionSource ...'

I checked that my manifest file was showing properly and the main activity code was actually executed. I saw that the code only requests Bluetooth permission so I duplicated it for the BluetoothScan permission, but that doesn't change anything. My app requires Bluetooth permission and requests it but still doesn't have it. Did you account for that in the demo somewhere?

The MainActivity.Android.cs file also has several calls underlined in green in Visual Studio that I don't understand. What is that version "M"? I have version 12 and it is executed (stepped through in the debugger).

grafik

The demo source code in this repo looks outdated anyway, it has a completely different project structure than what I get when I create a new project. But I can read everywhere that Uno is developing much quicker than its documentation could keep up with, causing a lot of issues for beginners who want to follow the tutorials and documentation. Maybe you should slow down a little and focus on documentation so that this stuff actually becomes usable by others. You want others to use it, right? At least your website makes that impression.

@ygoe
Copy link
Author

ygoe commented Nov 30, 2024

Looks like the code has a bug. I found this and translated it to C#. The difference is that the code wasn't requesting all permissions but only one.

This is what works for me:

public class MainActivity : Microsoft.UI.Xaml.ApplicationActivity
{
	protected override void OnCreate(Bundle bundle)
	{
		base.OnCreate(bundle);

		RequestBluetoothPermissions();
	}

	private void RequestBluetoothPermissions()
	{
		// TODO: Somehow set minimum Android version to 6.0 (or "23")
		if (CheckSelfPermission(Manifest.Permission.Bluetooth) != Permission.Granted)
		{
			RequestPermissions(
			[
				Manifest.Permission.Bluetooth,
				Manifest.Permission.AccessFineLocation,
				Manifest.Permission.BluetoothScan,
				Manifest.Permission.BluetoothConnect
			], 1);
		}
	}
}

This has triggered a permission confirmation that I had to accept. Then the app was able to receive BLE advertisements.

(Unfortunately that 32feet library has other issues on its own limiting its use severely. Can't there be something built in to .NET/Uno for such elementary communication like Bluetooth that exists on every phone?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant