0

現在アンドロイドアプリにアプリから必要な権限を有効化するロジックを組み込んでいます
Xamarin.FormsにNuGetからXamarin.Android.Support.Compatを組み込んでいます

https://docs.microsoft.com/ja-jp/xamarin/android/app-fundamentals/permissions?tabs=windows
こちらのページを参考に、ページの下にあるサンプルアプリをダウンロードして
なんとか権限付与が動作するところまでこぎ着けたのですが
各アプリ毎の「付近のデバイス」などをONにしてくださいというメッセージを出すことが出来たのですが
BluetoothをOFFの場合にBluetoothをONにするようにと言うメッセージが出せません

付近のデバイスの権限が無いときは、狙ったようにアプリの権限付与の画面に行くのですが
Bluetoothのようなハードの使用権限は何か違う方法が必要なのでしょうか

マニフェストファイルに記述した権限は

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.testpermission" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="31" />
    <application android:label="TestPermission.Android" android:theme="@style/MainTheme"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="com.google.android.things.permission.MANAGE_BLUETOOTH" />
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />   
</manifest>

ソリューションのうちのAndroidのプロジェクトの
MainActivity.csのOnCreate関数の中身

 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);


            LoadApplication(new App());

            
            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.Bluetooth) != Permission.Granted || ActivityCompat.CheckSelfPermission(this,Manifest.Permission.BluetoothAdmin)!= Permission.Granted)
            {
                //★BluetoothをOFFにしても、ここに入ってこない
                ActivityCompat.RequestPermissions(this, new System.String[] { Manifest.Permission.Bluetooth ,Manifest.Permission.BluetoothAdmin}, 0);
            }


            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.BluetoothScan) != Permission.Granted)
            {
                //★付近のデバイスの権限がないとここでONにするようなメッセージが出る
                ActivityCompat.RequestPermissions(this, new System.String[] { Manifest.Permission.BluetoothScan, Manifest.Permission.BluetoothConnect, Manifest.Permission.BluetoothAdvertise }, 0);
            }
        }

1 件の回答 1

0

Android 12以降、Bluetooth権限の仕様変更がありました。
https://developer.android.com/about/versions/12/features/bluetooth-permissions?hl=ja
上記AndroidManifestを見てみると、android:targetSdkVersion=31としているため、
Android 12も想定していると判断して回答します。

Bluetoothの権限は、
OS自体が持つ、Bluetoothの許可/不許可と
アプリ毎に持つ、Bluetoothの許可/不許可があります。

上記のOnCreateの中身を見ると、
ActivityCompat.CheckSelfPermission
メソッドを使っていますね。これは後者のアプリ毎に持つBluetoothの権限になり、
その名称は、Android上では「付近のデバイス」と表記されます。
なお、Android 11以前は、「付近のデバイス」権限の必要なく、
Bluetoothの使用が可能でした。

ご希望の、OS自体が持つBluetoothの権限は、
以下の方法で判定します。参考にしてみてください。

https://developer.android.com/guide/topics/connectivity/bluetooth?hl=ja#SettingUp
https://learn.microsoft.com/ja-jp/dotnet/api/android.app.activity.startactivityforresult?view=xamarin-android-sdk-12&viewFallbackFrom=xamarin-android-sdk-9

1
  • ありがとうございます。記載されているサイトに従ってマニフェストファイルをAndroid11以前とでわけて12以降は付近のデバイスで動作しました 2022年11月20日 7:18

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。