Bluetooth の権限

Bluetooth 機能をアプリで使用するには、 権限。また、アプリで Bluetooth クラシックまたは Bluetooth Low Energy(BLE)のサポートお使いの 従来の Bluetooth や BLE は不要ですが、引き続き利用できます。 実行時に可用性を確認できます。

権限を宣言する

アプリで宣言する権限のセットは、アプリのターゲットによって異なる SDK のバージョン。

Android 12 以降をターゲットとする

注: Android 8.0(API レベル 26)以降では、 コンパニオン モード デバイス マネージャー(CDM)は、より効率的に 使用する権限と、このセクションで説明する権限を比較します。「 CDM システムはアプリに代わってペア設定 UI を提供します。 位置情報の利用許可

ペア設定や接続をより詳細に制御するには、 付与する必要があります。

<ph type="x-smartling-placeholder">
</ph> Bluetooth 権限ダイアログ
ユーザーに次の権限を求めるシステム権限ダイアログ 付近の情報を検出、宣伝、接続するための権限をアプリに付与します できます。

Android 12(API レベル 31)以降をターゲットとするアプリの場合は、以下を宣言します。 権限を宣言する必要があります。

  1. アプリで Bluetooth が必要な場合 デバイスを BLE 周辺機器を使用するには、 BLUETOOTH_SCAN 付与します。
  2. アプリで現在のデバイスを他の Bluetooth から検出可能にしている場合 デバイス、 宣言する BLUETOOTH_ADVERTISE 付与します。
  3. アプリがすでにペア設定されている Bluetooth と通信する場合 BLUETOOTH_CONNECT 付与します。
  4. Bluetooth 関連の従来の権限宣言では、android:maxSdkVersion30 に設定します。このアプリの互換性に関する手順により、システムは アプリのインストール時に必要な Bluetooth 権限のみをアプリに付与する Android 12 以降を搭載したデバイス
  5. アプリが Bluetooth のスキャン結果を使用して物理的な位置情報を取得する場合は、 ACCESS_FINE_LOCATION 付与します。それ以外の場合は、アプリが派生していないことを強くアサートできます。 物理的な場所

BLUETOOTH_ADVERTISEBLUETOOTH_CONNECTBLUETOOTH_SCAN の各権限は実行時の権限です。そのため、ユーザー アカウントを アプリを実際に承認してから、 Bluetooth デバイスへの接続、他のデバイスからのデバイスの検出を可能にする、通信を可能にする すでにペア設定されている Bluetooth デバイスとペア設定されています。アプリが アプリがこれらの権限にアクセスするのを許可するようユーザーに求めます。 付近のデバイス(図 1 を参照)。

次のコード スニペットは、Bluetooth 関連の宣言方法を示しています。 Android 12 以降をターゲットとしているアプリに付与する必要があります。

<manifest>
    <!-- Request legacy Bluetooth permissions on older devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH"
                     android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
                     android:maxSdkVersion="30" />

    <!-- Needed only if your app looks for Bluetooth devices.
         If your app doesn't use Bluetooth scan results to derive physical
         location information, you can
         <a href="#assert-never-for-location">strongly assert that your app
         doesn't derive physical location</a>. -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

    <!-- Needed only if your app makes the device discoverable to Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

    <!-- Needed only if your app communicates with already-paired Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    <!-- Needed only if your app uses Bluetooth scan results to derive physical location. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    ...
</manifest>

アプリが物理的な位置情報を取得しないことを強く表明する

アプリが物理的な位置情報を取得するために Bluetooth スキャン結果を使用しない場合は、 アプリが Bluetooth 権限を使用しないことを強く主張できる 物理的な現在地を導き出しますそのための手順は次のとおりです。

  1. android:usesPermissionFlags 属性を BLUETOOTH_SCAN 権限宣言に追加し、この属性の値を neverForLocation に設定します。

  2. アプリで位置情報が必要ない場合は、アプリのマニフェストから ACCESS_FINE_LOCATION 権限を削除します。

次のコード スニペットは、アプリのマニフェスト ファイルを更新する方法を示しています。

<manifest>
    <!-- Include "neverForLocation" only if you can strongly assert that
         your app never derives physical location from Bluetooth scan results. -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"
                     android:usesPermissionFlags="neverForLocation" />

    <!-- Not needed if you can strongly assert that your app never derives
         physical location from Bluetooth scan results and doesn't need location
         access for any other purpose. -->
    <strike><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /></strike>
    ...
</manifest>

Android 11 以前をターゲットとする

Android 11(API レベル 30)以下をターゲットとするアプリの場合は、以下を宣言します。 権限を宣言する必要があります。

  • BLUETOOTH は必須です リクエストなど、Bluetooth のクラシック通信または BLE 通信を 接続の受け入れ データの転送などがあります
  • ACCESS_FINE_LOCATION を必要とするのは、Android 11 以前では Bluetooth スキャンが 場所に関する情報を収集するために使用できます。 できます。
で確認できます。

位置情報の利用許可は実行時の権限であるため、 実行時にこれらの権限をリクエストする必要があります。 マニフェストで宣言することもできます。

ローカルの Bluetooth デバイスを検出する

アプリでデバイスの検出や Bluetooth の操作を行う場合 使用する場合は、 BLUETOOTH_ADMIN 付与します。ほとんどのアプリは、アプリを検出、検出、再生するためだけに、 接続します。この機能により付与される他の能力は使用しない アプリが「電源マネージャー」権限の場合を除き、Bluetooth 設定を変更する 作成できます。アプリ マニフェスト ファイルで権限を宣言します。次に例を示します。

<manifest>
...
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
...
</manifest>

アプリがサービスをサポートし、Android 10(API レベル 29)または Android 11 では、 ACCESS_BACKGROUND_LOCATION Bluetooth デバイスの検出を許可します。詳細については、このモジュールの 要件については、アクセス ロケーション( バックグラウンドをご覧ください。

次のコード スニペットは、ACCESS_BACKGROUND_LOCATION を宣言する方法を示しています。 権限:

<manifest>
...
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
...
</manifest>

<uses-permission> をご覧ください。 をご覧ください。

Bluetooth 機能の使用を指定する

Bluetooth がアプリの重要な部分である場合、マニフェストにフラグを追加できます この要件を示すファイルです。「 <uses-feature> 要素を使用すると、 アプリで使用するハードウェアのタイプと、そのタイプと 必要ありません。

この例では、Bluetooth クラシックが 。

<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>

アプリが Bluetooth Low Energy を利用している場合は、以下を使用できます。

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

アプリにこの機能が必須だと言えば、Google Play ストアは その機能が搭載されていないデバイスで、ユーザーに対してアプリを非表示にする。このため、 必須の属性を true に設定するのは、必須でないとアプリが動作しない場合のみです。 おすすめします。

実行時に機能の可用性を確認する

従来の Bluetooth や Bluetooth 対応ではないデバイスでアプリを利用できるようにするには、 場合は、その場合でも、アプリの <uses-feature> 要素を required="false" を設定します。そうすれば実行時に 利用可能な機能の可用性を PackageManager.hasSystemFeature():

Kotlin

// Check to see if the Bluetooth classic feature is available.
val bluetoothAvailable = packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)

// Check to see if the BLE feature is available.
val bluetoothLEAvailable = packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)

Java

// Use this check to determine whether Bluetooth classic is supported on the device.
// Then you can selectively disable BLE-related features.
boolean bluetoothAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
boolean bluetoothLEAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);