اعتبارًا من 27 آذار (مارس) 2025، ننصحك باستخدام android-latest-release
بدلاً من aosp-main
لإنشاء AOSP والمساهمة فيه. لمزيد من المعلومات، يُرجى الاطّلاع على التغييرات في AOSP.
الإعلانات منخفضة الطاقة للبلوتوث
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
توفّر تقنية Bluetooth Low Energy (BLE) الطاقة من خلال البقاء في وضع السكون معظم الوقت. ويتم تنشيطه فقط لعرض الإعلانات وإجراء اتصالات قصيرة، لذا تؤثّر الإعلانات في كلّ من استهلاك الطاقة وعرض النطاق الترددي لنقل البيانات.
إضافة إعلانات Bluetooth 5
يتوافق الإصدار 8.0 من نظام التشغيل Android مع الإصدار 5 من Bluetooth، ما يوفّر تحسينات في البث وإعلانات مرنة للبيانات في Bluetooth Low Energy. يتوافق الإصدار 5 من البلوتوث مع طبقات BLE المادية (PHY) التي تحافظ على انخفاض استهلاك الطاقة في الإصدار 4.2 من البلوتوث وتتيح للمستخدمين اختيار زيادة النطاق الترددي أو المدى. يمكنك العثور على مزيد من المعلومات في
مواصفات Bluetooth 5 الأساسية.
التنفيذ
تتوفّر ميزات البلوتوث 5 الجديدة تلقائيًا على الأجهزة التي تعمل بالإصدار 8.0 من نظام التشغيل Android والمزوّدة بأجهزة تحكّم متوافقة مع البلوتوث. استخدِم الطرق التالية BluetoothAdapter
للتحقّق مما إذا كان الجهاز متوافقًا مع ميزات Bluetooth 5:
isLe2MPhySupported()
isLeCodedPhySupported()
isLeExtendedAdvertisingSupported()
isLePeriodicAdvertisingSupported()
لإيقاف الميزات الإعلانية، عليك التواصل مع مورّد شريحة البلوتوث لإيقاف دعم مجموعة الشرائح.
تكون طبقات PHY الخاصة بتقنية Bluetooth حصرية لبعضها البعض، ويتم تحديد سلوك كل طبقة PHY مسبقًا من قِبل مجموعة Bluetooth SIG. يستخدم الإصدار 8.0 من نظام التشغيل Android تلقائيًا تقنية البلوتوث ذات الاستهلاك المنخفض للطاقة 1M
PHY، من الإصدار 4.2 من البلوتوث. تعرض حزمة android.bluetooth.le
ميزات الإعلان عن Bluetooth 5 من خلال واجهات برمجة التطبيقات التالية:
AdvertisingSet
AdvertisingSetCallback
AdvertisingSetParameters
PeriodicAdvertisingParameters
أنشئ AdvertisingSet
لتعديل إعدادات إعلانات البلوتوث باستخدام طريقة startAdvertisingSet()
في android.bluetooth.le.BluetoothLeAdvertiser
. حتى إذا تم إيقاف إمكانية استخدام Bluetooth 5 أو ميزاته الإعلانية، يمكن أن تنطبق ميزات واجهة برمجة التطبيقات أيضًا على LE 1M PHY.
أمثلة
يستخدم تطبيق المثال هذا الطبقة الفيزيائية 1M للبلوتوث المنخفض الاستهلاك للطاقة في عرض الإعلانات:
// Start legacy advertising. Works for devices with 5.x controllers,
// and devices that support multi-advertising.
void example1() {
BluetoothLeAdvertiser advertiser =
BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
AdvertisingSetParameters parameters = (new AdvertisingSetParameters.Builder())
.setLegacyMode(true) // True by default, but set here as a reminder.
.setConnectable(true)
.setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
.setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
.build();
AdvertiseData data = (new AdvertiseData.Builder()).setIncludeDeviceName(true).build();
AdvertisingSetCallback callback = new AdvertisingSetCallback() {
@Override
public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
Log.i(LOG_TAG, "onAdvertisingSetStarted(): txPower:" + txPower + " , status: "
+ status);
currentAdvertisingSet = advertisingSet;
}
@Override
public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
Log.i(LOG_TAG, "onAdvertisingDataSet() :status:" + status);
}
@Override
public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) {
Log.i(LOG_TAG, "onScanResponseDataSet(): status:" + status);
}
@Override
public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
Log.i(LOG_TAG, "onAdvertisingSetStopped():");
}
};
advertiser.startAdvertisingSet(parameters, data, null, null, null, callback);
// After onAdvertisingSetStarted callback is called, you can modify the
// advertising data and scan response data:
currentAdvertisingSet.setAdvertisingData(new AdvertiseData.Builder().
setIncludeDeviceName(true).setIncludeTxPowerLevel(true).build());
// Wait for onAdvertisingDataSet callback...
currentAdvertisingSet.setScanResponseData(new
AdvertiseData.Builder().addServiceUuid(new ParcelUuid(UUID.randomUUID())).build());
// Wait for onScanResponseDataSet callback...
// When done with the advertising:
advertiser.stopAdvertisingSet(callback);
}
يستخدم تطبيق المثال هذا طبقة PHY التي تبلغ سرعتها 2 ميغابت في الثانية في تقنية البلوتوث المنخفض الطاقة للإعلان. يتحقّق التطبيق أولاً من أنّ الجهاز يتيح استخدام الميزات المستخدَمة. إذا كانت الميزات الإعلانية متوافقة، يضبط التطبيق BLE 2M PHY كطبقة PHY الأساسية. عندما يكون معدل نقل البيانات 2M PHY نشطًا، لا يتيح الإعلان استخدام وحدات التحكّم في الإصدار 4.x من البلوتوث، لذا يتم ضبط setLegacyMode
على false
. يعدّل هذا المثال المَعلمات أثناء عرض الإعلان، كما يوقف الإعلان مؤقتًا.
void example2() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothLeAdvertiser advertiser =
BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
// Check if all features are supported
if (!adapter.isLe2MPhySupported()) {
Log.e(LOG_TAG, "2M PHY not supported!");
return;
}
if (!adapter.isLeExtendedAdvertisingSupported()) {
Log.e(LOG_TAG, "LE Extended Advertising not supported!");
return;
}
int maxDataLength = adapter.getLeMaximumAdvertisingDataLength();
AdvertisingSetParameters.Builder parameters = (new AdvertisingSetParameters.Builder())
.setLegacyMode(false)
.setInterval(AdvertisingSetParameters.INTERVAL_HIGH)
.setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
.setPrimaryPhy(BluetoothDevice.PHY_LE_1M)
.setSecondaryPhy(BluetoothDevice.PHY_LE_2M);
AdvertiseData data = (new AdvertiseData.Builder()).addServiceData(new
ParcelUuid(UUID.randomUUID()),
"You can fit large amounts of data up to maxDataLength. This goes up to 1650 bytes. For legacy advertising this would not work".getBytes()).build();
AdvertisingSetCallback callback = new AdvertisingSetCallback() {
@Override
public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {
Log.i(LOG_TAG, "onAdvertisingSetStarted(): txPower:" + txPower + " , status: "
+ status);
currentAdvertisingSet = advertisingSet;
}
@Override
public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
Log.i(LOG_TAG, "onAdvertisingSetStopped():");
}
};
advertiser.startAdvertisingSet(parameters.build(), data, null, null, null, callback);
// After the set starts, you can modify the data and parameters of currentAdvertisingSet.
currentAdvertisingSet.setAdvertisingData((new
AdvertiseData.Builder()).addServiceData(new ParcelUuid(UUID.randomUUID()),
"Without disabling the advertiser first, you can set the data, if new data is less than 251 bytes long.".getBytes()).build());
// Wait for onAdvertisingDataSet callback...
// Can also stop and restart the advertising
currentAdvertisingSet.enableAdvertising(false, 0, 0);
// Wait for onAdvertisingEnabled callback...
currentAdvertisingSet.enableAdvertising(true, 0, 0);
// Wait for onAdvertisingEnabled callback...
// Or modify the parameters - for example, lower the tx power
currentAdvertisingSet.enableAdvertising(false, 0, 0);
// Wait for onAdvertisingEnabled callback...
currentAdvertisingSet.setAdvertisingParameters(parameters.setTxPowerLevel
(AdvertisingSetParameters.TX_POWER_LOW).build());
// Wait for onAdvertisingParametersUpdated callback...
currentAdvertisingSet.enableAdvertising(true, 0, 0);
// Wait for onAdvertisingEnabled callback...
// When done with the advertising:
advertiser.stopAdvertisingSet(callback);
}
إثبات الملكية
أجرِ اختبارات منتجات البلوتوث المناسبة للتأكّد من توافق الجهاز مع البلوتوث 5.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-08-16 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-08-16 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Bluetooth Low Energy advertising\n\nBluetooth Low Energy (BLE) conserves power by remaining in sleep mode most of\nthe time. It wakes up only to make advertisements and short connections, so\nadvertisements affect both power consumption and data transfer bandwidth.\n\nBluetooth 5 advertising extension\n---------------------------------\n\nAndroid 8.0 supports Bluetooth 5, which provides broadcasting improvements and\nflexible data advertisement for BLE. Bluetooth 5 supports BLE Physical Layers\n(PHYs) that retain the reduced power consumption of Bluetooth 4.2 and let users\nchoose increased bandwidth or range. More information can be found in the\n[Bluetooth 5 Core Specifications](https://www.bluetooth.com/specifications/adopted-specifications).\n\n### Implementation\n\nNew Bluetooth 5 features are automatically available for devices running Android\n8.0 with compatible Bluetooth controllers. Use these [`BluetoothAdapter`](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html)\nmethods to check if a device supports Bluetooth 5 features:\n\n- `isLe2MPhySupported()`\n- `isLeCodedPhySupported()`\n- `isLeExtendedAdvertisingSupported()`\n- `isLePeriodicAdvertisingSupported()`\n\nTo disable the advertising features, work with the Bluetooth chip vendor to\ndisable chip-set support.\n\nThe Bluetooth PHYs are exclusive of one another, and the behavior of each PHY is\npredefined by the Bluetooth SIG. By default, Android 8.0 uses Bluetooth LE 1M\nPHY, from Bluetooth 4.2. The [`android.bluetooth.le`](https://developer.android.com/reference/android/bluetooth/le/package-summary.html) package exposes the\nBluetooth 5 advertising features through these APIs:\n\n- `AdvertisingSet`\n- `AdvertisingSetCallback`\n- `AdvertisingSetParameters`\n- `PeriodicAdvertisingParameters`\n\nCreate an [`AdvertisingSet`](https://developer.android.com/reference/android/bluetooth/le/AdvertisingSet.html) to modify Bluetooth advertisement settings by\nusing the [`startAdvertisingSet()`](https://developer.android.com/reference/android/bluetooth/le/BluetoothLeAdvertiser.html#startAdvertisingSet(android.bluetooth.le.AdvertisingSetParameters,%20android.bluetooth.le.AdvertiseData,%20android.bluetooth.le.AdvertiseData,%20android.bluetooth.le.PeriodicAdvertisingParameters,%20android.bluetooth.le.AdvertiseData,%20int,%20int,%20android.bluetooth.le.AdvertisingSetCallback)) method in\n[`android.bluetooth.le.BluetoothLeAdvertiser`](https://developer.android.com/reference/android/bluetooth/le/BluetoothLeAdvertiser.html). Even if support for Bluetooth\n5 or its advertising features is disabled, the API features can also apply to LE\n1M PHY.\n\n#### Examples\n\nThis example app uses Bluetooth LE 1M PHY for advertising: \n\n // Start legacy advertising. Works for devices with 5.x controllers,\n // and devices that support multi-advertising.\n\n void example1() {\n BluetoothLeAdvertiser advertiser =\n BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();\n\n AdvertisingSetParameters parameters = (new AdvertisingSetParameters.Builder())\n .setLegacyMode(true) // True by default, but set here as a reminder.\n .setConnectable(true)\n .setInterval(AdvertisingSetParameters.INTERVAL_HIGH)\n .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)\n .build();\n\n AdvertiseData data = (new AdvertiseData.Builder()).setIncludeDeviceName(true).build();\n\n AdvertisingSetCallback callback = new AdvertisingSetCallback() {\n @Override\n public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {\n Log.i(LOG_TAG, \"onAdvertisingSetStarted(): txPower:\" + txPower + \" , status: \"\n + status);\n currentAdvertisingSet = advertisingSet;\n }\n\n @Override\n public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {\n Log.i(LOG_TAG, \"onAdvertisingDataSet() :status:\" + status);\n }\n\n @Override\n public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) {\n Log.i(LOG_TAG, \"onScanResponseDataSet(): status:\" + status);\n }\n\n @Override\n public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {\n Log.i(LOG_TAG, \"onAdvertisingSetStopped():\");\n }\n };\n\n advertiser.startAdvertisingSet(parameters, data, null, null, null, callback);\n\n // After onAdvertisingSetStarted callback is called, you can modify the\n // advertising data and scan response data:\n currentAdvertisingSet.setAdvertisingData(new AdvertiseData.Builder().\n setIncludeDeviceName(true).setIncludeTxPowerLevel(true).build());\n // Wait for onAdvertisingDataSet callback...\n currentAdvertisingSet.setScanResponseData(new\n AdvertiseData.Builder().addServiceUuid(new ParcelUuid(UUID.randomUUID())).build());\n // Wait for onScanResponseDataSet callback...\n\n // When done with the advertising:\n advertiser.stopAdvertisingSet(callback);\n }\n\nThis example app uses the BLE 2M PHY for advertising. The app first checks that\nthe device supports the features being used. If the advertising features are\nsupported, then the app configures BLE 2M PHY as the primary PHY. While 2M PHY\nis active, advertisement does not support Bluetooth 4.x controllers, so\n`setLegacyMode` is set to `false`. This example modifies parameters while\nadvertising and also pauses the advertisement. \n\n void example2() {\n BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();\n BluetoothLeAdvertiser advertiser =\n BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();\n\n // Check if all features are supported\n if (!adapter.isLe2MPhySupported()) {\n Log.e(LOG_TAG, \"2M PHY not supported!\");\n return;\n }\n if (!adapter.isLeExtendedAdvertisingSupported()) {\n Log.e(LOG_TAG, \"LE Extended Advertising not supported!\");\n return;\n }\n\n int maxDataLength = adapter.getLeMaximumAdvertisingDataLength();\n\n AdvertisingSetParameters.Builder parameters = (new AdvertisingSetParameters.Builder())\n .setLegacyMode(false)\n .setInterval(AdvertisingSetParameters.INTERVAL_HIGH)\n .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)\n .setPrimaryPhy(BluetoothDevice.PHY_LE_1M)\n .setSecondaryPhy(BluetoothDevice.PHY_LE_2M);\n\n AdvertiseData data = (new AdvertiseData.Builder()).addServiceData(new\n ParcelUuid(UUID.randomUUID()),\n \"You can fit large amounts of data up to maxDataLength. This goes up to 1650 bytes. For legacy advertising this would not work\".getBytes()).build();\n\n AdvertisingSetCallback callback = new AdvertisingSetCallback() {\n @Override\n public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status) {\n Log.i(LOG_TAG, \"onAdvertisingSetStarted(): txPower:\" + txPower + \" , status: \"\n + status);\n currentAdvertisingSet = advertisingSet;\n }\n\n @Override\n public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {\n Log.i(LOG_TAG, \"onAdvertisingSetStopped():\");\n }\n };\n\n advertiser.startAdvertisingSet(parameters.build(), data, null, null, null, callback);\n\n // After the set starts, you can modify the data and parameters of currentAdvertisingSet.\n currentAdvertisingSet.setAdvertisingData((new\n AdvertiseData.Builder()).addServiceData(new ParcelUuid(UUID.randomUUID()),\n \"Without disabling the advertiser first, you can set the data, if new data is less than 251 bytes long.\".getBytes()).build());\n\n // Wait for onAdvertisingDataSet callback...\n\n // Can also stop and restart the advertising\n currentAdvertisingSet.enableAdvertising(false, 0, 0);\n // Wait for onAdvertisingEnabled callback...\n currentAdvertisingSet.enableAdvertising(true, 0, 0);\n // Wait for onAdvertisingEnabled callback...\n\n // Or modify the parameters - for example, lower the tx power\n currentAdvertisingSet.enableAdvertising(false, 0, 0);\n // Wait for onAdvertisingEnabled callback...\n currentAdvertisingSet.setAdvertisingParameters(parameters.setTxPowerLevel\n (AdvertisingSetParameters.TX_POWER_LOW).build());\n // Wait for onAdvertisingParametersUpdated callback...\n currentAdvertisingSet.enableAdvertising(true, 0, 0);\n // Wait for onAdvertisingEnabled callback...\n\n // When done with the advertising:\n advertiser.stopAdvertisingSet(callback);\n }\n\n### Verification\n\nRun applicable [Bluetooth product tests](https://www.bluetooth.com/develop-with-bluetooth/build/test-tools/) to verify device\ncompatibility with Bluetooth 5."]]