Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Open settings - android 7

Hi,

I want to able to open for example bluetooth settings page from my app. I succeeded to do that using this lines of code:

  Intent intentOpenBluetoothSettings = new Intent();
  intentOpenBluetoothSettings.SetAction(Android.Provider.Settings.ActionBluetoothSettings);
  Android.App.Application.Context.StartActivity(intentOpenBluetoothSettings);

Everything is working perfectly if using android 9 but on lower versions of android (for example android 7) is not working. Does someone have any ideas why this happens ?

Thanks

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited August 2019

    I've reproduce your code on Android 8.1 and It works well.

    BluetoothSettings was introduced in initial android. The code works for all verisons of android sdk. What's the android 7 device your used for testing? Try using another test device to verify if it is caused by the device.

    https://developer.android.com/reference/android/provider/Settings#ACTION_BLUETOOTH_SETTINGS

  • calincozmincalincozmin Member ✭✭

    @Jarvan I have tested as well on a device with Android 8.1 and it works well for me as well. Still, I tested on a Sony mobile device which has Android 7 and it's not working.

    Thanks

  • JarvanJarvan Member, Xamarin Team Xamurai

    Have you tested the code on other Sony mobile devices or other Android 7.0 devices?

  • JohnHardmanJohnHardman GBUniversity admin

    @calincozmin

    Rather than call simply calling StartActivity, first check whether the intent is available on the device. If it is, then start it, otherwise fall back to a more generic settings enum value.

    The following is how I do this for other intents (I don't use Bluetooth currently). As this makes use of some helper methods, treat this as pseudo-code.

    public Task StartActivityForIntentAsync(string action)
    {
        Intent intent = new Intent(action);
    
        // ActivityHelper.IsIntentAvailable is based on code from the book "C# 7.1 and .NET Core 2.0 - Modern Cross-Platform Development, Third Edition" by Mark J. Price
        if (ActivityHelper.IsIntentAvailable(intent))
        {
            // ActivityHelper.StartActivitySideBySide allows more than one app to be on screen at a time where supported
            ActivityHelper.StartActivitySideBySide(intent, null);
            return Task.CompletedTask;
        }
    
        if (action == Android.Provider.Settings.ActionLocationSourceSettings)
        {
            intent = new Intent(Android.Provider.Settings.ActionSecuritySettings);
            if (ActivityHelper.IsIntentAvailable(intent))
            {
                ActivityHelper.StartActivitySideBySide(intent, null);
                return Task.CompletedTask;
            }
        }
    
        if (action == Android.Provider.Settings.ActionSecuritySettings)
        {
            intent = new Intent(Android.Provider.Settings.ActionPrivacySettings);
            if (ActivityHelper.IsIntentAvailable(intent))
            {
                ActivityHelper.StartActivitySideBySide(intent, null);
                return Task.CompletedTask;
            }
        }
    
        if (action != Android.Provider.Settings.ActionSettings)
        {
            intent = new Intent(Android.Provider.Settings.ActionSettings);
            if (ActivityHelper.IsIntentAvailable(intent))
            {
                ActivityHelper.StartActivitySideBySide(intent, null);
                return Task.CompletedTask;
            }
        }
    
        return InsightsWrapper.SilentlyReportExceptionAsync(Could not resolve activity"));
    }
    
Sign In or Register to comment.