Bluetoothの設定
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
アプリが Bluetooth または Bluetooth Low Energy で通信する前に、
Bluetooth がデバイスでサポートされていることを確認する必要があります。サポートされている場合は、
有効になっていることを確認します。このチェックが必要となるのは、
<uses-feature.../>
マニフェスト ファイル エントリの android:required
属性
false
に設定。
Bluetooth がサポートされていない場合は、Bluetooth を無効にしてください。
説明します。Bluetooth がサポートされていても無効になっている場合は、
ユーザーがアプリを終了せずに Bluetooth を有効にできる。
最初のステップは
Bluetooth 権限の追加
次の API を使用するためにマニフェスト ファイルに追加する必要があります。
権限を設定したら、Bluetooth のセットアップを 2 ステップで完了します。
BluetoothAdapter
を使用します。
BluetoothAdapter
を取得します。
BluetoothAdapter
は、あらゆる Bluetooth アクティビティに必要です。「
BluetoothAdapter
は、デバイス独自の Bluetooth アダプター(
Bluetooth 無線など)。BluetoothAdapter
を取得するには、まず以下が必要です
Context
。このコンテキストを使用して、
BluetoothManager
のインスタンス
使用することもできます。BluetoothManager#getAdapter
を呼び出しています
BluetoothAdapter
オブジェクトが得られます。getAdapter()
が null を返す場合、
デバイスは Bluetooth に対応していません。
例:
Kotlin
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.getAdapter()
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
Java
BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
Bluetooth を有効にします。
次に、Bluetooth が有効になっていることを確認する必要があります。発信
isEnabled()
~
Bluetooth が現在有効になっているかどうかを確認します。このメソッドが false を返した場合、
Bluetooth は無効になります。Bluetooth を有効にするようリクエストするには、
startActivityForResult()
渡すには
ACTION_REQUEST_ENABLE
アクションを指定します。この呼び出しは、Bluetooth を有効にするためのリクエストを
システム設定(アプリを停止せずに)管理できます。
次に例を示します。
Kotlin
if (bluetoothAdapter?.isEnabled == false) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
Java
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
以下に示すように、Bluetooth を有効にするユーザー権限をリクエストするダイアログが表示されます。
図 1.ユーザーが権限を付与すると、システムが Bluetooth の有効化を開始し、
プロセスが完了する(または失敗する)と、フォーカスがアプリに戻ります。
図 1. Bluetooth を有効にするダイアログ。
次に渡される REQUEST_ENABLE_BT
定数:
startActivityForResult()
ローカルで定義された整数です。0 以上の値にする必要があります。システム
この定数を
onActivityResult()
requestCode
パラメータとして渡すことをおすすめします。
Bluetooth の有効化に成功すると、アクティビティは
RESULT_OK
の結果コード:
onActivityResult()
コールバック。エラー(または
ユーザーが「拒否」と応答した場合)の場合、結果コードは次のようになります。
RESULT_CANCELED
。
必要に応じて、アプリはリッスンもできます。
ACTION_STATE_CHANGED
ブロードキャスト インテント(Bluetooth の状態が変化するたびに、システムがブロードキャスト)
できます。このブロードキャストには追加フィールドが含まれています
EXTRA_STATE
、
EXTRA_PREVIOUS_STATE
,
新しい Bluetooth の状態と古い Bluetooth の状態がそれぞれ格納されます。指定できる値:
これらの追加フィールドは
STATE_TURNING_ON
STATE_ON
,
STATE_TURNING_OFF
,
および STATE_OFF
このブロードキャストのリッスンは、アプリでランタイムを検出する必要がある場合に役立ちます。
Bluetooth の状態に加えられた変更。
ヒント: 検出の許可を有効にすると、
Bluetooth。以前にデバイスの検出の許可を一貫して有効にする予定の場合は、
Bluetooth アクティビティを実行している場合は、前のステップのステップ 2 をスキップできます。
デバイスで Bluetooth を有効にすると、Bluetooth クラシックと
Bluetooth Low Energy。
クラシック Bluetooth の場合は、Bluetooth デバイスを探すことができます
および
Bluetooth デバイスに接続する
Bluetooth Low Energy の場合は、BLE デバイスを検索し、GATT サーバーに接続して、
BLE データを転送します。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[null,null,["最終更新日 2025-07-27 UTC。"],[],[],null,["# Set up Bluetooth\n\nBefore your app can communicate over Bluetooth or Bluetooth Low Energy,\nyou need to verify that Bluetooth is supported on the device, and if it is,\nensure that it is enabled. Note that this check is only necessary if the\n`android:required` attribute in the `\u003cuses-feature.../\u003e` manifest file entry is\nset to `false`.\n\nIf Bluetooth isn't supported, then you should gracefully disable any Bluetooth\nfeatures. If Bluetooth is supported, but disabled, then you can request that the\nuser enable Bluetooth without leaving your app.\n\nThe first step is\n[adding the Bluetooth permissions](/develop/connectivity/bluetooth/bt-permissions#declare)\nto your manifest file in order to use the following APIs.\n\nOnce the permissions are in place, Bluetooth setup is accomplished in two steps\nusing the [`BluetoothAdapter`](/reference/android/bluetooth/BluetoothAdapter):\n\n1. Get the `BluetoothAdapter`.\n\n The `BluetoothAdapter` is required for any and all Bluetooth activity. The\n `BluetoothAdapter` represents the device's own Bluetooth adapter (the\n Bluetooth radio). To get a `BluetoothAdapter`, you first need to have a\n [`Context`](/reference/android/content/Context). Use this context to obtain\n an instance of the [`BluetoothManager`](/reference/android/bluetooth/BluetoothManager)\n system service. Calling [`BluetoothManager#getAdapter`](/reference/android/bluetooth/BluetoothManager#getAdapter)\n will give you a `BluetoothAdapter` object. If `getAdapter()` returns null,\n then the device doesn't support Bluetooth.\n\n For example: \n\n ### Kotlin\n\n ```kotlin\n val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)\n val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.getAdapter()\n if (bluetoothAdapter == null) {\n // Device doesn't support Bluetooth\n }\n ```\n\n ### Java\n\n ```java\n BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);\n BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();\n if (bluetoothAdapter == null) {\n // Device doesn't support Bluetooth\n }\n ```\n2. Enable Bluetooth.\n\n Next, you need to ensure that Bluetooth is enabled. Call\n [`isEnabled()`](/reference/android/bluetooth/BluetoothAdapter#isEnabled()) to\n check whether Bluetooth is currently enabled. If this method returns false,\n then Bluetooth is disabled. To request that Bluetooth be enabled, call\n [`startActivityForResult()`](/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)),\n passing in an\n [`ACTION_REQUEST_ENABLE`](/reference/android/bluetooth/BluetoothAdapter#ACTION_REQUEST_ENABLE)\n intent action. This call issues a request to enable Bluetooth through the\n system settings (without stopping your app).\n\n For example: \n\n ### Kotlin\n\n ```kotlin\n if (bluetoothAdapter?.isEnabled == false) {\n val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)\n startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)\n }\n ```\n\n ### Java\n\n ```java\n if (!bluetoothAdapter.isEnabled()) {\n Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);\n startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);\n }\n ```\n\n \u003cbr /\u003e\n\nA dialog appears requesting user permission to enable Bluetooth, as shown in\nfigure 1. If the user grants permission, the system begins to enable Bluetooth,\nand focus returns to your app once the process completes (or fails).\n\n\u003cbr /\u003e\n\n\n**Figure 1.** The enabling Bluetooth dialog.\n\nThe `REQUEST_ENABLE_BT` constant passed to\n[`startActivityForResult()`](/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int))\nis a locally-defined integer that must be greater than or equal to 0. The system\npasses this constant back to you in your\n[`onActivityResult()`](/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent))\nimplementation as the `requestCode` parameter.\n\nIf enabling Bluetooth succeeds, your activity receives the\n[`RESULT_OK`](/reference/android/app/Activity#RESULT_OK) result code in the\n`onActivityResult()` callback. If Bluetooth was not enabled due to an error (or\nthe user responded \"Deny\") then the result code is\n[`RESULT_CANCELED`](/reference/android/app/Activity#RESULT_CANCELED).\n\nOptionally, your app can also listen for the\n[`ACTION_STATE_CHANGED`](/reference/android/bluetooth/BluetoothAdapter#ACTION_STATE_CHANGED)\nbroadcast intent, which the system broadcasts whenever the Bluetooth state\nchanges. This broadcast contains the extra fields\n[`EXTRA_STATE`](/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE) and\n[`EXTRA_PREVIOUS_STATE`](/reference/android/bluetooth/BluetoothAdapter#EXTRA_PREVIOUS_STATE),\ncontaining the new and old Bluetooth states, respectively. Possible values for\nthese extra fields are\n[`STATE_TURNING_ON`](/reference/android/bluetooth/BluetoothAdapter#STATE_TURNING_ON),\n[`STATE_ON`](/reference/android/bluetooth/BluetoothAdapter#STATE_ON),\n[`STATE_TURNING_OFF`](/reference/android/bluetooth/BluetoothAdapter#STATE_TURNING_OFF),\nand [`STATE_OFF`](/reference/android/bluetooth/BluetoothAdapter#STATE_OFF).\nListening for this broadcast can be useful if your app needs to detect runtime\nchanges made to the Bluetooth state. \n**Tip:** Enabling discoverability automatically enables Bluetooth. If you plan to consistently enable device discoverability before performing Bluetooth activity, you can skip step 2 in the earlier steps.\n\nOnce Bluetooth is enabled on the device, you can use both Bluetooth classic and\nBluetooth Low Energy.\n\nFor Bluetooth classic, you can [find Bluetooth devices](/develop/connectivity/bluetooth/find-bluetooth-devices)\nand\n[connect to Bluetooth devices](/develop/connectivity/bluetooth/connect-bluetooth-devices).\n\nFor Bluetooth Low Energy, you can [find BLE devices](/develop/connectivity/bluetooth/find-ble-devices), [connect to a GATT server](/develop/connectivity/bluetooth/connect-gatt-server), and\n[transfer BLE data](/develop/connectivity/bluetooth/transfer-ble-data)."]]