블루투스 설정
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
앱이 블루투스 또는 저전력 블루투스를 통해 통신하려면
기기에서 블루투스가 지원되는지 확인해야 하며, 지원되는 경우
사용 설정되어 있는지 확인합니다 이 확인은
<uses-feature.../>
매니페스트 파일 항목의 android:required
속성:
false
로 설정합니다.
블루투스가 지원되지 않는 경우 블루투스를 정상적으로 비활성화해야 합니다.
기능을 살펴보겠습니다 블루투스가 지원되지만 비활성화된 상태라면
사용자가 앱을 종료하지 않고 블루투스를 활성화할 수 있습니다.
첫 번째 단계는
블루투스 권한 추가
추가해야 합니다.
권한이 부여되면 두 단계로 블루투스 설정이 완료됩니다.
BluetoothAdapter
사용:
BluetoothAdapter
를 가져옵니다.
BluetoothAdapter
는 모든 블루투스 활동에 필요합니다. 이
BluetoothAdapter
는 기기의 자체 블루투스 어댑터(
블루투스 라디오). BluetoothAdapter
를 사용하려면 먼저
Context
이 컨텍스트를 사용하여
BluetoothManager
의 인스턴스
시스템 서비스로 이동합니다. BluetoothManager#getAdapter
에 전화 거는 중
BluetoothAdapter
객체가 제공됩니다. getAdapter()
가 null을 반환하면
기기에서 블루투스를 지원하지 않는 것입니다.
예를 들면 다음과 같습니다.
Kotlin
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.getAdapter()
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
자바
BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
블루투스를 활성화합니다.
이제 블루투스를 활성화해야 합니다. 전화걸기
isEnabled()
(으)로
현재 블루투스가 사용 설정되어 있는지 확인합니다. 이 메서드가 false를 반환하면
블루투스가 비활성화됩니다. 블루투스를 사용 설정하도록 요청하려면
startActivityForResult()
님,
인코더-디코더 아키텍처를
ACTION_REQUEST_ENABLE
인텐트 작업입니다. 이 호출은
시스템 설정 (앱을 중지하지 않음)
예를 들면 다음과 같습니다.
Kotlin
if (bluetoothAdapter?.isEnabled == false) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
자바
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
아래와 같이 블루투스를 사용 설정하기 위해 사용자 권한을 요청하는 대화상자가 나타납니다.
그림 1. 사용자가 권한을 부여하면 시스템이 Bluetooth를 활성화하기 시작합니다.
프로세스가 완료되거나 실패하면 포커스가 앱으로 반환됩니다.
그림 1. 블루투스 사용 설정 대화상자
REQUEST_ENABLE_BT
상수가
startActivityForResult()
0 이상이어야 하는 로컬에서 정의된 정수입니다. 시스템
이 상수를
onActivityResult()
드림
구현을 requestCode
매개변수로 전달합니다.
블루투스 사용에 성공하면 활동이
RESULT_OK
결과 코드가
onActivityResult()
콜백에 전달합니다. 오류로 인해 블루투스를 사용할 수 없는 경우 (또는
사용자가 '거부')를 응답한 경우 결과 코드는
RESULT_CANCELED
선택적으로 앱에서
ACTION_STATE_CHANGED
드림
브로드캐스트 인텐트를 처리하며 블루투스 상태가
있습니다. 이 브로드캐스트에는
EXTRA_STATE
및
EXTRA_PREVIOUS_STATE
,
새 블루투스 상태와 이전 블루투스 상태를 각각 포함합니다. 가능한 값
이러한 추가 필드는
STATE_TURNING_ON
님,
STATE_ON
,
STATE_TURNING_OFF
,
및 STATE_OFF
.
앱에서 런타임을 감지해야 하는 경우 이 브로드캐스트 수신 대기가 유용할 수 있습니다.
블루투스 상태에 대한 변경사항입니다.
팁: 검색 기능을 사용 설정하면 자동으로 사용 설정됩니다.
블루투스 다음 날짜 이전에 기기 검색 기능을 지속적으로 사용 설정할 계획이라면
연결하는 경우 이전 단계의 2단계를 건너뛸 수 있습니다.
기기에서 블루투스가 사용 설정되면 블루투스 클래식 및
저전력 블루투스.
블루투스 클래식의 경우 블루투스 기기를 검색할 수 있습니다.
및
블루투스 기기에 연결합니다.
저전력 블루투스의 경우 BLE 기기를 찾고 GATT 서버에 연결할 수 있습니다.
BLE 데이터를 전송합니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 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)."]]