بلوتوث را تنظیم کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
قبل از اینکه برنامه شما بتواند از طریق بلوتوث یا بلوتوث کم انرژی ارتباط برقرار کند، باید تأیید کنید که بلوتوث در دستگاه پشتیبانی میشود و اگر پشتیبانی میشود، مطمئن شوید که فعال است. توجه داشته باشید که این بررسی فقط در صورتی ضروری است که ویژگی android:required
در ورودی فایل مانیفست <uses-feature.../>
روی false
تنظیم شده باشد.
اگر بلوتوث پشتیبانی نمیشود، باید هر یک از ویژگیهای بلوتوث را به راحتی غیرفعال کنید. اگر بلوتوث پشتیبانی میشود، اما غیرفعال است، میتوانید از کاربر درخواست کنید که بلوتوث را بدون ترک برنامهتان فعال کند.
اولین قدم اضافه کردن مجوزهای بلوتوث به فایل مانیفست خود برای استفاده از APIهای زیر است.
پس از برقراری مجوزها، راه اندازی بلوتوث در دو مرحله با استفاده از BluetoothAdapter
انجام می شود:
BluetoothAdapter
دریافت کنید.
BluetoothAdapter
برای تمام فعالیتهای بلوتوث مورد نیاز است. BluetoothAdapter
نشان دهنده آداپتور بلوتوث خود دستگاه (رادیو بلوتوث) است. برای دریافت BluetoothAdapter
، ابتدا باید یک Context
داشته باشید. از این زمینه برای به دست آوردن نمونه ای از سرویس سیستم BluetoothManager
استفاده کنید. با فراخوانی BluetoothManager#getAdapter
یک شئ BluetoothAdapter
در اختیار شما قرار می گیرد. اگر getAdapter()
null را برگرداند، دستگاه از بلوتوث پشتیبانی نمی کند.
به عنوان مثال:
کاتلین
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
را انجام دهید. این تماس درخواستی برای فعال کردن بلوتوث از طریق تنظیمات سیستم (بدون توقف برنامه شما) صادر می کند.
به عنوان مثال:
کاتلین
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 نشان داده شده است، یک گفتگو ظاهر می شود که از کاربر اجازه می دهد تا بلوتوث را فعال کند. اگر کاربر اجازه دهد، سیستم شروع به فعال کردن بلوتوث می کند و پس از تکمیل فرآیند (یا با شکست) تمرکز به برنامه شما برمی گردد.

شکل 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 و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],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)."]]