إعداد البلوتوث
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
قبل أن يتمكن التطبيق من الاتصال عبر البلوتوث أو Bluetooth Low Energy،
عليك التأكّد من أنّ البلوتوث متوافق مع الجهاز، وإذا كان متوفرًا،
تأكَّد من تفعيله. لاحظ أن هذا الفحص لا يكون ضروريًا إلا إذا تم
السمة android:required
في إدخال ملف البيان <uses-feature.../>
هي
تم الضبط على false
في حال عدم توفّر تقنية البلوتوث، يجب إيقاف أي تقنية متوافقة مع البلوتوث.
الجديدة. إذا كانت تقنية البلوتوث متاحة ولكنها غير مفعَّلة، يمكنك طلب
تفعيل المستخدم للبلوتوث بدون مغادرة التطبيق
تتمثل الخطوة الأولى في
إضافة أذونات البلوتوث
إلى ملف البيان من أجل استخدام واجهات برمجة التطبيقات التالية.
بعد الحصول على الأذونات، يتم إكمال إعداد البلوتوث في خطوتَين.
باستخدام BluetoothAdapter
:
احصل على BluetoothAdapter
.
يجب توفُّر "BluetoothAdapter
" لجميع أنشطة البلوتوث. تشير رسالة الأشكال البيانية
BluetoothAdapter
يمثل محوّل البلوتوث في الجهاز (
لاسلكي عبر البلوتوث). للحصول على BluetoothAdapter
، يجب أولاً أن يكون لديك
Context
استخدم هذا السياق للحصول على
مثال على BluetoothManager
خدمة نظام التشغيل. جارٍ الاتصال بالرقم BluetoothManager#getAdapter
ستحصل على كائن BluetoothAdapter
. إذا عرضت getAdapter()
قيمة خالية،
فهذا يعني أن الجهاز لا يتوافق مع البلوتوث.
مثلاً:
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
}
تفعيل البلوتوث
بعد ذلك، عليك التأكّد من تفعيل البلوتوث. اتصل
isEnabled()
إلى
تحقّق مما إذا كانت تقنية البلوتوث مفعَّلة حاليًا. إذا قامت هذه الطريقة بإرجاع خطأ،
فسيتم إيقاف البلوتوث. لطلب تفعيل البلوتوث، يُرجى الاتصال
startActivityForResult()
،
ACTION_REQUEST_ENABLE
الهدف من الإجراء. تصدر هذه المكالمة طلبًا لتفعيل البلوتوث من خلال
إعدادات النظام (بدون إيقاف التطبيق).
مثلاً:
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);
}
يظهر مربع حوار يطلب من المستخدم إذن المستخدم لتفعيل البلوتوث، كما هو موضّح في
الشكل 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
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)."]]