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

Request permissions before enabling bluetooth #175

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.App;
using AndroidX.Core.App;
using Google.Android.Material.Card;

namespace NearShare.Droid;
Expand All @@ -24,7 +25,7 @@ protected override void OnCreate(Bundle? savedInstanceState)
Button receiveButton = FindViewById<Button>(Resource.Id.receiveButton)!;
receiveButton.Click += ReceiveButton_Click;

FindViewById<MaterialCardView>(Resource.Id.enableBluetoothButton)!.Click += (_, _) => StartActivity(new Intent(BluetoothAdapter.ActionRequestEnable));
FindViewById<MaterialCardView>(Resource.Id.enableBluetoothButton)!.Click += (_, _) => EnableBluetooth();
FindViewById<MaterialCardView>(Resource.Id.setupWindowButton)!.Click += (_, _) => UIHelper.OpenSetup(this);
FindViewById<MaterialCardView>(Resource.Id.openFAQButton)!.Click += (_, _) => UIHelper.OpenFAQ(this);
}
Expand Down Expand Up @@ -89,4 +90,21 @@ public override bool OnCreateOptionsMenu(IMenu? menu)

public override bool OnOptionsItemSelected(IMenuItem item)
=> UIHelper.OnOptionsItemSelected(this, item);

private void EnableBluetooth()
{
if (OperatingSystem.IsAndroidVersionAtLeast(31))
ActivityCompat.RequestPermissions(this, [ManifestPermission.BluetoothConnect], EnableBluetoothCode);
else
StartActivity(new Intent(BluetoothAdapter.ActionRequestEnable));
}

const int EnableBluetoothCode = 0x2;
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
if (requestCode != EnableBluetoothCode || grantResults.Any(x => x != Android.Content.PM.Permission.Granted))
return;

StartActivity(new Intent(BluetoothAdapter.ActionRequestEnable));
}
}
Loading